2

anyone used Laravel Cashier and has an answer to my below question. I want to cancel a subscription and in the docs it's written like that

$user->subscription('default')->cancelNow();

In my case the user owns a place and i added an extra table to the subscriptions migration which is "place_id" and has multiple subscriptions for each place he owns, can i make the above code more specific and write something like this so he can cancel the specific subscription of a place he owns

$user->subscription($planName)->where('place_id','=',$placeID)->cancelNow()

Also tried a subscription query

$subscription = Subscription::query()->active()->
where(['place_id', '=', $placeID], ['user_id', '=', $user->id])->get();

$subscription->cancelNow()

Doesn't work, it's just something i tried, anyone got any ideas? I'm just looking for a way to cancel a subscription with a more specific code, not just the name of the subscription. Thanks and regards!

Steve
  • 548
  • 1
  • 10
  • 21

2 Answers2

3

I have a similar situation where the paying user can have more than one subscriber with more than one subscription. I managed this by creating a migration that adds a subscriber_id field to the subscriptions table

Schema::table('subscriptions', function (Blueprint $table) {
    $table->integer('subscriber_id')->nullable()->after('user_id');
});

and then updating that ID after creating the subscription.

$subscription = $subscription->create($paymentMethod->id); // returns stripe subscription
$subscription = Subscription::find($subscription->id); // so you have ot get the local subscription from the db
$subscription->update(['subscriber_id' => $subscriber->id]);

I can then get all the subscriptions for a subscriber or a specific subscription by subscriber id and plan

$subscription = Subscription::where('subscriber_id', $subscriber->id)->where('stripe_plan','your_plan')...```

Essentially you could do the same but with place_id

justrusty
  • 827
  • 7
  • 10
  • That's exactly what i did, but i can't get the function cancelNow to work, where should i use it exactly? – Steve Nov 14 '20 at 13:33
  • What's the error? What are you getting returned from ```$subscription = Subscription::query()->active()-> where(['place_id', '=', $placeID], ['user_id', '=', $user->id])->get();```? have you dumped out the result? You should add that to your question as you actually asked how to get the sub not why the ```cancelNow()``` method doesn't work on the returned subscription. You may need something like ```asStripeSubscription()``` – justrusty Nov 14 '20 at 13:38
  • The question doesn't say anywhere how to get the sub, it says how to cancel a specific subscription. – Steve Nov 14 '20 at 13:50
  • You need to post your errors and the result returned from the query – justrusty Nov 14 '20 at 14:00
  • There are no errors, it returns the subscription correctly, i just can't find a way to cancel the returned subscription. – Steve Nov 14 '20 at 14:04
2

So i solved my issue, instead of using

->get(); 

I used:

->first();

Like this:

$subscription = Subscription::query()->active()->where([['place_id', '=', $placeID], ['user_id', '=', $user->id]])->first();

$subscription->cancelNow();

That's because first() method will return only one record, while the get() method will return an array of records even though in my case it returned only one record as it was supposed to return, but first() made it work and it solved my problem.

Steve
  • 548
  • 1
  • 10
  • 21
  • Hence why I said you need to post your returned result from the query. If it was an array or collection it would have been immediately obvious. Getting a good answer requires a well formulated question with as much detail as poss :) – justrusty Nov 15 '20 at 11:08