Accept Alipay payments
Last updated: Sept 19th, 3:18am
Alipay is a third-party mobile and online payment platform used to make cashless payments over the world.
Buyer Countries | Payment type | Payment flow | Currencies | Maximum amount | Refunds |
---|---|---|---|---|---|
China (CN ) |
Wallet | redirect | AUD CAD EUR GBP HKD NZD SGD USD |
N/A | Within 360 days |
How it works
- You add alternative payment methods to your checkout page.
- Payer provides their personal details and selects an alternative payment method from your checkout page.
- Payer is transferred from your checkout page to the third-party bank to authorize and confirm payment.
- Payer returns to your site to see confirmation of purchase.
Know before you code
-
Integration steps for implementing alternate payment methods are similar. If an alternate payment method has been previously integrated, most of the code may be reused.
-
Make sure you're subscribed to the following webhook events:
- The
PAYMENT.CAPTURE.COMPLETED
webhook event indicates a successful order capture. - The
PAYMENT.CAPTURE.DENIED
webhook event indicate a failed order capture.
- The
-
After you receive each webhook, fetch the latest order details using Show order details. The
up
HATEOAS link in the webhook payload indicates the order associated with the capture. -
When processing Alipay payments, you don't need to capture payment for the order.
1. Offer Alipay on your checkout page
You'll need to create the user interface to offer Alipay and collect the payer's full name. Then you'll use the API calls described in the remainder of this topic to:
- Create the order with Alipay as the payment method, the payer's
full name
, and thecountry_code
. - Redirect the payer to Alipay
Refer to Payment method icons for icons you to use on your checkout page.
2. Create an order with Alipay as the payment source
Use the payer information you captured from your user interface to create an order with Alipay as the payment source.
Sample request
API endpoint used: Create order
1curl --location --request POST 'https://api-m.sandbox.paypal.com/v2/checkout/orders' \2--header 'Content-Type: application/json' \3--header 'Authorization: Bearer ACCESS-TOKEN' \4--header 'PayPal-Request-Id: PAYPAL-REQUEST-ID' \5--data-raw '{6 "intent": "CAPTURE",7 "payment_source": {8 "alipay": {9 "country_code": "CN",10 "name": "John Doe"11 }12 },13 "processing_instruction": "ORDER_COMPLETE_ON_PAYMENT_APPROVAL",14 "purchase_units": [15 {16 "invoice_id": "Invoice-12345",17 "custom_id": "Custom-1234",18 "amount": {19 "currency_code": "HKD",20 "value": "49.11"21 },22 "shipping": {23 "name": {24 "full_name": "John Doe"25 },26 "address": {27 "address_line_1": "HongKong Rd East, 126",28 "address_line_2": "Floor 5",29 "admin_area_2": "Qingdao",30 "admin_area_1": "rm",31 "postal_code": "266071",32 "country_code": "CN"33 }34 }35 }36 ]37}'
Modify the code
After you copy the code in the sample request, modify the following:
ACCESS-TOKEN
- Your access token.PAYPAL-REQUEST-ID
- Replace the sample ID with a unique ID that you generate. ThePAYPAL-REQUEST-ID
helps prevent duplicate authorizations if the API call is disrupted. For more information about idempotent requests, see API idempotency.intent
- Set this value toCAPTURE
.payment_source
- Specifyalipay
, and include the following:country_code
name
- The payer's full name.experience_context
- Specify the following:locale
- The preferred language for returned errorsreturn_url
- The URL the buyer is returned to after approving the purchase with their selected payment method.cancel_url
- The URL the buyer is returned to after canceling an approval with their selected payment method.processing_instruction
- Set this value toORDER_COMPLETE_ON_PAYMENT_APPROVAL
.purchase_units: amount
- Specify thevalue
of the order and thecurrency code
.
Step result
A successful request results in the following:
- A return status code of HTTP
200 OK
. - A JSON response body that contains the order ID.
- HATEOAS links.
Sample response
1{2 "id": "5V159329PV571861D",3 "status": "PAYER_ACTION_REQUIRED",4 "payment_source": {5 "alipay": {6 "name": "John Doe",7 "country_code": "CN"8 }9 },10 "links": [11 {12 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5V159329PV571861D",13 "rel": "self",14 "method": "GET"15 },16 {17 "href": "https://www.sandbox.paypal.com/payment/alipay?token=5V159329PV571861D",18 "rel": "payer-action",19 "method": "GET"20 }21 ]22}
3. Redirect payer to Alipay
In your user interface, attach the payer-action
redirect URL returned in the Create Order
response to the Alipay payment button. This sends the payer to their wallet to approve the purchase. After the payer approves the purchase, the payment is automatically captured.
In the sample, the redirect URL is: "https://www.sandbox.paypal.com/payment/alipay?token=5V159329PV571861D"
.
Step result
After successfully completing the wallet transaction:
- The payer is redirected to the
return_url
in the Create Order request. - The
PAYMENT.CAPTURE.COMPLETED
webhook event is triggered, indicating successful payment capture.
After an unsuccessful bank approval, the payer is redirected to the cancel_url
in the Create Order request.
4. Listen to webhooks to get the result of payment status
The following webhooks provide the result of order capture:
- The
PAYMENT.CAPTURE.COMPLETED
webhook event indicates a successful order capture. - The
PAYMENT.CAPTURE.DENIED
webhook events indicate a failed order capture. - Optional: Use Show order details endpoint to determine the status of an order.
- The
up
HATEOAS link indicates the order associated with this capture.
- The
Here are some additional resources as you create webhook handler code:
- Webhook Management API - Manage webhooks, list event notifications, and more. For more information about webhooks, see:
- Subscribe to checkout webhooks
- Checkout webhook events - Checkout payer approval-related webhooks.
- Order webhook events - Other order-related webhooks.
- Show order details endpoint - Determine the status of an order.
Sample PAYMENT.CAPTURE.COMPLETED webhook
1{2 "id": "WH-9LK14475MF9937440-9EG47645TP521822L",3 "event_version": "1.0",4 "create_time": "2022-05-10T06:04:12.533Z",5 "resource_type": "capture",6 "resource_version": "2.0",7 "event_type": "PAYMENT.CAPTURE.COMPLETED",8 "summary": "Payment completed for HKD 100.0 HKD",9 "resource": {10 "amount": {11 "value": "100.00",12 "currency_code": "HKD"13 },14 "supplementary_data": {15 "related_ids": {16 "order_id": "5V159329PV571861D"17 }18 },19 "update_time": "2022-01-19T06:04:08Z",20 "create_time": "2022-01-19T06:04:08Z",21 "final_capture": true,22 "seller_receivable_breakdown": {23 "paypal_fee": {24 "value": "3.80",25 "currency_code": "HKD"26 },27 "gross_amount": {28 "value": "100.00",29 "currency_code": "HKD"30 },31 "net_amount": {32 "value": "96.20",33 "currency_code": "HKD"34 }35 },36 "custom_id": "Custom-1234",37 "invoice_id": "Invoice-12345",38 "links": [39 {40 "method": "GET",41 "rel": "self",42 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/5R102774VL663561J"43 },44 {45 "method": "POST",46 "rel": "refund",47 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/5R102774VL663561J/refund"48 },49 {50 "method": "GET",51 "rel": "up",52 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5V159329PV571861D"53 }54 ],55 "id": "5R102774VL663561J",56 "status": "COMPLETED"57 },58 "links": [59 {60 "href": "https://api-m.sandbox.paypal.com/v1/notifications/webhooks-events/WH-9LK14475MF9937440-9EG47645TP521822L",61 "rel": "self",62 "method": "GET"63 },64 {65 "href": "https://api-m.sandbox.paypal.com/v1/notifications/webhooks-events/WH-9LK14475MF9937440-9EG47645TP521822L/resend",66 "rel": "resend",67 "method": "POST"68 }69 ]70}
Sample PAYMENT.CAPTURE.DENIED webhook
1{2 "id": "WH-9LK14475MF9937440-9EG47645TP521822L",3 "event_version": "1.0",4 "create_time": "2022-05-10T06:04:12.533Z",5 "resource_type": "capture",6 "resource_version": "2.0",7 "event_type": "PAYMENT.CAPTURE.DENIED",8 "summary": "Payment denied for HKD 100.0 HKD",9 "resource": {10 "amount": {11 "value": "100.00",12 "currency_code": "HKD"13 },14 "supplementary_data": {15 "related_ids": {16 "order_id": "5V159329PV571861D"17 }18 },19 "update_time": "2022-05-10T06:04:08Z",20 "create_time": "2022-05-10T06:04:08Z",21 "final_capture": true,22 "seller_receivable_breakdown": {23 "paypal_fee": {24 "value": "3.80",25 "currency_code": "HKD"26 },27 "gross_amount": {28 "value": "100.00",29 "currency_code": "HKD"30 },31 "net_amount": {32 "value": "96.20",33 "currency_code": "HKD"34 }35 },36 "links": [37 {38 "method": "GET",39 "rel": "self",40 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/5R102774VL663561J"41 },42 {43 "method": "POST",44 "rel": "refund",45 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/5R102774VL663561J/refund"46 },47 {48 "method": "GET",49 "rel": "up",50 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5V159329PV571861D"51 }52 ],53 "id": "5R102774VL663561J",54 "status": "DECLINED"55 },56 "links": [57 {58 "href": "https://api-m.sandbox.paypal.com/v1/notifications/webhooks-events/WH-9LK14475MF9937440-9EG47645TP521822L",59 "rel": "self",60 "method": "GET"61 },62 {63 "href": "https://api-m.sandbox.paypal.com/v1/notifications/webhooks-events/WH-9LK14475MF9937440-9EG47645TP521822L/resend",64 "rel": "resend",65 "method": "POST"66 }67 ]68}
Sample request
1curl -v -X GET https://api-m.sandbox.paypal.com/v2/checkout/orders/5V159329PV571861D \2 -H "Content-Type: application/json" \3 -H "Authorization: Bearer ACCESS-TOKEN"
Sample response
1{2 "id": "5V159329PV571861D",3 "intent": "CAPTURE",4 "status": "COMPLETED",5 "payment_source": {6 "alipay": {7 "name": "John Doe",8 "country_code": "CN"9 }10 },11 "processing_instruction": "ORDER_COMPLETE_ON_PAYMENT_APPROVAL",12 "purchase_units": [13 {14 "reference_id": "default",15 "amount": {16 "currency_code": "HKD",17 "value": "100.00"18 },19 "payments": {20 "captures": [21 {22 "id": "5R102774VL663561J",23 "status": "COMPLETED",24 "amount": {25 "currency_code": "HKD",26 "value": "100.00"27 },28 "final_capture": true,29 "seller_receivable_breakdown": {30 "gross_amount": {31 "currency_code": "HKD",32 "value": "100.00"33 },34 "paypal_fee": {35 "currency_code": "HKD",36 "value": "3.80"37 },38 "net_amount": {39 "currency_code": "HKD",40 "value": "96.20"41 }42 },43 "links": [44 {45 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/5R102774VL663561J",46 "rel": "self",47 "method": "GET"48 },49 {50 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/5R102774VL663561J/refund",51 "rel": "refund",52 "method": "POST"53 },54 {55 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5V159329PV571861D",56 "rel": "up",57 "method": "GET"58 }59 ]60 }61 ]62 }63 }64 ],65 "links": [66 {67 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5V159329PV571861D",68 "rel": "self",69 "method": "GET"70 }71 ]72}
Step result
A successful request returns the HTTP 200 OK
status code with a JSON response body that returns a COMPLETED
status.
A captured order has the following:
- A
COMPLETED
order status, which means the order was captured successfully. - A capture with
COMPLETED
status is present in the response parameterpurchase_units[0].payments.captures[0]
. - The
up
HATEOAS link indicates the order associated with this capture.
5. Notify payer of success
After a successful payment, notify the payer of a successful transaction.
Next steps
Test in PayPal sandbox, then go live in PayPal's production environment.