使用ActiveMerchant自定义Paypal Express的评论页面
我正在使用ActiveMerchant让我的rails应用程序访问Paypal的Express Checkout。 我想在评论页面中包含订单详细信息,如下所述: https : //cms.paypal.com/us/cgi-bin/?cmd = __render-content&content_ID = developer / e_howto_api_ECCustomizing
可以这样做吗?
目前,我的控制器代码如下所示:
def paypal #currently, options is unused, I'm not sure where to send this info options = { :L_NAME0=>"Tickets", :L_QTY0=>@payment.quantity, :L_DESC0=>"Tickets for #{@payment.event_name}", :L_AMT0=>@payment.unit_price } #the actual code that gets used setup_response = gateway.setup_purchase(@payment.amount, :ip=> request.remote_ip, :return_url=> url_for(:action=>:confirm, :id=>@payment.id, :only_path=>false), :cancel_return_url => url_for(:action=>:show, :id=>@payment.id, :only_path=>false) ) redirect_to gateway.redirect_url_for(setup_response.token) end
如果我想做的事情是可能的,我需要改变什么?
@Soleone我尝试你的解决方案,但不适合我。
xml.tag! 'n2:OrderDescription', options[:description] xml.tag! 'n2:Name', options[:name] xml.tag! 'n2:Description', options[:desc] xml.tag! 'n2:Amount', options[:amount] xml.tag! 'n2:Quantity', options[:quantity]
我认为xml结构不正确,订单项是多个,所以应该这样
xml.tag! 'n2:OrderItems' do xml.tag! 'n2:OrderItem' do xml.tag! 'n2:Name', options[:name] xml.tag! 'n2:Description', options[:desc] xml.tag! 'n2:Amount', options[:amount] xml.tag! 'n2:Quantity', options[:quantity] end end
但我真的不知道正确的结构,现在正在寻找。
====更新
我找到了SOAP api doc, https: //cms.paypal.com/us/cgi-bin/?cmd = __render-content & content_ID = developer_e_howto_api_soap_r_SetExpressCheckout#id09BHC0QF07Q
xml.tag! 'n2:PaymentDetails' do xml.tag! 'n2:PaymentDetailsItem' do xml.tag! 'n2:Name', options[:name] xml.tag! 'n2:Description', options[:desc] xml.tag! 'n2:Amount', options[:amount] xml.tag! 'n2:Quantity', options[:quantity] end end
但也行不通,谁能帮忙?
=====更新====
我尝试了添加PaymentDetails参数的方法,但似乎仍然不起作用,我找到了SetExpressCheckoutReq xml的架构, http://www.visualschema.com/vs/paypal/SetExpressCheckoutReq/ ,没有PaymentDetails的定义,谁这样做了之前的东西,希望得到你的帮助。
====== FINAL ========
我已经修复了这个问题,新版本的ActiveMerchant支持订单详情审查,而mwagg推出了关于此的补丁,你们可以使用这个版本https://github.com/mwagg/active_merchant
确保您的1.12.0
版本不低于1.12.0
。
EXPRESS_GATEWAY.setup_purchase(220, :items => [{:name =>“Tickets”,:quantity => 22,:description =>“232323的门票”,:amount => 10}], :return_url =>'example.com', :cancel_return_url =>'example.com' )
希望这可以帮助 :)
您可以在此表中看到可用参数(只有中间列适用于activemerchant使用SOAP API):
为了更好地理解activemerchant的作用,可能直接查看实现。 您可以看到相关参数插入到SOAP XML请求(当前)中,从插入OrderTotal
第98行开始:
请注意如何从options
散列中获取参数,以便您可以在此处查看要为每个参数传递的正确符号。
在您列出以下参数的情况下,您可以这样做:
def paypal options = { :name => "Tickets", :quantity => @payment.quantity, :description => "Tickets for #{@payment.event_name}", :amount => @payment.unit_price :ip => request.remote_ip, :return_url => url_for(:action=>:confirm, :id=>@payment.id, :only_path=>false), :cancel_return_url => url_for(:action=>:show, :id=>@payment.id, :only_path=>false) } # the actual code that gets used setup_response = gateway.setup_purchase(@payment.amount, options) redirect_to gateway.redirect_url_for(setup_response.token) end
但请注意:activemerchant目前不支持name
, quantity
和amount
字段。 您必须分叉存储库并自行插入并使用您的项目副本。 当你查看代码并看看它是如何与其他代码完成时,它真的非常简单。
例如,要添加订单名称,物料数量和物料单价,您可以在插入OrderDescription
后放置这些行:
xml.tag! 'n2:Name', options[:name] xml.tag! 'n2:Amount', options[:amount] xml.tag! 'n2:Quantity', options[:quantity]
希望有所帮助!
更新:
好吧,我认为根据SOAP API的XML Schema看起来你必须在activemerchant中指定它:
xml.tag! 'n2:PaymentDetails' do items = options[:items] || [] items.each do |item| xml.tag! 'n2:PaymentDetailsItem' do xml.tag! 'n2:Name', item[:name] xml.tag! 'n2:Description', item[:desc] xml.tag! 'n2:Amount', item[:amount] xml.tag! 'n2:Quantity', item[:quantity] end end end
你会在你的Rails应用程序中传递所有项目,如下所示:
options = { :items => [ { :name => "Tickets", :quantity => @payment.quantity, :description => "Tickets for #{@payment.event_name}", :amount => @payment.unit_price }, { :name => "Other product", :quantity => @other_payment.quantity, :description => "Something else for #{@other_payment.event_name}", :amount => @other_payment.unit_price } ] :ip => request.remote_ip, :return_url => url_for(:action=>:confirm, :id=>@payment.id, :only_path=>false), :cancel_return_url => url_for(:action=>:show, :id=>@payment.id, :only_path=>false) }
希望工作更好,祝你好运!
我也有问题让这个工作。 解决方案是所有项目的金额总和必须是订单的小计,其中小计,运费,处理和税收必须总计到订单的总价值。 我的paypal控制器看起来像这样:
def begin_paypal # ... options = express_options(@order) # ... response = EXPRESS_GATEWAY.setup_purchase(@order.gross_price_in_cent, options) redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token) end private def express_options order options = {} options[:ip] = request.remote_ip options[:order_id] = order.bearbeitungsnummer # subtotal, shipping, handling and tax must sum up to the orders total value # subtotal must be the sum of all amounts of all items options[:subtotal] = order.gross_price_in_cent options[:shipping] = 0 options[:handling] = 0 options[:tax] = 0 options[:items] = order.line_items.map do |line_item| { :name => line_item.product.name, :number => line_item.product.kcode, :quantity => line_item.quantity, :description => line_item.product.beschreibung, :amount => line_item.gross_price_in_cent, :url => nil } end # ... end
工作良好