【问题标题】:Customizing Paypal Express's Review Page using ActiveMerchant使用 ActiveMerchant 自定义 Paypal Express 的评论页面
【发布时间】:2011-04-23 08:18:57
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails paypal activemerchant


    【解决方案1】:

    您可以在此表中看到可用参数(仅中间列适用,因为 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 请求中(当前),从插入 OrderTotal 的第 98 行开始:

    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
    

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

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

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

    【讨论】:

    • 这个解决方案对我不起作用。我已经添加了您在代码 sn-p 中描述的项目,我从贝宝收到了无效的交易错误。
    【解决方案2】:

    @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

    【讨论】:

      【解决方案3】:

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

      希望这会有所帮助:)

      【讨论】:

      • 在使用这个时,我被重定向到实时环境,而不是我想要的沙箱。没有:items 选项也可以正常工作并正确重定向到沙箱
      【解决方案4】:

      我也遇到了问题,无法使其正常工作。解决方案是所有物品的金额之和必须是订单的小计,其中小计、运费、手续费和税费的总和必须是订单的总价值。我的贝宝控制器如下所示:

      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
      

      工作正常

      【讨论】:

      • 我试过这个,但我被重定向到实时站点而不是贝宝上的沙箱。有什么想法可以解决这个问题吗?
      猜你喜欢
      • 2014-12-29
      • 2015-07-08
      • 2012-08-03
      • 2014-01-15
      • 2011-06-01
      • 2011-10-08
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      相关资源
      最近更新 更多