gpt4 book ai didi

ruby-on-rails - Rails 将对象传递给模块目录 paypal-sdk-merchant 中的类

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:43 26 4
gpt4 key购买 nike

我正在按照 http://www.tommyblue.it/2013/07/03/paypal-express-checkout-with-ruby-on-rails-and-paypal-sdk-merchant 上的指南进行操作;并且,当我尝试将订单传递给 paypal_interface 时,我得到一个 nil 对象。

def show
@order = Order.find(params[:id])
@paypal = PaypalInterface.new(@order)
@paypal.express_checkout
if @paypal.express_checkout_response.success?
@paypal_url = @paypal.api.express_checkout_url(@paypal.express_checkout_response)
else
# manage error
end
end

编辑以包含整个文件

paypal_interface.rb

require 'paypal-sdk-merchant'

class PaypalInterface
attr_reader :api, :express_checkout_response

PAYPAL_RETURN_URL = Rails.application.routes.url_helpers.paid_orders_url(host: HOST_WO_HTTP)
PAYPAL_CANCEL_URL = Rails.application.routes.url_helpers.revoked_orders_url(host: HOST_WO_HTTP)
PAYPAL_NOTIFY_URL = Rails.application.routes.url_helpers.ipn_orders_url(host: HOST_WO_HTTP)

def initialize(order)
@api = PayPal::SDK::Merchant::API.new
@order = order
end

@set_express_checkout = @api.build_set_express_checkout({
SetExpressCheckoutRequestDetails: {
ReturnURL: PAYPAL_RETURN_URL,
CancelURL: PAYPAL_CANCEL_URL,
PaymentDetails: [{
NotifyURL: PAYPAL_NOTIFY_URL,
OrderTotal: {
currencyID: "EUR",
value: @order.total
},
ItemTotal: {
currencyID: "EUR",
value: @order.total
},
ShippingTotal: {
currencyID: "EUR",
value: "0"
},
TaxTotal: {
currencyID: "EUR",
value: "0"
},
PaymentDetailsItem: [{
Name: @order.code,
Quantity: 1,
Amount: {
currencyID: "EUR",
value: @order.total
},
ItemCategory: "Physical"
}],
PaymentAction: "Sale"
}]
}
})
# Make API call & get response
@express_checkout_response = @api.set_express_checkout(@set_express_checkout)

# Access Response
if @express_checkout_response.success?
@order.set_payment_token(@express_checkout_response.Token)
else
@express_checkout_response.Errors
end


def do_express_checkout
@do_express_checkout_payment = @api.build_do_express_checkout_payment({
DoExpressCheckoutPaymentRequestDetails: {
PaymentAction: "Sale",
Token: @order.payment_token,
PayerID: @order.payerID,
PaymentDetails: [{
OrderTotal: {
currencyID: "EUR",
value: @order.total
},
NotifyURL: PAYPAL_NOTIFY_URL
}]
}
})

# Make API call & get response
@do_express_checkout_payment_response = @api.do_express_checkout_payment(@do_express_checkout_payment)

# Access Response
if @do_express_checkout_payment_response.success?
details = @do_express_checkout_payment_response.DoExpressCheckoutPaymentResponseDetails
@order.set_payment_details(prepare_express_checkout_response(details))
else
errors = @do_express_checkout_payment_response.Errors # => Array
@order.save_payment_errors errors
end
end
end

该类在/lib/modules 中,加载时实例变量为nil

在 2014-01-07 01:33:46 -0600 开始为 127.0.0.1 获取“/orders/1”由 OrdersController 处理#show as HTML 参数:{“id”=>“1”} 订单加载 (0.2ms) SELECT "orders".* FROM "orders"WHERE "orders"."id"= ?限制 1 [["id", "1"]]在 18 毫秒内完成 500 个内部服务器错误

NoMethodError(未定义方法 total' for nil:NilClass):
lib/modules/paypal_interface.rb:25:in
' lib/modules/paypal_interface.rb:3:in <top (required)>'
app/controllers/orders_controller.rb:17:in
显示'

架构.rb

ActiveRecord::Schema.define(:version => 20140107060318) do

create_table "orders", :force => true do |t|
t.text "code"
t.text "total"
t.text "payerID"
t.text "payment_token"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end

我将值 10 加载到表单中;并且,我尝试将总字段更改为 :decimal 但无济于事,但现在我被卡住了,因为对象没有从订单 Controller 传递到模块。

最佳答案

如果 PaypalInterface 中的 @question 为 nil,可能 Order.find(params[:id]) 没有找到。在显示方法中:

@order = Order.find(params[:id]) 

@order 为零?你可以试试:

def show
if @order = Order.find(params[:id])
[...]
else
# Can't find order
end
end

我使用 Pry 来调试此类错误,只需要“pry”然后将 binding.pry 放在您要停止代码并打开控制台的位置。一个例子是:

def show
@order = Order.find(params[:id])
require 'pry'
binding.pry # Go to the console and start debugging @order
[...]
end

附言。您指定的 paypal_interface.rb 只是该文件的一部分?因为该代码似乎是错误的。

关于ruby-on-rails - Rails 将对象传递给模块目录 paypal-sdk-merchant 中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20976071/

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