ruby-on-rails - 用户的未定义方法 'suspend_paypal'

标签 ruby-on-rails paypal paypal-rest-sdk paypal-subscriptions recurring-billing

我正在使用 paypal-recurring我的应用程序的 gem,我不知道如何取消用户的订阅。我试过加布里埃尔的answer来自此处的类似问题,但我在尝试取消订阅时在 SubscriptionsController#suspend undefined method 'suspend_paypal' for User` 中得到了一个NoMethodError。

在我的 subscriptions.rb 模型中

class Subscription < ApplicationRecord

  belongs_to :plan
  belongs_to :user
  validates_presence_of :plan_id
  validates_presence_of :user_id
  attr_accessor :paypal_payment_token

def save_with_payment
  if valid?
    if paypal_payment_token.present?
      save_with_paypal_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 suspend_paypal
  paypal.suspend
  self.status = "canceled"
  save
end



def payment_provided?
paypal_payment_token.present?
end
end

订阅 Controller

    class SubscriptionsController < ApplicationController
      before_action :authenticate_user! , only: [:new,  :edit]

    def new
      plan = Plan.find(params[:plan_id])
      @subscription = plan.subscriptions.build
      @subscription.user_id = current_user.id
      if params[:PayerID]
        @subscription.paypal_cutomer_token = params[:PayerID]
        @subscription.paypal_payment_token = params[:token]
        @subscription.email = @subscription.paypal.checkout_details.email
      end
    end

    def index
    end



    def create

      @user = current_user

        @subscription = Subscription.new(subscription_params)
      if @subscription.save_with_payment
        redirect_to root_path, :notice => "Thank you for subscribing"
        @user.premium!
      else
        render :new
      end
    end


    def suspend
     @user = current_user
     @user.suspend_paypal
     flash[:success] = 'Subscription Canceled.'
     @user.free!
     redirect_to root_path
    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


    private

    def subscription_params
      params.require(:subscription).permit!
    end

end

paypal_payment.rb

  class PaypalPayment
def initialize(subscription)
    @subscription = subscription
  end

def checkout_details
process :checkout_details
end


def payment_active?
  self.status = "active"
  end

  def suspend
       process :suspend, :profile_id => @subscription.paypal_recurring_profile_token
   end
def checkout_url(options)
  process(:checkout, options).checkout_url
end

def save_with_paypal_payment
  response = paypal.make_recurring
  self.paypal_recurring_profile_token = response.profile_id
  save!

end



def make_recurring
  process :request_payment
  process :create_recurring_profile, period: :monthly, frequency: 1, start_at: Time.zone.now
end

private

def process(action, options = {})
  options = options.reverse_merge(
  token: @subscription.paypal_payment_token,
  payer_id: @subscription.paypal_cutomer_token,
  description: @subscription.plan.name,
  amount: @subscription.plan.price,
  currency: "USD"



  )
  response = PayPal::Recurring.new(options).send(action)
  raise response.errors.inspect if response.errors.present?
  response


end

end

查看

<%= link_to "Suspend paypal", subscriptions_suspend_path, :data => { :confirm => "Are you sure?" } %>

最佳答案

您已经在 Subscription 模型上定义了 suspend_paypal,但您正试图对用户调用它。改为获取订阅并调用方法:

# class SubscriptionsController

def suspend
   @subscription = Subscription.find(params[:id])
   @subscription.suspend_paypal
   flash[:success] = 'Subscription Canceled.'
   current_user.free!
   redirect_to root_path
end

注意——您应该添加授权逻辑,否则任何可以进行身份​​验证的用户都可以通过发送其 ID 来暂停任意订阅。如果用户 has_many 订阅,subscription = current_user.subscriptions.find(params[:id])。如果 has_one,只需 current_user.subscription 并验证它是否与 params[:id] 相匹配,以取得良好的效果。

您可能也不需要 subscription 成为 ivar,因为您只是重定向到 root。只需将 @subscription 替换为 subscription。当您需要在 View 中访问这些变量时使用 ivars。

关于ruby-on-rails - 用户的未定义方法 'suspend_paypal',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693111/

相关文章:

angularjs - 如何在 AngularJS 而不是 NodeJS 中处理来自 PayPal In-Context 的响应

javascript - 为什么 var 在 ruby​​ 循环中可以工作但 const 不能工作?

java - PayPal ExpressCheckout 实现 - 导入错误,缺少 jar 文件?

php - 如何在 paypal 快速结帐 php 中从沙盒模式继续直播?

php - PayPal PHP SDK - 不返回 GET 变量 'success'

php - Laravel Paypal - 从网站传递送货地址

java - 用于 Java 桌面 MVC 的 Rails

ruby-on-rails - ruby on rails syntax-变量后的: come before v.什么时候出现?

ruby-on-rails - 如何使用 Rails 3 停止 <%= strings %> 自动编码

ActiveMerchant PaypalExpress 网关 - 设置谁支付费用