Paypal REST API: How to retrieve payment ID after user has approved the payment.

为君一笑 提交于 2019-12-18 14:45:15

问题


By following the guide on https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/ , I have successfully created a payment and redirect the user to approve it.

The created payment is something look like bellow, and I save it in user's session for further reference.

{
  "id": "PAY-6RV70583SB702805EKEYSZ6Y",
  "create_time": "2013-03-01T22:34:35Z",
  "update_time": "2013-03-01T22:34:36Z",
  "state": "created",
  "intent": "sale",
  "payer": {
    "payment_method": "paypal"
  },
  "transactions": [
    {
      "amount": {
        "total": "7.47",
        "currency": "USD",
        "details": {
          "subtotal": "7.47"
        }
      },
      "description": "This is the payment transaction description."
    }
  ],
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RV70583SB702805EKEYSZ6Y",
      "rel": "self",
      "method": "GET"
    },
    {
      "href": "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-60U79048BN7719609",
      "rel": "approval_url",
      "method": "REDIRECT"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RV70583SB702805EKEYSZ6Y/execute",
      "rel": "execute",
      "method": "POST"
    }
  ]
}

After user approved the payment, Paypal will redirect the user to the return_url. For example, http://<return_url>?token=EC-60U79048BN7719609&PayerID=7E7MGXCWTTKK2.

In order to execute the payment, a POST request has to made to https://api.sandbox.paypal.com/v1/payments/payment/{payment_id}/execute/.

Question

The only two pieces of information provided from Paypal in the URL is token and PayerID. How can I find the corresponding payment_id?

Possible Solution

The token is part of the approval_url, parse the URL and store the token -> payment relationship can solve the problem. But I'm looking for a better solution that doesn't require parsing.


回答1:


I think the paypal documentation isn't clear about this. But you can do something simple to resolve your problem passing de PaymentID through a parameter in your return url.

Like this: return_url = 'http://www.yourdomain.com/paypal/success/?paymentID=PAY-1234567'

When the Paypal redirect to your site, then, it will return the paymentID together with the other parameters.


Em português:

Não acho que isso esteja claro na documentação do Paypal. A solução que eu usei foi simples, eu passo o PaymentID como parâmetro na minha URL de retorno que informo para o Paypal. Assim, quando o Paypal redirecionar para meu site de volta ele matem o parâmetro com o PaymentID que eu tinha passado.




回答2:


You would have to remember the Payment ID on your side (typically attached with your user session - shopping cart or order or as a session cookie) before redirecting the user to PayPal approval url. Once the is redirected back to your return Url along with the PayerID - you would need to extract the PaymentID from your user session and execute the Payment.




回答3:


The Payment Id can be obtained in PHP by using the following method after the first API request has returned a successful response:

$payment->getId();

The online code sample (http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html) shows how to send the request, however it does not include the getId() method.

To find this out I had to look in the downloaded SDK files at the file sample\payments\CreatePayment.php which has the following example code showing the use of this method:

ResultPrinter::printResult('Create Payment Using Credit Card', 'Payment', $payment->getId(), $request, $payment);



回答4:


I found this link to be extremely helpful, in case anyone wants to check it out: https://github.com/paypal/PayPal-NET-SDK/issues/79




回答5:


  1. paymentid:

After you created the payment, in return json, you can get paymentid. like this: "id":"PAY-01K00482KX842131HKORKVKY"

  1. payerid:

you can use API:GET /v1/payments/payment/{paymentId} to get payer id after user approved the payment,and you will find payerid in return json,like this:

{
    "id":"PAY-01K00482KX842131HKORKVKY",
    "create_time":"2014-06-19T09:17:31Z",
    "update_time":"2014-06-19T09:17:31Z",
    "state":"created",
    "intent":"sale",
    "payer":{
            "payment_method":"paypal",
            "payer_info":{
                    "email":"buyer@samsung.com",
                    "first_name":"buyer",
                    "last_name":"samsung",
                    "payer_id":"H2DKRTTYWW8HS",
                    "shipping_address":{                                              "line1":"Lushan Road Num.188",                                              "line2":"JianYe",
                                              "city":"Tucson",
                                                   "state":"AZ",   
                                                   "postal_code":"85715", 
                                                   "country_code":"US",
                                                    "recipientName":"buyer samsung"}}},
    "transactions":[{
                           "amount":{
                                        "total":"12.00",
                                        "currency":"USD",
                                        "details"{"subtotal":"12.00"}},
                 "description":"creating a payment"}],
    "links":[
                   {"href":"xxxxxxx","rel":"self","method":"GET"},
              {"href":"xxxxxxx","rel":"approval_url","method":"REDIRECT"},
              {"href":"xxxxxxx","rel":"execute","method":"POST"}]}


来源:https://stackoverflow.com/questions/18543958/paypal-rest-api-how-to-retrieve-payment-id-after-user-has-approved-the-payment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!