Reputation: 307
Simple problem but I cannot for the life of me figure this out.
I have 3 subscriptions on my Stripe account, one that's free and two that are paid with a 14 day trial period (set-up in Stripe). Everything works fine, except that when I go to subscribe new users to the paid trial plans right away I keep getting an error about the customer not having a payment source on file. My code looks like this:
// Create new User
$user = new Account($validatedUserData);
// Create Stripe user and subscribe to plan. Laravel automatically adds Stripe Customer ID
// Free plan, create customer and subscribe to free plan in one line of code
if ($planInRequest == 1) {
$user->newSubscription('My Subscription', 'Free Plan')->create(null, ['email' => $user->email]);
}
// If Paid plan...
else {
// Create user in Stripe first (this works)
$user->createAsStripeCustomer(null);
// Subscribe user to paid plan with trial period
$user->newSubscription('My Subscription', $planToUse)->create(null, ['email' => $user->email]); }
However, when I run this code I get the error
No Payment Source exists for Customer.
I know I haven't added a Stripe Source to the customer yet (hence passing null), but I thought if there was a free trial period that I'd be able to subscribe them to it for the time being. I tried NOT passing null into the paid plan subscription as well (as per this SO post), but that gave me an invalid token error.
My user model also contains the 'trial_ends_at', 'updated_at', and 'created_at' fields that the Laravel docs recommend. The Stripe customer gets added just fine, and when I try to add the paid subscriptions to the new customer in the Stripe Dashboard everything works fine.
Anyone have any ideas? Help greatly appreciated!
Upvotes: 0
Views: 502
Reputation: 307
Ended up figuring out I had to use the following code for my trial plans after digging through Cashier's Billable, Subscription, and SubscriptionBuilder Controllers, posting here in case anyone else runs into the same problem!
$user->newSubscription('My Subscription', $planToUse)->trialUntil($user->trial_ends_at)->create(null, [
'email' => $user->email,
'description' => $user->name
]);
Upvotes: 0