![](/img/trans.png)
[英]Add multiplie receivers to Paypal Mass Pay using Rails and 'paypal-sdk-merchant' gem
[英]Rails passing object to class in modules directory paypal-sdk-merchant
我遵循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年1月7日01:33:46 -0600開始為127.0.0.1的GET“ / orders / 1”,由OrdersController#show處理為HTML參數:{“ id” =>“ 1”}訂單加載(0.2ms)在“訂單”中選擇“訂單”。*在“訂單”中。“ id” =? LIMIT 1 [[“ id”,“ 1”]]在18毫秒內完成500個內部服務器錯誤
NoMethodError( total' for nil:NilClass): lib/modules/paypal_interface.rb:25:in
未定義方法total' for nil:NilClass): lib/modules/paypal_interface.rb:25:in
in'lib / modules / paypal_interface.rb:3:in <top (required)>' app/controllers/orders_controller.rb:17:in
表演中
schema.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的值加載到表單中;並且,我嘗試將total字段更改為:decimal無效,但是由於對象沒有從訂單控制器傳遞到模塊,現在我被卡住了。
如果PaypalInterface中的@question為nil,則Order.find(params[:id])
可能找不到任何內容。 在show方法中:
@order = Order.find(params[:id])
@order為零? 您可以嘗試:
def show
if @order = Order.find(params[:id])
[...]
else
# Can't find order
end
end
我使用Pry調試此類錯誤,只需要“ pry”,然后將bind.pry放在要停止代碼並打開控制台的位置即可。 一個例子是:
def show
@order = Order.find(params[:id])
require 'pry'
binding.pry # Go to the console and start debugging @order
[...]
end
PS。 您所指示的paypal_interface.rb只是該文件的一部分? 因為該代碼似乎是錯誤的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.