Shankar S Bavan
Shankar S Bavan

Reputation: 962

Laravel Cashier - Cancel a subscription using the stripe id

I'm using Laravel Cashier with stripe payment. One user can have multiple subscriptions. User should able to cancel particular subscription. Is there anyway to cancel subscription by stripe id or plan id?

Upvotes: 3

Views: 5925

Answers (6)

Hadayat Niazi
Hadayat Niazi

Reputation: 2480

You can do this by using the cashier Subscription model as like Laravel model

Cancel Subscription

 $subscription = Subscription::where('name', 'default')->where('user_id', auth()->id())->first();
 $subscription->cancel();

Resume Subscription

 $subscription = Subscription::where('name', 'default')->where('user_id', auth()->id())->first();
 $subscription->resume();

Upvotes: 0

Koushik Das
Koushik Das

Reputation: 10793

You don't need to have the id to cancel.

You can just call

$subscription->cancel();

and it will work. If you have multiple subscriptions which you would like to cancel at the same time. You can do this. Let's find all the active subscriptions. You can find all the scopes here

$subscriptions = $user->subscriptions()->active()->get(); // getting all the active subscriptions 

$subscriptions->map(function($subscription) {
    $subscription->cancel(); // cancelling each of the active subscription
});

Upvotes: 0

Tiago Silva Pereira
Tiago Silva Pereira

Reputation: 336

You can just call the cancel() method directly on your subscription:

$subscription->cancel();

As you can see here (Github Reference), the method $this->subscription called from your User model just gets a Subscription instance filtering by name:

/**
 * Get a subscription instance by name.
 *
 * @param  string  $name
 * @return \Laravel\Cashier\Subscription|null
 */
public function subscription($name = 'default')
{
    return $this->subscriptions->where('name', $name)->first();
}

And, as you can see (Github Reference), the Subscription model has its own cancel method:

/**
 * Cancel the subscription at the end of the billing period.
 *
 * @return $this
 */
public function cancel()
{
    // ...
}

So, you can always call it directly on the Subscription instance.

Upvotes: 1

John Shipp
John Shipp

Reputation: 1153

I'm sure this is documented somewhere, but for the life of me I can't find it, but, if you pass the stripe subscription id in as the 2nd parameter you can cancel any subscription the Laravel way...

$user->subscription('default', 'price_abcdefghi12345')->cancel();

Upvotes: 2

Raza Rafaideen
Raza Rafaideen

Reputation: 2241

You can use it with Laravel Cashier using subscription id .

In Laravel You can find the subscription id in the subscriptions table in your database.(column name is stripe_id)

 $subscription = \Stripe\Subscription::retrieve($subscription_id);

If you pass the correct subscription id , you will get the subscription details

To cancel the subscription

 $sub->cancel();

Update your subscriptions table for the particular subscription id (again column name is stripe_id). In my

 \Stripe\Subscription::where('stripe_id', $sub->id)
        ->update([
            'trial_ends_at' => Carbon::now()->toDateTimeString(),
         ]);

Upvotes: 4

rajathans
rajathans

Reputation: 197

You can use the PHP Stripe library to do it.

To cancel immediately

$sub = Stripe\Subscription::retrieve($subscription_id);
$sub->cancel();

To cancel after current period end

$sub = Stripe\Subscription::update($subscription_id, [
   'cancel_at_period_end' => true
]);

Upvotes: 4

Related Questions