ruby-on-rails - Rails 中的 Paypal 自适应支付自动化流程

标签 ruby-on-rails paypal ruby-on-rails-4 paypal-adaptive-payments

我将 Paypal 集成到我的 Rails 网络应用程序中。我需要实现下一个用例:

  1. 客户购买服务并向我的应用付款。
  2. 几天后,例如一周后,我会在保留费用后向服务提供商付款。 这一点 2,我有一个引擎,它会自动检查支付客户的日期是否已经到来。如果是这样,它会尝试自动向提供商付款。

我有我的第 1 点,实现并使用 Express Checkout。 对于我的第 2 点,我知道我应该使用自适应支付 API。但是,它并没有像我理解的那样工作,所以我不确定。

我的第一个问题是,我希望一切都是自动化的,所以,我不希望弹出任何窗口来确认任何事情?是否可以自动执行操作?

我的代码:

def pay(amount, paypalAccountEmail)

  # ## Build request object
  @pay_request = @api.build_pay()

  # The action for this request. Possible values are:
  # 
  # * PAY - Use this option if you are not using the Pay request in
  # combination with ExecutePayment.
  # * CREATE - Use this option to set up the payment instructions with
  # SetPaymentOptions and then execute the payment at a later time with
  # the ExecutePayment.
  # * PAY_PRIMARY - For chained payments only, specify this value to delay
  # payments to the secondary receivers; only the payment to the primary
  # receiver is processed.
  @pay_request.actionType         = "PAY"

  # URL to redirect the sender's browser to after
  # canceling the approval for a payment; it is always required but only
  # used for payments that require approval (explicit payments)
  @pay_request.cancelUrl          = "https://paypal-sdk-samples.herokuapp.com/adaptive_payments/pay"

  # The code for the currency in which the payment is
  # made; you can specify only one currency, regardless of the number of
  # receivers
  @pay_request.currencyCode       = "EUR"

  # 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)
  @pay_request.feesPayer          = "SENDER"

  # The URL to which you want all IPN messages for this payment to be sent.
  @pay_request.ipnNotificationUrl = "https://paypal-sdk-samples.herokuapp.com/adaptive_payments/ipn_notify"

  # Amount to be paid to the receiver.
  @pay_request.receiverList.receiver[0].amount = amount

  # Receiver's email address. This address can be unregistered with paypal.com. If so, a receiver cannot claim the payment until a PayPal account is linked to the email address. The PayRequest must pass either an email address or a phone number.
  @pay_request.receiverList.receiver[0].email  = paypalAccountEmail

  # The URL to which the sender's browser is redirected after approving a payment on paypal.com. Specify the URL with the HTTP or HTTPS designator.
  @pay_request.returnUrl          = "https://paypal-sdk-samples.herokuapp.com/adaptive_payments/pay"

  # ## Make API call & get response
  @pay_response = @api.pay(@pay_request)

  # ## Access Response
  # ### Success Response

  if @pay_response.responseEnvelope.ack == "Success"
    # Once you get success response, user has to redirect to PayPal
    # for the payment. Construct redirectURL as follows,
    # `redirectURL=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey="
    # + @pay_response.payKey;`

    # The pay key, which is a token you use in other Adaptive
    # Payment APIs (such as the Refund Method) to identify this
    # payment. The pay key is valid for 3 hours; the payment must
    # be approved while the pay key is valid.
    puts  @pay_response.payKey

    # The status of the payment. Possible values are:
    # 
    # * CREATED - The payment request was received; funds will be transferred once the payment is approved
    # * COMPLETED - The payment was successful
    # * INCOMPLETE - Some transfers succeeded and some failed for a parallel payment or, for a delayed chained payment, secondary receivers have not been paid
    # * ERROR - The payment failed and all attempted transfers failed or all completed transfers were successfully reversed
    # * REVERSALERROR - One or more transfers failed when attempting to reverse a payment
    # * PROCESSING - The payment is in progress
    # * PENDING - The payment is awaiting processing
    puts "Pay Key : " + @pay_response.paymentExecStatus

    # ###Error Response
  else
    puts @pay_response.error[0].message

  end
  @pay_response
end

和我的日志:

providerPayment-->begin
Request[post]: https://svcs.sandbox.paypal.com/AdaptivePayments/Pay
Response[200]: OK, Duration: 1.417s
AP-1E607027P7722991V
Pay Key : CREATED
providerPayment-->ok

我从 Paypal 拿了一个例子,但是当我执行它时,我看到结果是好的。我的 actionType =“支付”。所以,我想付钱,但关键是,我得到了 = CREATED,之后什么也没有发生。我不想创建但要完成交易。我错过了什么?

最佳答案

我没有看到您将用户重定向到任何地方..??调用 Pay 后,您必须将用户重定向到 PayPal 以登录并验证付款。您唯一不会这样做的情况是,如果您在付款请求中包含预批准 key ,但我看不到您这样做。

您需要将获得的 paykey 附加到您重定向到的 PayPal URL 的末尾。这是一个示例:https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=AP-3VV02679XF235284R

在用户登录并完成支付之前,它确实会被创建。

我确实在您的代码注释中看到了一些关于重定向 URL 的信息,但我没有看到任何实际执行该重定向以将用户发送到那里的信息。

-----更新-----

现在我看到您正在进行隐式付款,您只需确保在请求中包含 senderEmail。这是我刚刚发出的付款请求的示例,它使用隐式批准,因此不需要重定向和进一步的身份验证。

<?xml version="1.0" encoding="utf-8"?>
<PayRequest xmlns="http://svcs.paypal.com/types/ap">
  <requestEnvelope xmlns="">
    <detailLevel>ReturnAll</detailLevel>
    <errorLanguage>en_US</errorLanguage>
  </requestEnvelope>
  <actionType xmlns="">PAY</actionType>
  <cancelUrl xmlns="">http://paypal.angelleye.com/paypal/class/1.2/Pay_Cancel.php</cancelUrl>
  <clientDetails xmlns="">
    <applicationId xmlns="">APP-80W284485P519543T</applicationId>
    <ipAddress xmlns="">192.168.1.115</ipAddress>
    <partnerName xmlns="">Always Give Back</partnerName>
  </clientDetails>
  <currencyCode xmlns="">USD</currencyCode>
  <receiverList xmlns="">
    <receiver xmlns="">
      <amount xmlns="">10.00</amount>
      <email xmlns="">sandbo_1204199080_biz@angelleye.com</email>
    </receiver>
    <receiver xmlns="">
      <amount xmlns="">5.00</amount>
      <email xmlns="">usb_1329725429_biz@angelleye.com</email>
    </receiver>
  </receiverList>
  <sender>
    <useCredentials xmlns=""></useCredentials>
  </sender>
  <account xmlns="">
    <phone xmlns=""></phone>
  </account>
  <returnUrl xmlns="">http://paypal.angelleye.com/paypal/class/1.2/Pay_Return.php</returnUrl>
  <senderEmail xmlns="">sandbo_1215254764_biz@angelleye.com</senderEmail>
</PayRequest>

这是响应,它显示 paymentExecStatus 已完全完成,而不是刚刚创建/待定,就像没有隐式批准一样。

<?xml version='1.0' encoding='UTF-8'?>
<ns2:PayResponse xmlns:ns2="http://svcs.paypal.com/types/ap">
  <responseEnvelope>
    <timestamp>2014-01-20T07:53:08.420-08:00</timestamp>
    <ack>Success</ack>
    <correlationId>c74cd2a669678</correlationId>
    <build>7935900</build>
  </responseEnvelope>
  <payKey>AP-39D64611TH198910V</payKey>
  <paymentExecStatus>COMPLETED</paymentExecStatus>
  <paymentInfoList>
    <paymentInfo>
      <transactionId>0M064165EV3552504</transactionId>
      <transactionStatus>COMPLETED</transactionStatus>
      <receiver>
        <amount>10.00</amount>
        <email>sandbo_1204199080_biz@angelleye.com</email>
        <primary>false</primary>
        <accountId>E7BTGVXBFSUAU</accountId>
      </receiver>
      <pendingRefund>false</pendingRefund>
      <senderTransactionId>43F26555RA073153C</senderTransactionId>
      <senderTransactionStatus>COMPLETED</senderTransactionStatus>
    </paymentInfo>
    <paymentInfo>
      <transactionId>7K9309808X980452U</transactionId>
      <transactionStatus>COMPLETED</transactionStatus>
      <receiver>
        <amount>5.00</amount>
        <email>usb_1329725429_biz@angelleye.com</email>
        <primary>false</primary>
        <accountId>C9TAVNJFATXCS</accountId>
      </receiver>
      <pendingRefund>false</pendingRefund>
      <senderTransactionId>0M822840XM282203C</senderTransactionId>
      <senderTransactionStatus>COMPLETED</senderTransactionStatus>
    </paymentInfo>
  </paymentInfoList>
  <sender>
    <accountId>ATSCG2QMC9KAU</accountId>
  </sender>
</ns2:PayResponse>

因此,无论您使用的是什么类库,您都应该能够轻松添加额外的 senderEmail 参数,这将解决您的问题。

关于ruby-on-rails - Rails 中的 Paypal 自适应支付自动化流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21236071/

相关文章:

ruby-on-rails - 表单提交按钮仅在重新加载后有效

ruby-on-rails - form_for 中 float 的自定义格式

ruby-on-rails - 使用 AASM 的 ActiveRecord 模型上的 delayed_job - 吞下失败的方法错误,而是抛出 "wrong number of arguments"

api - 其他国家的支付系统流程

PayPal IPN 无法到达 EC2 tomcat

java - 如何从新的 Paypal Sync Api 获取销售 ID

ruby-on-rails - 带有 Bootstrap 3 的 Simple_form 类表单水平在 Rails 4 中不起作用

ruby-on-rails - 可以预期这些参数在任何Rails应用程序中总是会提高500吗?

ruby-on-rails - 静态#home 中的 Haml::SyntaxError?

ruby-on-rails - 如何 stub 删除关联以返回 false