[英]How to call in the view for User to cancel Stripe subscription
I am new to Rails and trying to figure out how to setup the view for user to cancel a subscription or update (to a higher or lower plan). 我是Rails的新手,他试图弄清楚如何为用户设置视图以取消订阅或更新(以较高或较低的计划)。 I added code to the subscription controller, but I am not understanding how to call this in the view so it can take action for switching from plan 1 to plan 12 (monthly to yearly) and to cancel the subscription altogether. 我向预订控制器添加了代码,但是我不理解如何在视图中调用此代码,因此它可以采取措施从计划1切换到计划12(每月改为每年)并完全取消订阅。
The code in the controller should allow the user to cancel their subscription or change the plan. 控制器中的代码应允许用户取消其订阅或更改计划。 Need help with creating the view action for it. 在为其创建视图操作时需要帮助。
Subscriptions Controller: 订阅控制器:
def new
plan = Plan.find(params[:plan_id])
@subscription = plan.subscriptions.build
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])
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 update_subscription
@customer = Stripe::Customer.retrieve(@user.stripe_customer_token)
@subscription = customer.subscriptions.retrieve(@user.plan_id)
@subscription.plan = params[:plan_id]
subscription.save
end
def cancel_subscription
@customer = Stripe::Customer.retrieve(@user.stripe_customer_token)
@customer.subscriptions.retrieve(@user.plan_id).delete()
end
end
Routes: 路线:
resources :charges
resources :subscriptions
resources :plans
get 'paypal/checkout', to: 'subscriptions#paypal_checkout'
Subscription Model: 订阅模式:
belongs_to :plan
belongs_to :subscription
belongs_to :user
validates_presence_of :plan_id
validates_presence_of :email
attr_accessor :stripe_card_token, :paypal_payment_token
def save_with_payment
if valid?
if paypal_payment_token.present?
save_with_paypal_payment
else
save_with_stripe_payment
end
end
end
def paypal
PaypalPayment.new(self)
end
def save_with_paypal_payment
response = paypal.make_recurring
self.paypal_recurring_profile_token = response.profile_id
save!
end
def save_with_stripe_payment
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
def payment_provided?
stripe_card_token.present? || paypal_payment_token.present?
end
end
You would need to add to routes: 您将需要添加到路线:
get "subscriptions/cancelsubscription"
Then in your view, <%= link_to "Cancel my subscription", subscriptions_cancelsubscription_path, :data => { :confirm => "Are you sure?" } %>
然后在您看来, <%= link_to "Cancel my subscription", subscriptions_cancelsubscription_path, :data => { :confirm => "Are you sure?" } %>
<%= link_to "Cancel my subscription", subscriptions_cancelsubscription_path, :data => { :confirm => "Are you sure?" } %>
您可以将这些功能移到控制器上,并通过自定义路由进行调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.