Reputation: 991
I have been trying to integrate a payment system for a subscription service using Laravel Cashier.
I have set up my RegistrationController
so that it works out how many days until the first day of the next month.
Using $user->trial_ends_at = Carbon::now()->addDays($datediff);
When creating a user it sets the trial end date for the first of the next month.
From here I want to use the $user->onTrial()
method to create the actual subscription.
Please note: Even though the user is created there is no customer created in Stripe as the subscription has not yet been created.
My questions:
In the registration form the card details are taken so the users can subscribe and set up their payment method. This involves stripe validating the card and sending back a token.
Can this token be stored and used later?
Assuming I can use this token later on, to create the actual subscription in cashier I have to check whether the users trial has ended using the onTrial method.
The subscription is created using this:
$user->subscription($subscription_plan)->create($stripeToken);
Where is best to add the above code so that the user is subscribed and the billing period is started at the end of the trial?
This isn't mentioned at all in the in the documentation so I was just wondering if I create a new controller to actually start the subscription where to call the function and how to get it to run as each users trial ends.
Edit:
Would something like this be possible using Queues?
http://laravel.com/docs/4.2/queues
$start_subscription = Carbon::now()->addDays($datediff);
Queue::later($start_subscription, 'RegistrationController@subscribe',
array('stripeToken' => $stripeToken, 'details' => $otherDetails));
and then run the subscription method for each user at the set time.
Upvotes: 0
Views: 830
Reputation: 5438
I noticed some issues with creating the Stripe customer at the same time as the subscription. I was ending up with lots of extra customers inside Stripe because if the customer is created, but the subscription has an error, it doesn't connect them in Cashier, but the customer is connected in Stripe.
So to separate the two, I created a helper method in the User model. It will let me create the customer without creating a subscription. I can then create a subscription at a later time.
I think this addresses your issue.
Helper methods in user model:
/**
* Method to create stripe customer without subscription
*/
public function createStripeCustomer($token, $properties) {
$gateway = $this->subscription();
$customer = $gateway->createStripeCustomer($token, $properties);
$gateway->updateLocalStripeData($customer);
}
public function getStripeCustomer() {
$gateway = $this->subscription();
return $gateway->getStripeCustomer();
}
Then in my API controller where I handle the subscription:
if (!$user->getStripeId()) {
$user->createStripeCustomer($inputs['token'], ['email' => $user->email]);
//this can be done at a much later time, the customer is already stored.
$user->subscription($inputs['plan'])->create(null, [], $user->getStripeCustomer());
Flash::success("Subscription Success!");
} elseif (!$user->getStripeSubscription()) {
$user->subscription($inputs['plan'])->create($inputs['token'], [], $user->getStripeCustomer());
Flash::success("Subscription Success!");
}
Upvotes: 1
Reputation: 2271
As for the Stripe token, you'll want to use that within a few moments of its creation. It's not expected to last for longer than that.
As for your other questions, I'm not a Laravel person, so I couldn't speak to that, but PHP's strtotime() function takes a variety of strings and returns a timestamp. So if you always wanted to have billing start at noon of the first of the next month, you'd set your timestamp to:
strtotime('first day of next month 12:00');
Hope that helps, Larry
PS I work on Support at Stripe.
Upvotes: 1