0
votes

I've just updated Laravel cashier package from 5 version to the latest 6 version. It supports multiple subscriptions and it's really cool. But I've got one problem with renewing subscription after subscription cancellation.

I'm removing subscription manually from stripe dashboard and customer.subscription.deleted event is firing.

Cashier method is catching this event: \Laravel\Cashier\Http\Controllers\WebhookController@handleWebhook

And $subscription->markAsCancelled(); is firing.

From that moment subscription cannot be renewed. I've tried to use resume() function, but subscription can be resumed only(!) on grace period.

In previous version of cashier I was using swap() method to resume subscription. Now it returns:

Stripe\Error\InvalidRequest: Customer cus_*** does not have a subscription with ID sub_***** in /**/vendor/stripe/stripe-php/lib/ApiRequestor.php:103 from API request 'req_****'

Creating new customer and subscription is not very efficient way. What your thoughts about this issue?

1

1 Answers

1
votes

My solution at this moment:

public function resume()
{
    $user = Auth::user();
    $subscription = $user->subscription(ServicePackageRepository::SUBSCRIPTION_NAME);

    if ($subscription->cancelled() && $subscription->onGracePeriod()) { 
        //if it was cancelled by user in grace period
        $subscription->resume();

        return $this->respondWithSaved([]);
    } else { //if cancelled by payment failure or smth else...
        if($user->subscription(ServicePackageRepository::SUBSCRIPTION_NAME)) {
            $user->newSubscription(ServicePackageRepository::SUBSCRIPTION_NAME,
                    $user->subscription(ServicePackageRepository::SUBSCRIPTION_NAME)->stripe_plan)
                ->create();

            return $this->respondWithSaved([]);
        } else {
            return $this->respondWithError([]);
        }
    }
}