我正在使用Stripe进行订阅,因此当用户取消订阅时(关闭自动续订),它会保持订阅有效,直到Stripe的结算周期结束.
该操作通过Stripe工作,但我如何设置以便cancelled
我的数据库中的列具有相同的效果?目前,如果用户点击取消订阅链接,它会将其cancelled
列标记为1
.我希望它不会在结算期结束前标记为已取消,因此用户可以继续访问该网站,直到最后一个结算日(我已打开autorenwal)
我已阅读txdavidtx建议.他建议将所有用户标记为在结算周期结束时取消.那种方法不适合我想要完成的任务.
我订阅设置为autorenew.我需要cancel
创建一个仅current_user
在结算周期结束时标记为已取消的操作.
例如:
用户A在9月27日报名参加月度订阅.用户A决定在12月15日取消订阅.用户A的订阅还剩12天.用户A单击该cancel
链接.用户A在其PayPal或Stripe帐户中已取消自动更新和订阅.在我的数据库中,他们的cancelled
属性值在12天(12月27日)结束之前不会改变.
如果有人可以提供帮助那就太棒了.
订阅控制器:
def new plan = Plan.find(params[:plan_id]) @subscription = plan.subscriptions.build render layout: 'new_application' if params[:PayerID] @subscription.paypal_customer_token = params[:PayerID] @subscription.paypal_payment_token = params[:token] @subscription.email = @subscription.paypal.checkout_details.email end end def create @subscription = Subscription.new(params[:subscription]) if @subscription.save_with_payment redirect_to @subscription, :notice => "Thank you for subscribing!" else render :new end end def show @subscription = Subscription.find(params[:id]) render layout: 'new_application' end def paypal_checkout plan = Plan.find(params[:plan_id]) subscription = plan.subscriptions.build redirect_to subscription.paypal.checkout_url( return_url: new_subscription_url(:plan_id => plan.id), cancel_url: root_url ) end def updatesubscription @user = current_user @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token) if @user.subscription.plan_id == 12 @customer.update_subscription(:plan => "1", :prorate => true) current_user.subscription.update_attributes(:plan_id => 1) flash.alert = 'Your subscription has been changed to monthly!' redirect_to root_url elsif @user.subscription.plan_id == 1 @customer.update_subscription(:plan => "12", :prorate => true) current_user.subscription.update_attributes(:plan_id => 12) current_user.save! flash.alert = 'Your subscription has been changed to annually!' redirect_to root_url end end def cancelsubscription @user = current_user @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token) @customer.cancel_subscription(:at_period_end => true) current_user.subscription.update_attributes(:cancelled => 1) current_user.save! flash.alert = 'Your subscription has been cancelled successfully!' redirect_to root_url end def showcard @user = current_user Stripe::Customer.retrieve(@user.subscription.stripe_customer_token).cards.all() end def suspend @user = current_user @user.subscription.suspend_paypal current_user.subscription.update_attributes(:cancelled => 1) flash.alert = 'Billing has been suspended!' redirect_to root_url end def reactivate @user = current_user @user.subscription.reactivate_paypal current_user.subscription.update_attributes(:cancelled => nil) flash.alert = 'Billing has been activated!' redirect_to root_url end def edit_card @user = current_user end def update_card @user = current_user card_info = { name: params[:name], number: params[:number], exp_month: params[:date][:month], exp_year: params[:date][:year], cvc: params[:cvc] } if @user.subscription.update_card(@subscriber, card_info) flash.alert = 'Saved. Your card information has been updated.' redirect_to root_url else flash.alert = 'Stripe reported an error while updating your card. Please try again.' redirect_to root_url end end end
Kimooz.. 8
我认为最简单的方法是使用条带webhooks,一旦订阅结束条带将使用customer.subscription.deleted事件ping您的系统,此时您应该取消用户订阅.
设置非常简单.
您只需转到设置页面并添加webhook网址,然后条带将开始向您发送系统事件.
然后创建一个条带事件控制器,它将解析后端中的条带事件.
使用at_period_end params true检测用户并取消订阅
customer = Stripe::Customer.retrieve("cus_3R1W8PG2DmsmM9") customer.subscriptions.retrieve("sub_3R3PlB2YlJe84a").delete(:at_period_end => true
如果您在结算周期结束时取消订阅,则会立即触发customer.subscription.updated事件,反映订阅的cancel_at_period_end值的更改.当订阅在期末实际取消时,将发生customer.subscription.deleted事件.Stripe Doc
4-然后设置回调控制器并检测订阅已删除
class StripeEventsController skip_before_filter :verify_authenticity_token def catch object = params[:data][:object] case params[:type] when "customer.subscription.deleted" customer = object[:id] user = User.find_by_stripe_id(customer) user.subscription.update_attributes(cancelled: "1") end end结束
我认为最简单的方法是使用条带webhooks,一旦订阅结束条带将使用customer.subscription.deleted事件ping您的系统,此时您应该取消用户订阅.
设置非常简单.
您只需转到设置页面并添加webhook网址,然后条带将开始向您发送系统事件.
然后创建一个条带事件控制器,它将解析后端中的条带事件.
使用at_period_end params true检测用户并取消订阅
customer = Stripe::Customer.retrieve("cus_3R1W8PG2DmsmM9") customer.subscriptions.retrieve("sub_3R3PlB2YlJe84a").delete(:at_period_end => true
如果您在结算周期结束时取消订阅,则会立即触发customer.subscription.updated事件,反映订阅的cancel_at_period_end值的更改.当订阅在期末实际取消时,将发生customer.subscription.deleted事件.Stripe Doc
4-然后设置回调控制器并检测订阅已删除
<pre> class StripeEventsController skip_before_filter :verify_authenticity_token def catch object = params[:data][:object] case params[:type] when "customer.subscription.deleted" customer = object[:id] user = User.find_by_stripe_id(customer) user.subscription.update_attributes(cancelled: "1") end end
结束