1

I'm trying to update a subscription and add a coupon at the same time. Here's the code:

$user->subscription('subscription_name')->withCoupon($request->stripe_coupons)->swap($planName);

The error I'm getting is Call to undefined method Laravel\Cashier\Subscription::withCoupon()

My understanding is that this should just work. If I remove the ->withCoupon() bit, it works fine, and I can see the method in the cashier code. What am I doing wrong??

Because it's been mentioned, as far as I can tell I'm on the latest version. Cashier was installed using composer require laravel/cashier, but that seems to have put me on "laravel/cashier": "^9.3", in my composer.json file.

How do I fix?

TH1981
  • 3,105
  • 7
  • 42
  • 78
  • That functionality has been added in cashier version 10. Check that you have that latest master version. Attached links of the repository where they indicate the resolution of the same. It's issue # 580. Check this. https://github.com/laravel/cashier/pull/620 – NewWorldNeverland Jul 15 '19 at 15:28
  • https://github.com/laravel/cashier/blob/master/src/SubscriptionBuilder.php#L162 This shows the method on the `SubscriptionBuilder` class but the [`Subscription` class](https://github.com/laravel/cashier/blob/master/src/Subscription.php) does not have the `withCoupon` method. – stokoe0990 Jul 15 '19 at 15:28
  • You can see the development code here https://github.com/laravel/cashier/pull/620/commits/5fa250a07c3217975a910efc4f7e8c26e8c10b13. Remember to check that you use the version where this update is available. @TH1981, please vote positively for my comments if it has been useful. Thank you. – NewWorldNeverland Jul 15 '19 at 15:41

1 Answers1

3

Based on the comments that I put, the code would be like this.

    $subscription = $user->subscription('subscription_name');

    // Swap Plan with Coupon
    $subscription->swap($planName, [
        'coupon' => $request->stripe_coupons,
    ]);

You can see the development code here

Repository laravel/cashier master

Updated

Apparently, version 10 is still in development. (dev-master)

Try updating the composer to see if it is updated.

composer update

If you do not update, do it manually. Modify composer.json.

"require":
{
    ...
    "laravel/cashier": "dev-master",
    ...
}

Run composer update again to update the changes.

But if it works, it will be clear to you that, until you release the production version, you will not have that functionality available.

I warn you that it is not advisable to use the development version in production. Because they have not passed the necessary tests to verify a minimum stability.

After trying, you can always go back to the previous.

"require":
{
    ...
    "laravel/cashier": "^9.3"
    ...
}

Run composer update again to update the changes.

Please, accept it as the correct answer, if it has been useful and a favorable vote if you think I deserve it, thank you very much.

A cordial greeting.

  • 1
    I updated the code but it didn't work. stopped giving an error but didn't add the coupon. I've also updated the question regarding the version. – TH1981 Jul 15 '19 at 18:57
  • @TH1981, please, check my answer I have modified it to try to indicate how to proceed. – NewWorldNeverland Jul 15 '19 at 20:58