gpt4 book ai didi

ruby-on-rails - 在结算期后显示用户订阅已取消

转载 作者:行者123 更新时间:2023-12-05 08:58:13 26 4
gpt4 key购买 nike

我正在使用 Stripe 进行订阅,我已将其设置为当用户取消订阅(以关闭自动续订)时,它将保持订阅有效,直到 Stripe 的计费期结束。

该操作通过 Stripe 起作用,但我如何设置才能使数据库中的 cancelled 列产生相同的效果?目前,如果用户点击取消订阅链接,它将把他们的 cancelled 列标记为 1。我希望它在计费期结束之前不标记为已取消,以便用户可以继续访问该网站,直到他们的最后一个计费日(我打开了 autorenwal)

我已经阅读了 txdavidtx 的建议。他的建议是将所有用户在计费期结束时标记为已取消。该方法不适合我要完成的任务。

我将订阅设置为自动续订。我需要创建一个 cancel 操作,该操作只会将 current_user 标记为在其计费周期结束时已取消。

例如:

用户 A 在 9 月 27 日注册了每月订阅。用户 A 在 12 月 15 日决定取消订阅。用户 A 的订阅还剩 12 天。用户 A 单击 cancel 链接。用户 A 在他们的 PayPal 或 Stripe 账户中取消了自动续订和订阅。在我的数据库中,它们的 cancelled 属性值在这 12 天结束(12 月 27 日)之前不会更改。

如果有人可以提供帮助,那就太好了。

订阅 Controller :

  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 ,一旦订阅结束,条纹就会用 customer.subscription.deleted ping 您的系统。事件,此时您应该取消用户订阅。

设置非常简单。

  1. 您只需转到您的设置页面并添加一个 webhook url,然后 stripe 就会开始向您发送系统事件。
  2. 然后创建一个 strip 事件 Controller ,它将在后端解析 strip 事件。
  3. 检测用户并使用 at_period_end 参数 true 取消他的订阅

    customer = Stripe::Customer.retrieve("cus_3R1W8PG2DmsmM9")customer.subscriptions.retrieve("sub_3R3PlB2YlJe84a").delete(:at_period_end => true

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

结束

关于ruby-on-rails - 在结算期后显示用户订阅已取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24894841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com