laravel / cashier-stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.
https://laravel.com/docs/billing
MIT License
2.38k stars 679 forks source link

Ability to Update a Subscription #936

Closed connecteev closed 4 years ago

connecteev commented 4 years ago

There is no way to update a subscription using Cashier. I'd like to be able to use Cashier to do the following:

  1. Use withCoupon() to apply a coupon to an existing subscription

It would also be nice to be able to do other things such as:

  1. Change the billing date and use anchorBillingCycleOn() on an existing subscription
  2. Possibly other things that Stripe allows in it's Update Subscription API

But even if we just get 1, that would be a start :)

Thanks for the work on Cashier, btw...definitely makes our lives easier.

driesvints commented 4 years ago

Hey @connecteev. Think you missed this section:

https://laravel.com/docs/7.x/billing#stripe-sdk

Screenshot 2020-05-13 at 14 16 18
connecteev commented 4 years ago

@driesvints I tried it but it does not work

This works fine:

            Stripe::setApiKey(env('STRIPE_SECRET'));
            \Stripe\Subscription::update(
                $stripeSubscriptionId,
                ['coupon' => $couponCode]
            );

However, this throws an exception:

            $stripeSubscription = $currentSubscription->asStripeSubscription();
            $stripeSubscriptionId = $stripeSubscription->id;
            $stripeSubscription->update(
                json_encode(['coupon' => $couponCode])
            );

Exception error message:

{
    "error": "No API key provided.  (HINT: set your API key using \"Stripe::setApiKey(<API-KEY>)\".  You can generate API keys from the Stripe web interface.  See https://stripe.com/api for details, or email support@stripe.com if you have any questions."
}

which is strange, because I have these defined in my .env file

STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret

Does it work for you? Can you apply a coupon this way?

connecteev commented 4 years ago

@driesvints one more note:

If I manually call Stripe::setApiKey(env('STRIPE_SECRET')); I can get past the "No API key provided." error exception. Even though calling the Stripe library directly like that defeats the purpose, I am encountered with a different error.

{
    "error": "No such subscription: {\"coupon\":\"coupon_forever_plan\"}"
}

Note that this is a valid subscription and valid coupon code, and it works fine on doing this:

            Stripe::setApiKey(env('STRIPE_SECRET'));
            \Stripe\Subscription::update(
                $stripeSubscriptionId,
                ['coupon' => $couponCode]
            );

(just trying to convey that in all likelihood it's not user-error)

driesvints commented 4 years ago

Hmm, odd. The example in the docs indeed appears to be wrong. I'll rectify it.

I've also send in a PR to add two convenience methods for updating the underlying stripe objects: https://github.com/laravel/cashier/pull/943

connecteev commented 4 years ago

thanks @driesvints once this is merged in I will test my code again