I am going to explain to you how to make payments using the PayPal REST API call to create orders through PayPal in Magento 2 e-commerce platform.
Here I am using the PayPal payment method to create an order for which I will pay through PayPal REST API and complete the order.
So it is important to have the right API calls as well as the right payload to fulfill the order.
When we hit the Paypal API, other details like basic authorization and header details must also be correct, otherwise, the payment will not be completed.
Read more: Configure PayPal in Magento 2 : How to Integrate PayPal
Now the correct point is what is the correct API call and payload to complete the order using PayPal express payment method to process checkout payment get it done the order.
So let's start steps one by one in order to complete the product order via Paypal rest API.
Step 1: Get an Access Token PayPal Sandbox App
First of all, we have to do the configuration of Magento admin, for that you can follow this step of Paypal Integration in Magento 2.
When your account is created and all its details are received then you reach this step to get access token using Paypal rest API.
You can proceed to the next step only after getting the required sandbox account details from the PayPal Developer Dashboard, for this you can complete those steps of the development environment to get OAuth 2.0 client ID and secret credentials for the sandbox accounts.
The access token you received authorizes you to access the PayPal REST API server by using those tokens.
Endpoint:
https://api-m.sandbox.paypal.com/v1/oauth2/token
Method: POST
Authorization: Basic Auth
Username: My app and credentials(client_id)
Password: Client secret
Body: x-www-form-urlencoded
grant_type = client_credentials
Step 2: Create Order Using PayPal REST API
Now by using the below payload details you can create an order and complete the payment by using Paypal REST Apis call.
Endpoint:
https://api-m.sandbox.paypal.com/v1/payments/payment
Method: POST
Header:
Content-type: application/json
Authorization: <access-token>
Authorization: Basic Auth
Username: my app and credentials(client_id)
Password: Client secret
{ "intent": "authorize", "experience_profile_id": "XP-T8WE-L7SC-RN4L-QJAS", "payer": { "payment_method": "paypal" }, "transactions": [{ "amount": { "currency": "USD", "total": "1.25", "details": { "shipping": "0.1", "subtotal": "1", "tax": "0.15" } }, "payee": { "email": "cs.hemantsingh@gmail.com" }, "description": "Test paypal payment done on 13th Jan 2022", "item_list": { "items": [{ "name": "acessories belt monolog", "quantity": "1", "price": "1", "sku": "test1", "currency": "USD" }], "shipping_address": { "recipient_name": "Hemant Singh", "line1": "New test", "city": "Saratoga", "country_code": "US", "postal_code": "95070", "phone": "0116519999164", "state": "CA" } } }], "redirect_urls": { "return_url": "https://wishusucess.com/", "cancel_url": "https://wishusucess.com/" } }
Response: Order Created Using PayPal Payment Method
{ "id": "PAYID-MHL4VTA69N500857X1559800", "intent": "authorize", "state": "created", "payer": { "payment_method": "paypal" }, "transactions": [ { "amount": { "total": "1.25", "currency": "USD", "details": { "subtotal": "1.00", "tax": "0.15", "shipping": "0.10" } }, "payee": { "email": "cs.hemantsingh@gmail.com" }, "description": "Test paypal payment done on 13th Jan 2022", "item_list": { "items": [ { "name": "acessories belt monolog", "sku": "acessoriebelt321", "price": "1.00", "currency": "USD", "quantity": 1 } ], "shipping_address": { "recipient_name": "Hemant Singh", "line1": "New test", "city": "Saratoga", "state": "CA", "postal_code": "95070", "country_code": "US", "phone": "0116519999164" } }, "related_resources": [] } ], "experience_profile_id": "XP-T8WE-L7SC-RN4L-QJAS", "create_time": "2022-01-07T05:08:28Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MHL4VTA69N500857X1559800", "rel": "self", "method": "GET" }, { "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-47V04030JC5984021", "rel": "approval_url", "method": "REDIRECT" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MHL4VTA69N500857X1559800/execute", "rel": "execute", "method": "POST" } ] }
Approval PayPal REST API Payments
Paypal Login Details: Enter your Paypal username
Password: Enter your Paypal password details
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50C051037E557641M",
"rel": "approval_url",
"method": "REDIRECT"
Now you have to hit the approval URL in your response body and proceed as a guest for the testing purpose and it will redirect to the website to review the order.
As soon as you do this complete step, then your order will be completed and it will automatically go to your website.