Nova Framework, Tutorials, Stripe API

Stripe API Cancel Subscription

When canceling a subscription, there are 2 options to use. 

I’m using Nova Framework not setup with Nova? please read Getting Stripe API setup with Nova Framework

To cancel a subscription retrieve the current subscription by passing the subscription id to create a subscription instance then call cancel().

Cancel can optionally take an array with the parameter of at_period_end which is a boolean when set, the subscription will be cancelled at the end of the subscription. The default will cancel the subscription immediately. 

$cancelNow = array('at_period_end' => true); // false will cancel now

//cancel
$stripe = Subscription::retrieve($subscription_id);
$result = $stripe->cancel($cancelNow);

//update user
$user = User::find(Auth::id());
$user->current_period_end = date('Y-m-d H:i:s', $result->current_period_end);
if ($result->canceled_at) {
    $user->canceled_at = date('Y-m-d H:i:s', $result->canceled_at);
}
$user->cancel_at_period_end = ($result->cancel_at_period_end) ? 'true' : 'false';
$user->status = $result->status;
$user->save();

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.