问题描述
我将 Stripe 用于我的订阅,并且我已将其设置为当用户取消订阅(以关闭自动续订)时,它将保持订阅有效,直到 Stripe 的计费周期结束.
I am using Stripe for my subscriptions and I have it set so when a user cancels their subscription (to turn off automatic renewal) it will keep the subscription active until the end of the billing period on Stripe.
该操作通过 Stripe 起作用,但我如何设置以使我的数据库中的 cancelled
列具有相同的效果?目前,如果用户点击取消订阅链接,它会将他们的 cancelled
列标记为 1
.我希望它在计费周期结束之前不会被标记为已取消,以便用户可以继续访问该网站,直到他们的最后一个计费日(我已开启自动续订)
The action works via Stripe, but how can I setup so that the cancelled
column in my database takes the same affect? Currently if the user clicks on the cancel subscription link it will mark their cancelled
column to 1
. I would like for it to not mark as cancelled until the end of their billing period so the user can continue have access to the website until their final billing day (I have autorenwal turned on)
我已阅读 txdavidtx 建议.他的建议是将所有用户在计费周期结束时标记为已取消.这种方法不适合我想要完成的工作.
I have read txdavidtx suggestion. What he suggests would mark all Users as cancelled at the end of their billing period. That method would not fit with what I am looking to accomplish.
我已将订阅设置为自动续订.我需要创建一个 cancel
操作,该操作只会将 current_user
标记为在其计费周期结束时取消.
I have subscriptions set to autorenew. I would need a cancel
action created that would only mark the current_user
as cancelled at the end of their billing period.
例如:
用户 A 于 9 月 27 日注册了月度订阅.用户 A 决定在 12 月 15 日取消订阅.用户 A 的订阅期还有 12 天.用户 A 单击 cancel
链接.用户 A 已在其 PayPal 或 Stripe 帐户中自动续订并取消订阅.在我的数据库中,他们的 cancelled
属性值在这 12 天结束之前(12 月 27 日)不会改变.
User A signs up for the monthly subscription on September 27. User A decides on December 15 they want to cancel their subscription. User A still has 12 days left in their subscription. User A clicks on the cancel
link. User A has autorenew and subscription cancelled in their PayPal or Stripe account. Inside my database their cancelled
attribute value will not change until those 12 days have finished (December 27).
如果有人可以提供帮助那就太好了.
If someone can assist that would be great.
订阅控制器:
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
推荐答案
我认为最简单的方法是使用stripe webhooks,一旦订阅结束,stripe 将使用 customer.subscription.deleted 事件 ping 您的系统,此时您应该只需取消用户订阅即可.
I think the easiest way is to use stripe webhooks, where as soon as the subscription ends stripe will ping your system with a customer.subscription.deleted event and at that point you should just cancel the user subscription.
设置非常简单.
- 您只需转到您的设置页面并添加一个 webhook 网址,然后 Stripe 就会开始向您发送系统事件.
- 然后创建一个条带事件控制器,它将在后端解析条带事件.
使用 at_period_end params true 检测用户并取消他的订阅
- You just go to your settings page and add a webhook url and then stripe will start sending you system events.
- Then Create a stripe events controller which will parse stripe events in the backend.
Detect the user and cancel his subscription with at_period_end params true
如果您在计费周期结束时取消订阅,则会立即触发 customer.subscription.updated 事件,反映订阅的 cancel_at_period_end 值的变化.当订阅在周期结束时实际取消时,会发生 customer.subscription.deleted 事件.条纹文档
4- 然后设置您的回调控制器并检测订阅已删除
A customer.subscription.updated event is immediately triggered if you cancel a subscription at the end of the billing period instead, reflecting the change in the subscription’s cancel_at_period_end value. When the subscription is actually canceled at the end of the period, a customer.subscription.deleted event will occur. Stripe Doc
4- Then Setup your callback controller and detect subscription deleted
<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
结束
这篇关于在计费周期后将用户订阅显示为已取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!