Phalcon Cashier have not implement so that to use it just install library at https://github.com/duythien/cashier .It provides a simple API for handling common tasks associated with subscriptions. In this tip we will take an in-depth look at working with subscriptions and coupons.

Install Phalcon cashier

First you need install Phalcon Cashier via composer, to do that just following command below:

composer require duythien/cashier

After that you need created table and some fields in your database

CREATE TABLE `subscriptions` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
  `stripe_id` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
  `stripe_plan` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
  `quantity` int(11) NOT NULL,
  `trial_ends_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `ends_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


ALTER TABLE `subscriptions`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `subscriptions`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;


ALTER TABLE `users` ADD `stripe_id` VARCHAR(200) NULL;
ALTER TABLE `users` ADD `card_brand` VARCHAR(200) NULL;
ALTER TABLE `users` ADD `card_last_four` VARCHAR(200) NULL;
ALTER TABLE `users` ADD `trial_ends_at` timestamp NULL DEFAULT NULL;

Model setup

use Phalcon\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}

Provider Keys in your config

Add some parameter in config.php such as like below:

'stripe' => [
        'model' => 'App\Models\Users',
        'secretKey' => null,
        'publishKey' => null
     ]

After configuration library, you will need to create them using the Stripe administration console. Once created, you'll need to integrate the necessary logic into your web application's subscription work-flow. Let is start by entering the administration console and creating a coupon.

Creating a Stripe Plan and Coupon

To create a coupon for your users, go to website stripe at https://dashboard.stripe.com The form to add a plan can be found in the sidebar under Subscriptions -> Plan and then click the new link on the right. The form will then open the following modal window:

The form Coupon: the sidebar under Subscriptions -> Coupon

Phalcon cashier

Which elements:

  • Percent Off and Amount Off: Give users this percentage off invoices

  • Duration: How long will the coupon last once applied to the customer.

  • ID Code: This is the code the user will enter to take advantage of the discount.

  • Max Redemptions:The coupon can only be used this many times in total.

  • Redeem By: Coupons can only be redeemed up to this date.

After creating your coupon, you can then e-mail the ID Code to your user and allow them to apply the discount.

Use it

To create a subscription, first retrieve an instance of your billable model, which typically will be an instance of App\User. Once you have retrieved the model instance, you may use the newSubscription method to create the model's subscription:

//$user = $this->auth->getUser();
$user = User::findFirst();

$user->newSubscription('main', 'monthly')->create($creditCardToken);

The first argument passed to the newSubscription method should be the name of the subscription. If your application only offers a single subscription, you might call this main or primary. The second argument is the specific Stripe plan the user is subscribing to. This value should correspond to the plan's identifier in Stripe.

The create method will automatically create the subscription, as well as update your database with the customer ID and other relevant billing information.You can also take a look example real:

public function subscriptionAction()
    {
       $user = Users::findFirst();
       $user->newSubscription('main', 'monthly-10-1')->create($this->getTestToken());
    }
    protected function getTestToken()
    {
        return \Stripe\Token::create([
            'card' => [
                'number' => '4242424242424242',
                'exp_month' => 5,
                'exp_year' => 2020,
                'cvc' => '123',
            ],
        ], ['api_key' => 'sk_test_xxxxxxxxx'])->id;
    }

Adding a Coupon to a New Subscription

All you have to do is utilize Cashier's withCoupon() method when creating the subscription. Here is an example:

$user->subscription('plan_id')
    ->withCoupon('coupon_code')
    ->create($stripe_token);

As you can see from this tip working with subscription and coupons is really simple with Phalcon and Stripe, right?