使用 ActiveMerchant 自定义 Paypal Express 的评论页面

发布于 2024-09-27 03:36:21 字数 1129 浏览 5 评论 0原文

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

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

I am using ActiveMerchant to give my rails app access to Paypal's Express Checkout.
I would like to include the Order Details on the Review Page as described here: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_ECCustomizing

Can this be done?

Currently, my controller code looks like this:

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

If what I'm trying to do is possible, what do I need to change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如日中天 2024-10-04 03:36:21

确保您的 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'
)

希望这有帮助:)

Make sure you have activemerchant version not less than 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'
)

Hope this helps :)

舞袖。长 2024-10-04 03:36:21

@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

@Soleone
I try your solution,but don't work for me.

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]

I think the xml structure is not right,the order items is multiple,so should like this

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

But really I don't know the correct structure,looking for now.

====Update

I found the 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

But also doesn't work,who can help?

=====UPDATE====

I tried the method of adding PaymentDetails parameter,but seems still not work,I found the schema of SetExpressCheckoutReq xml, http://www.visualschema.com/vs/paypal/SetExpressCheckoutReq/ , there is no definition of PaymentDetails,who did this stuff before,hope for your help.

======FINAL========

I have fixed this issue,new version of ActiveMerchant support the order details review,and mwagg pushed the patch about this,you guys can use this version https://github.com/mwagg/active_merchant

南街女流氓 2024-10-04 03:36:21

您可以在此表中查看可用参数(仅中间列适用,因为 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

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

例如,要添加订单名称、商品数量和商品单价,您可以在插入 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) 
}

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

You can see the available parameters in this table (only the middle column applies as activemerchant is using the SOAP API):

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

To best understand how activemerchant does it is probably to look directly into the implementation. You can see the relevant parameters getting inserted in the SOAP XML request (currently) starting at line 98 where the OrderTotal gets inserted:

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

Notice how the parameters are fetched from the options hash so you can see the correct symbol to pass for each one here.

In your case as you listed the following parameters, you would do it like this:

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

Note though:
The name, quantity and amount fields are currently not support in activemerchant. You would have to fork the repository and insert these yourself and use your copy of the project. It's really very straightforward when you look at the code and see how it is done with the other ones.

For example to add the order name, item quantity and item unit price you would put these lines after the OrderDescription gets inserted:

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

Hope that helps!

UPDATE:

Okay I think according to the XML Schema for the SOAP API it looks like you have to specify it like this in 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

And you would pass all your items in your Rails app like this:

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) 
}

Hope that works better, good luck!

浅暮の光 2024-10-04 03:36:21

我也遇到了让它发挥作用的问题。解决方案是所有商品的金额总和必须是订单的小计,其中小计、运费、手续费和税费的总和必须等于订单的总价值。我的贝宝控制器看起来像这样:

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

工作正常

I also had problems to get this to work. The solution is that the sum of the amount of all items must be the subtotal of the order, where the subtotal, shipping, handling and tax must sum up to the total value of the order. My paypal controller looks like this:

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

Works fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文