簡體   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