6

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

如果我想做的事情是可能的,我需要改变什么?

4

4 回答 4

11

确保您的activemerchant版本不低于1.12.0.

EXPRESS_GATEWAY.setup_purchase(220,
  :items => [{:name => "Tickets", :quantity => 22,:description => "Tickets for 232323", :amount => 10}],
  :return_url => 'example.com',
  :cancel_return_url => 'example.com'
)

希望这可以帮助 :)

于 2011-04-25T06:33:27.667 回答
4

@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 文档,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的定义,谁做的之前的东西,希望对你有所帮助。

======最终========

我已经修复了这个问题,新版ActiveMerchant支持订单详情审核,mwagg推送了这个补丁,大家可以使用这个版本https://github.com/mwagg/active_merchant

于 2011-01-14T07:21:28.487 回答
3

您可以在此表中查看可用参数(仅中间列适用,因为 activemerchant 正在使用 SOAP API):

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_ECCustomizing#id086NA300I5Z__id086NAC0J0PN

为了最好地理解 activemerchant 是如何做的,它可能是直接查看实现。您可以看到相关参数被插入到 SOAP XML 请求中(当前),从插入的第 98 行开始OrderTotal

https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/gateways/paypal_express.rb#L98

请注意如何从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

name但请注意:activemerchant 目前不支持quantity和字段。amount您必须分叉存储库并自己插入这些并使用您的项目副本。当您查看代码并了解它是如何与其他代码一起完成时,这真的非常简单。

例如,要添加订单名称、商品数量和商品单价,您可以在OrderDescription插入后添加这些行:

  xml.tag! 'n2:Name', options[:name]
  xml.tag! 'n2:Amount', options[:amount]
  xml.tag! 'n2:Quantity', options[:quantity]

希望有帮助!

更新:

好的,我认为根据 SOAP API 的 XML 模式,您必须在 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) 
}

希望效果更好,祝你好运!

于 2011-01-14T02:03:57.050 回答
1

我也有问题要让它工作。解决方案是所有项目的金额之和必须是订单的小计,其中小计、运费、搬运和税金必须合计为订单的总价值。我的贝宝控制器如下所示:

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

工作正常

于 2011-11-15T12:56:57.427 回答