marked-down
marked-down

Reputation: 10408

"Extend" a user's subcription with Stripe using Laravel cashier?

I'm using Stripe to manage a subscription service, and Laravel Cashier as my API interface for Stripe's services.

I have a situation where I want to sign a user up to a service, and then occasionally, extend their subscription beyond the original subscription end date by an arbitrary amount. This is supported by Stripe, and the recommended way of doing so is to use the "subscription update" endpoint and pass a new trial_end value and a prorate value of false/null.

What I'm wondering is if Laravel Cashier supports this functionality. I've tried:

$user->subscription()->noProrate()->trialFor($newSubscriptionEndDate); 

But, my stripe dashboard doesn't appear to be showing the change registering.

Is this something I can accomplish using Cashier exclusively or do I need to use the native Stripe API? The StripeGateway class does have a number of methods pertaining to trials and end dates, but I'm having trouble deciphering their intended functionality. Thanks.

Upvotes: 2

Views: 2119

Answers (2)

Ali Nazari
Ali Nazari

Reputation: 1438

You can write a new function in your model (in which you have used Billable trait, probably User):

class User extends Model implements BillableContract
{
    use Billable;

    public function MyCustomeCashierFunction($newSubscriptionEndDate)
    {
        retrun $this->subscription()->noProrate()->trialFor($newSubscriptionEndDate); 
    }
}

Then you can use this function with your model object in your controller:

$user->MyCustomeCashierFunction($newSubscriptionEndDate);

Edit:

Also try this if the function didn't work:

public function MyCustomeCashierFunction($newSubscriptionEndDate)
{
    $stripe_gateway = new StripeGateway($this);

    retrun $stripe_gateway->subscription()->noProrate()->trialFor($newSubscriptionEndDate); 
}

Please let me know if it works.

Upvotes: 0

Tim Sheehan
Tim Sheehan

Reputation: 4024

I believe you need to use swap afterwards, e.g.

$user->subscription()->noProrate()->trialFor($newSubscriptionEndDate)->swap();

You can see what swap does here:

https://github.com/laravel/cashier/blob/5.0/src/Laravel/Cashier/StripeGateway.php#L181-L215

It returns the create method which actually updates as well if a customer object is passed through, so I'm assuming without that your changes won't make it to Stripe.


Here's a reference to someone using the same method with swap() https://github.com/laravel/cashier/pull/142

If you have problems saving the new trial date in your local DB there is a setTrialEndDate method in your Billable trait (used in your user model) which you can see here:

https://github.com/laravel/cashier/blob/5.0/src/Laravel/Cashier/Billable.php#L437-L448

You should be able to use it like this:

$user->setTrialEndDate( $date )->save();

EDIT

// getStripeKey is a static method
\Stripe\Stripe::setApiKey( $user::getStripeKey() );

// Get stripe id for customer using public method
$cu = \Stripe\Customer::retrieve( $user->getStripeId() );

// Get current stripe subscription id using public method
$subscription = $cu->subscriptions->retrieve( $user->getStripeSubscription() );

// Update trial end date and save
$subscription->trial_end = $date;
$subscription->save();

Then you can update it in Cashier manually with:

$user->setTrialEndDate( $date )->save();

Upvotes: 2

Related Questions