Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 421
Shankar S Bavan Avatar asked Feb 03 '26 09:02

Shankar S Bavan


2 Answers

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
]);
like image 75
rajathans Avatar answered Feb 06 '26 01:02

rajathans


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(),
         ]);
like image 32
Raza Rafaideen Avatar answered Feb 05 '26 23:02

Raza Rafaideen