How to remove coupon from Stripe plan w/ Laravel Cashier 4

狂风中的少年 提交于 2019-12-10 23:43:08

问题


I am currently trying to implement a method that swaps people over to a new plan. The problem I am having is that the coupon from the old plan carries over and the user is not charged. Every time I try to remove the old coupon, it doesn't seem to allow it.

protected function swapToYearlyPlan(){
    $user = Auth::user();
    // Tried this, doesn't work
    // $user->subscription()->retrieve($user->stripe_subscription)->deleteDiscount();

    // This works fine
    $user->subscription('Gold Annual Plan')->swap();

    // Tried this, doesn't work
    //$user->subscription()->applyCoupon(NULL);

    return 'Upgraded plan!';
}

Thoughts are appreciated. Cheers.


回答1:


Here is what ended up working:

protected function swapToYearlyPlan(){
    $company = Auth::user()->company;
    $customer = $company->subscription()->getStripeCustomer();

    if($customer->subscription->discount instanceof Stripe_Object){
        $customer->subscription->deleteDiscount();
    }

    $company->subscription("Gold Annual Plan")->swapAndInvoice();

    // Dealing with GST is a whole other issue

    return 'Upgraded Gold Annual plan!';
}

I'm dealing with legacy code here, so there's a lot of details which aren't clear. For example, the deleteDiscount method isn't even a feature of Laravel Cashier, or at least the version I'm working with. That method is found included with my project in a whole other set of code located here: vendor/stripe/stripe-php/lib/Stripe, whereas Laravel Cashier is located in vendor/laravel/cashier/src/Laravel/Cashier.

Overall, I have found the Laravel documentation, once again, to be lacking verbosity and examples. It said it could deal with coupons, and it showed how to add one, but not how to remove one, so that makes me think that it can't, which might be why other libraries had to be included. But that's all speculation.



来源:https://stackoverflow.com/questions/33990191/how-to-remove-coupon-from-stripe-plan-w-laravel-cashier-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!