Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add coupon to *Stripe subscription* with Laravel Cashier after subscription already created

According to the Stripe documentation, it appears as though you can actually apply coupons to a customer as a whole, but you can also apply coupons to their subscriptions. I am trying to apply a coupon to the customer's subscription after the subscription has already been created. I am using Laravel Cashier 4.2.

Here is what I have tried:

 $company = Auth::user()->company;
 $customer = $company->subscription()->getStripeCustomer(); // returns a StripeGateway object
 $customer->subscription->applyCoupon($input['coupon_code']);

Here is the error message:

"Call to undefined method Stripe_Subscription::updateSubscription()"

I can use the applyCoupon() method to a customer as a whole, but not an actual subscription... Ideas appreciated.

The Stripe documentation only shows how to remove a discount from a subscription: Discount Object. The only other information I've been able to find in their documentation is:

Coupons and discounts

If you want to provide discounts to certain customers, you can create coupon codes in the Dashboard. Coupons have a discount percentage and a duration, so you could create coupons like a 10% lifetime discount, or a one month 50% discount, etc. Coupons can also have expiration dates attached, after which they can't be used. Here's an example of adding a discount to a user's subscription. In this case, we've already created a coupon called 50OFF1MONTH:

curl https://api.stripe.com/v1/customers/cus_4fdAW5ftNQow1a \ -u sk_test_4LOIhlh19aUz8yFBLwMIxnBY: \ -d coupon=50OFF1MONTH

However, this isn't very helpful. Once again, Laravel's documentation is a little to elegant, and it's missing any info on the topic.

I wonder if I just need to recreate the subscription object entirely with the new coupon... But that's not ideal because you need a card token for that.

Update 1

I can confirm that this does in fact apply a coupon to the subscription itself

$user->subscription('monthly')
 ->withCoupon('code')
 ->create($creditCardToken);

However, once again the question is how can a coupon be added after the fact.

like image 350
Marcel Gruber Avatar asked Feb 08 '23 09:02

Marcel Gruber


2 Answers

Starting with Cashier v11.* there are new convenience methods to update the underlying Stripe subscription object. To apply a coupon to the user's Subscription model instead of the Customer model, try this in the controller:

$user->subscription('default')->updateStripeSubscription(['coupon' => $couponCode]);

To remove the coupon from the subscription, send an update request and pass an empty coupon string. As described under another question, a coupon set to an empty string removes it, while passing null is ignored.

$user->subscription('default')->updateStripeSubscription(['coupon' => '']);
like image 102
pv3_gabriel Avatar answered Feb 11 '23 00:02

pv3_gabriel


It doesn't look like what I'm trying to do is possible, so I found another way. All I do is swap the users existing plan to the current plan and use the withCoupon method. This does appear to apply the coupon to the subscription and doesn't seem to charge the user or change their billing period, but I could be wrong...

$company->subscription($company->stripe_plan)
        ->withCoupon($input['coupon_code'])
        ->swap(); 
like image 44
Marcel Gruber Avatar answered Feb 11 '23 00:02

Marcel Gruber