在Rails中的Paypal自适应支付自动流程

时间:2014-01-20 14:05:17

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

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

  1. 客户购买服务并付款给我的应用。
  2. 经过几天,例如,一周后,我会在收取费用后向服务提供商付款。 这一点2,我有一个引擎,可以自动检查支付给客户的日期是否已经到来。如果是这样,它会尝试自动向提供商付款。
  3. 我有我的观点1,实施并使用Express Checkout。 对于我的观点2,我知道我应该使用Adaptative支付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 =“PAY”。所以,我想付钱,但在密钥中,我得到= CREATED,之后没有任何反应。我不想创建但是要完成交易。我错过了什么?

1 个答案:

答案 0 :(得分:3)

我没有看到你在任何地方重定向用户.. ??拨打Pay后,您必须将用户重定向到PayPal才能登录并验证付款。您不会这样做的唯一一次是,如果您在付款请求中包含预先批准密钥,但我不认为您是。

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

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

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

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

现在,我发现您正在进行隐式付款,您只需确保在请求中包含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参数,从而解决您的问题。