Rails PayPal链式支付费用

时间:2014-05-14 19:19:36

标签: ruby-on-rails ruby paypal paypal-adaptive-payments chained-payments

我在rails app上有一个红宝石;我希望有一个用户能够支付另一个用户,减少10%的“佣金”给应用程序;我的客户希望费用来自应用程序保留的10%,原因有两个:1)没有镍/他们的客户需求2)经过一定数量的交易(每月)后,百分比显然会降低

因此,例如,如果用户1向用户2支付100美元,我希望它显示为:

用户1向应用发送100美元 - >应用程序收到97.09美元(减去100美元的费用) - > 应用程序向用户2发送90.00(90%) - >用户2收到全部90美元(他不收费)

然而,尽管将应用程序设置为主要接收方,但它在次要接收方上发布了费用,使用户2支付费用。我还尝试将用户2设置为主要用户,之后只向用户发送10%,但随后它将费用转移到主要接收者。我在代码中唯一更改的是收费百分比和主要/次要电子邮件。我的代码如下所示:

<!-- app/lib/pay_pal_api.rb -->
require "pp-adaptive"

class PayPalAPI

def self.payment_url(submission)
  amount = submission.amount_in_cents.to_f / 100.0
  recipient_cut = amount * 0.9
  recipient_email = submission.submitter.paypal_email

  client.execute(:Pay,
    :action_type     => "PAY",
    :currency_code   => "USD",
    :cancel_url      => "http://localhost:3000/my-studio",
    :return_url      => "http://localhost:3000/submissions/#{submission.id}",
    :receivers       => [
      { :email => recipient_email, :amount => recipient_cut, :primary => false },
      { :email => "TestApp@gmail.com", :amount => amount, :primary => true }
    ]
  ) do |response|

    if response.success?
      puts "Pay key: #{response.pay_key}"

      # send the user to PayPal to make the payment
      # e.g. https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=abc
      return client.payment_url(response)
    else
      puts "#{response.ack_code}: #{response.error_message}"
    end

  end
  return nil
end

2 个答案:

答案 0 :(得分:2)

我发现在pp-adaptive gem中,它需要这种语法:

:fees_payer       => "PRIMARYRECEIVER",

现在它工作得很好。

答案 1 :(得分:1)

使用feesPayer字段并将其设置为PRIMARYRECEIVERSECONDARYONLY,具体取决于谁先收到付款。 ruby SDK版本为fees_payer - 来自API Reference

feesPayer   xs:string (Optional) The payer of PayPal fees. Allowable values are: 
        SENDER – Sender pays all fees (for personal, implicit simple/parallel payments; do not use for chained or unilateral payments)
        PRIMARYRECEIVER – Primary receiver pays all fees (chained payments only)
        EACHRECEIVER – Each receiver pays their own fee (default, personal and unilateral payments)
        SECONDARYONLY – Secondary receivers pay all fees (use only for chained payments with one secondary receiver)

EG:

client.execute(:Pay,
    :action_type     => "PAY",
    :currency_code   => "USD",
    :cancel_url      => "http://localhost:3000/my-studio",
    :return_url      => "http://localhost:3000/submissions/#{submission.id}",
    :fees_payer      => "SECONDARYONLY",
    :receivers       => [
      { :email => recipient_email, :amount => recipient_cut, :primary => false },
      { :email => "TestApp@gmail.com", :amount => amount, :primary => true }
    ]
  )