繁体   English   中英

如何在视图中调用用户以取消Stripe订阅

[英]How to call in the view for User to cancel Stripe subscription

我是Rails的新手,他试图弄清楚如何为用户设置视图以取消订阅或更新(以较高或较低的计划)。 我向预订控制器添加了代码,但是我不理解如何在视图中调用此代码,因此它可以采取措施从计划1切换到计划12(每月改为每年)并完全取消订阅。

控制器中的代码应允许用户取消其订阅或更改计划。 在为其创建视图操作时需要帮助。

订阅控制器:

  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

路线:

 resources :charges
  resources :subscriptions
  resources :plans
  get 'paypal/checkout', to: 'subscriptions#paypal_checkout'

订阅模式:

  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

您将需要添加到路线:

get "subscriptions/cancelsubscription"

然后在您看来, <%= 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM