Alipay (Online)
Accept online payments from Alipay users through your website using the Alipay Online payment method.
This payment method allows you to gain access to one of China's most popular online payment methods without needing an Alipay account or registering for a Chinese bank account. This guide walks you through the payment flow and details on how to implement.
This guide is for accepting online Alipay payments. See Alipay (In-Store) for accepting in-store Alipay payments using a barcode reader. See Get paid with Alipay for accepting in-store Alipay payments using the OmisePay application.
How To Enable
To enable Alipay Online, send an email requesting this feature to support@omise.co. You will need to review and accept new terms and conditions.
- Supported Countries: Thailand
- Minimum API version:
2017-11-02
See a previous version of this guide for instructions on how to implement for API versions prior to
2017-11-02
.
Payment Flow
When your customer chooses Alipay as their preferred payment method at checkout, they go through a redirect payment flow. This means they are redirected from your website to Alipay’s secure checkout page where they authorize and confirm the payment.
The currency displayed to your customer will your account funding currency and the equivalent amount in CNY. Upon confirmation, the customer is redirected back to the your website.
Implementation
To create a Alipay Online charge, make the following API requests.
- Create a new payment source using Omise.js or one of the mobile SDKs (iOS and Android)
- Create a new charge using the identifier of the source created in Step 1.
- After receiving the charge completion webhook event, retrieve the charge to verify its status (optional, but recommended).
Use your public key to create the Alipay Online source on the client (a customer's browser or mobile phone). Use your secret key to create the Alipay Online charge on the server.
If both the creation and charge of a source must happen server-side, you can create and charge the source in a single API request using your secret key.
Creating a Source
Alipay Online payments are implemented as sources.
When the customer confirms that they wish to pay with this payment method, create a new source specifying the type
, amount
, and currency
.
The following examples demonstrate the creation of a new Alipay Online source for ฿4,000.
Replace the omise_public_key
and $OMISE_PUBLIC_KEY
variables with your test public key found on the dashboard: https://dashboard.omise.co/test/keys
Using Omise.js, the
type
parameter is supplied as the first argument to thecreateSource
method.
Omise.setPublicKey(omise_public_key);
Omise.createSource('alipay', {
"amount": 400000,
"currency": "THB"
}, function(statusCode, response) {
console.log(response)
});
For testing, you can create the same request using curl.
curl https://api.omise.co/sources \
-u $OMISE_PUBLIC_KEY: \
-d "amount=400000" \
-d "currency=THB" \
-d "type=alipay"
{
"object": "source",
"id": "src_test_5h21kkapa9cev9wivj5",
"livemode": false,
"location": "/sources/src_test_5h21kkapa9cev9wivj5",
"created_at": "2019-08-29T04:55:21Z",
"type": "alipay",
"flow": "redirect",
"amount": 400000,
"currency": "THB",
"mobile_number": null,
"phone_number": null,
"references": null,
"name": null,
"email": null,
"barcode": null,
"store_id": null,
"store_name": null,
"terminal_id": null,
"installment_term": null,
"zero_interest_installments": null
}
The id
attribute is the source identifier (begins with src
).
Creating a Charge
Create a charge specifying the parameters source
, return_uri
, amount
, and currency
.
source
specifies the source identifier.return_uri
specifies the location on your website to which the customer should be redirected after completing the payment authorization step.amount
andcurrency
must matchamount
andcurrency
of the source.
The following example demonstrates how to create a new charge using curl.
Replace $OMISE_SECRET_KEY
with your test secret key found on the dashboard: https://dashboard.omise.co/test/keys.
Replace $SOURCE_ID
with the id
of the source.
curl https://api.omise.co/charges \
-u $OMISE_SECRET_KEY: \
-d "amount=400000" \
-d "currency=THB" \
-d "return_uri=https://example.com/orders/345678/complete" \
-d "source=$SOURCE_ID"
{
"object": "charge",
"id": "chrg_test_5h21kkd2f5vcrweqt1f",
"livemode": false,
"location": "/charges/chrg_test_5h21kkd2f5vcrweqt1f",
"created_at": "2019-08-29T04:55:21Z",
"amount": 400000,
"currency": "THB",
"funding_amount": 400000,
"funding_currency": "THB",
"fee": 14600,
"fee_vat": 1022,
"interest": 0,
"interest_vat": 0,
"net": 384378,
"description": null,
"metadata": {},
"status": "pending",
"capture": true,
"authorized": false,
"schedule": null,
"reversed": false,
"reversed_at": null,
"expires_at": "2019-09-05T04:55:21Z",
"expired": false,
"expired_at": null,
"voided": false,
"paid": false,
"paid_at": null,
"transaction": null,
"refunded_amount": 0,
"refunds": {
"object": "list",
"from": "1970-01-01T00:00:00Z",
"to": "2019-08-29T04:55:21Z",
"offset": 0,
"limit": 20,
"total": 0,
"order": "chronological",
"location": "/charges/chrg_test_5h21kkd2f5vcrweqt1f/refunds",
"data": []
},
"link": null,
"return_uri": "https://example.com/orders/345678/complete",
"failure_code": null,
"failure_message": null,
"card": null,
"customer": null,
"ip": null,
"dispute": null,
"source": {
"object": "source",
"id": "src_test_5h21kk4i5c385yebrkv",
"livemode": false,
"location": "/sources/src_test_5h21kk4i5c385yebrkv",
"created_at": "2019-08-29T04:55:20Z",
"type": "alipay",
"flow": "redirect",
"amount": 400000,
"currency": "THB",
"mobile_number": null,
"phone_number": null,
"references": null,
"name": null,
"email": null,
"barcode": null,
"store_id": null,
"store_name": null,
"terminal_id": null,
"installment_term": null,
"zero_interest_installments": null
},
"platform_fee": {
"percentage": null,
"fixed": null,
"amount": null
},
"disputable": false,
"capturable": false,
"reversible": false,
"refundable": false,
"authorize_uri": "https://pay.omise.co/offsites/ofsp_test_5h21kkd7we8eqmig9ka/pay"
}
Creating a Source and Charge
Alternatively, you can create and charge a source in a single API request.
curl https://api.omise.co/charges \
-u $OMISE_SECRET_KEY: \
-d "amount=400000" \
-d "currency=THB" \
-d "return_uri=http://example.com/orders/345678/complete" \
-d "source[type]=alipay"
Completing the Charge
At this point, you have created a new charge with its status
set to pending
.
Possible values for charge status
are pending
, successful
, failed
, and expired
.
The following sections detail how to authorize a charge, receive its completion webhook event, and update its status.
Authorizing the Charge
Redirect the customer to the location specified in authorize_uri
.
At this location, the customer logs in and confirms the Alipay (Online) payment.
You can simulate this authorization phase in test mode by visiting the authorize_uri
to manually mark the charge as Successful
or Failed
.
After the customer has completed the authorization phase, they will be redirected to the location you specified in return_uri
.
Receiving the Charge Completion Event
The best way to be notified of the completion of a charge is using webhook events. Set up a location on your server to receive webhook events, and add this location as a webhook endpoint on the dashboard at https://dashboard.omise.co/test/webhooks. Once a charge is completed, a POST request will be sent to this endpoint with the charge response embedded.
The key
attribute for the event object contains charge.complete
and the data
attribute contains the charge object.
See Events API for event object structure.
Checking the Charge Status
After receiving this event, or after you have manually marked the charge, check the charge status
again.
If the value of status
has updated to successful
, you got paid.
If the value of status
has updated to failed
, check the failure_code
and failure_message
in the charge object for an explanation.
Possible failure codes are listed below.
Failure Code | Description |
---|---|
failed_processing |
General payment processing failure. |
insufficient_balance |
Insufficient funds in the account or the card has reached the credit limit. |
payment_rejected |
Payment rejected by issuer. |
timeout |
Payer did not take action before charge expiration. |
Voids and Refunds
You can create a full or partial refund through our refund API or from the charge page on your dashboard.
Alipay (Online) charges can only be refunded within three months of the transaction date.
Limits
- Minimum:
2000
(฿20) - Maximum:
4900000
(฿49,000)