gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:15 25 4
gpt4 key购买 nike

我正在使用 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/

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