-
Notifications
You must be signed in to change notification settings - Fork 9
Using Phalcon Cashier
Phalcon Cashier provides an expressive, fluent interface to Stripe's and Braintree's subscription billing services. It handles almost all of the boilerplate subscription billing code you are dreading writing. In addition to basic subscription management, Cashier can handle coupons, swapping subscription, subscription "quantities", cancellation grace periods, and even generate invoice PDFs.
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;
Next, add the Billable trait to your model definition:
use Phalcon\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
}
Add some parameter in config.php such as like below:
'stripe' => [
'model' => 'App\Models\Users',
'secretKey' => null,
'publishKey' => null
]
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 = 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;
}
If you would like to specify additional customer details, you may do so by passing them as the second argument to the create method:
$user->newSubscription('main', 'monthly')->create($creditCardToken, [
'email' => $email,
]);