Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to Update a Subscription #936

Closed
connecteev opened this issue May 13, 2020 · 5 comments
Closed

Ability to Update a Subscription #936

connecteev opened this issue May 13, 2020 · 5 comments

Comments

@connecteev
Copy link

connecteev commented May 13, 2020

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:
2. Change the billing date and use anchorBillingCycleOn() on an existing subscription
3. 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
Copy link
Member

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
Copy link
Author

@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 [email protected] 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
Copy link
Author

@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
Copy link
Member

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: #943

@connecteev
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants