Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate paypal pay id passed by Android in Magento site

We are making an Android app for our Magento 1.9 site. On our site, we already integrated Paypal successfully.

In the app, the customer is able to do payments, and, using the Paypal SDK, we are getting the transaction id in app. The Android team will pass the transaction id to Magento and in Magento we need to validate payment.

Our Android team needs an API from the Magento side.

How can we do this?


We are getting the transaction id from paypal sdk after completion of order.

Please visit github link & search for "server for verification" (using CTRL+F).

In the link, they mentioned we have to verify with the server, so how can I verify with server after a payment is done through app?


The Paypal team sent us a Request Sample:

$apiContext = new ApiContext(new OAuthTokenCredential(
        "<CLIENT_ID>", "<CLIENT_SECRET>"));

$payment = Payment::get('PAY-5YK922393D847794YKER7MUI', $apiContext);

They say we have to send a request to PayPal using the above sample code.

Once we pass pay id, then PayPal will respond with full payment details.

how to use the above code in our site?

The Paypal team gave us this github code.

They say I have to use the whole SDK for getting payment details. How do I use it in Magento?

like image 652
fresher Avatar asked May 18 '16 12:05

fresher


1 Answers

The following explanations are based on the use case that, you've PayPal (either Express Checkout or Payment Pro) enabled in your Magento Web store, yet additionally integrating Mobile Native SDK in your client APP (rather than using the exsing Magento PayPal flow, embedded into a webview in your APP).

  1. Verifying the payment (after your Client APP got the payment-id in the response) is important for fraud prevention. Mobile APP interacts with PayPal servers independently, and you would not want to deliver the goods/service upon a mobile API response (which is easy to replicate) without server (your Magento server) verification against the actual payment contents.

  2. Apparently Magento doesn't come with this part of codes in the PayPal module and you need to implement your own, but yet you won't necessarily have to import the whole RESTful SDK just for a single payment look-up API call. Everything is based on JSON requests & JSON parsing, it works as long as you follow the PayPal RESTful payload scheme HERE, and initiate the request with curl statements.

  3. Checklist on a payment after your server obtains the details can be also found HERE.

Additional best practice for server-end implementation:

  • Store the payment-id e.g. id": "PAY-564191241M8701234KL57LXI" in your database along with the order data entry;
  • Store the debug-id in the error object (if there's an error response) from the verification (payment lookup) API response into your database for further trouble shooting purpose with PayPal support.

Sample API tests by curl commands would be like:

Step#1 - Getting access token for authentication

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "<your sandbox APP client>:<your sandbox APP secret>" \
  -d "grant_type=client_credentials"

Parse the sample response and obtain the access token:

{
  "scope": "https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.*",
  "access_token": "<Your-Access-Token-for-further-calls>",
  "token_type": "Bearer",
  "app_id": "APP-6XR95014SS315863X",
  "expires_in": 28800
}

Step#2 - Making the look-up call with the access-token and payment ID (returned by you APP, e.g. PAY-123456789)

curl https://api.sandbox.paypal.com/v1/payments/payment/PAY-123456789 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <the access token from Step#1>"

And you'll get the response similar with this, which contains the payment details you would compare & verify with your order information in the database

{
  "proof_of_payment": {
    "adaptive_payment": {
      "pay_key": "AP-70M68096ML426802W",
      "payment_exec_status": "COMPLETED",
      "timestamp": "2013-02-20T00:26:25Z",
      "app_id": "APP-91B933855X481767M"
    }
  },
  "payment": {
    "short_description": "Hipster t-shirt",
    "amount": "9.95",
    "currency_code": "USD"
  },
  "client": {
    "platform": "iOS",
    "paypal_sdk_version": "1.0.0",
    "environment": "live",
    "product_name": "PayPal iOS SDK"
  }
}
like image 176
pp_pduan Avatar answered Sep 28 '22 03:09

pp_pduan