How to add a * Stripe * subscription coupon with Laravel Cashier after a subscription has already been created

According to the Stripe documentation, it looks like you can really apply coupons to the customer as a whole, but you can also apply coupons to their subscriptions. I am trying to apply a coupon to a customer’s subscription after the subscription has already been created. I am using Laravel Cashier 4.2.

Here is what I tried:

 $company = Auth::user()->company;
 $customer = $company->subscription()->getStripeCustomer(); // returns a StripeGateway object
 $customer->subscription->applyCoupon($input['coupon_code']);

Here is the error message:

"Call to undefined method Stripe_Subscription::updateSubscription()"

I can use the method applyCoupon()for the client as a whole, but not the actual subscription ... Ideas are appreciated.

The Stripe documentation only shows how to remove a discount from a subscription: Discount object . The only other information that I could find in their documentation is:

Coupons and discounts

, Dashboard. , , 10% , 50% .. , . . 50OFF1MONTH:

curl https://api.stripe.com/v1/customers/cus_4fdAW5ftNQow1a \ -u sk_test_4LOIhlh19aUz8yFBLwMIxnBY: \ -d coupon=50OFF1MONTH

. , Laravel , .

, ... , .

1

,

$user->subscription('monthly')
 ->withCoupon('code')
 ->create($creditCardToken);

, .

+4
3

, , . , , - withCoupon. , , , ...

$company->subscription($company->stripe_plan)
        ->withCoupon($input['coupon_code'])
        ->swap(); 
+3

. , :

try {
$user->applyCoupon('coupon_code');
return redirect()->back()->withMessage('Coupon Applied');  
} catch (Exception $e) {
return back()->withError($e->getMessage());
}

try/catch, , Stripe .

0

, , .

, Subscription Laravel\Cashier\Subscription.

withCoupon():

public function withCoupon($coupon)
{
    $this->coupon = $coupon;

    return $this;
}

swap() . - $subscription->save();.

...
// Applying the discount coupon if such exists
if ($this->coupon) {
     $subscription->coupon = $this->coupon;
}
...
$subscription->save();

, , , - Billable subscriptions(), Subscription.

After that, you can apply your own trait Billableto the paid model and apply coupons when exchanging plans.

$user
    ->subscription($product)
    ->withCoupon($coupon)
    ->swap($plan);
0
source

Source: https://habr.com/ru/post/1618742/


All Articles