Skip to main content

Alipay Online Payments

This document outlines the integration steps for Alipay Web and WAP Payments for both Hong Kong and overseas merchants.

note

Alipay Hong Kong does not support the native Alipay checkout page. Developers must request a QR code and embed it using an iframe.

Overview

Alipay online payments allow customers to complete purchases on a merchant website by scanning a QR code. Once the customer confirms the amount, Alipay deducts funds from the customer’s wallet in real-time. The customer is then redirected back to the merchant website (if return_url is provided).

  • Settlement for Mainland China users is in CNY; QFPay settles to merchants in local currency.
  • Settlement for Hong Kong users is in HKD.

HTTP Request

Endpoint:
POST /trade/v1/payment

Supported Pay Types

PayTypeDescription
801101Alipay Web Payment (Overseas)
801107Alipay WAP Payment (Overseas)
801514Alipay Web Payment (Hong Kong)
801512Alipay WAP Payment (Hong Kong)

Request Parameters

FieldTypeRequiredDescription
txamtInt(11)YesTransaction amount in cents. Suggest > 200 to avoid risk control.
txcurrcdString(3)YesTransaction currency (e.g. HKD, CNY). See Currencies.
pay_typeString(6)YesPayment method (see above PayType list).
out_trade_noString(128)YesUnique transaction number per merchant per request.
txdtmString(20)YesTransaction datetime: YYYY-MM-DD hh:mm:ss.
expired_timeString(3)NoQR code expiration in minutes. 5–120 allowed. (MPM only)
goods_nameString(64)NoProduct name. Max 20 chars; UTF-8 encoded if Chinese.
mchidString(16)NoRequired if merchant has sub-Merchant ID assigned.
udidString(40)NoUnique device ID (for reporting).
return_urlString(512)NoRedirect URL after successful payment. Max 200 chars (for WAP only).

Response Parameters

FieldTypeDescription
respcdString(4)Return code. 0000 = success, 1143/1145 = retry needed, others = failed.
resperrString(128)Additional info or error message.
respmsgString(128)General response description.
syssnString(40)QFPay transaction ID.
out_trade_noString(128)Original order ID.
txamtInt(11)Transaction amount in cents.
txdtmString(20)Request datetime.
sysdtmString(20)Server datetime. Used for cutoff in settlements.
pay_typeString(6)Payment method used.
pay_urlString(512)Payment page URL. Embed as QR code or iframe.

Code Examples

Use the tabs below to view implementation in Python, Java, JavaScript, or PHP.
#coding=utf8
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, hashlib
import requests
import datetime
import string

# Enter Client Credentials
environment = 'https://test-openapi-hk.qfapi.com'
app_code = 'D5589D2A1F2E42A9A60C37*********'
client_key = '0E32A59A8B454940A2FF39**********'


# Create parameter values for data payload
current_time = datetime.datetime.now().replace(microsecond=0)

print(current_time)

# Create signature
def make_req_sign(data, key):
keys = list(data.keys())
keys.sort()
p = []
for k in keys:
v = data[k]
p.append('%s=%s'%(k,v))
unsign_str = ('&'.join(p) + key).encode("utf-8")
s = hashlib.md5(unsign_str).hexdigest()
return s.upper()


# Body payload
txamt = '10' #In USD,EUR,etc. Cent. Suggest value > 200 to avoid risk control.
txcurrcd = 'HKD'
pay_type = '801101' # Alipay Web Payment = 801101
auth_code='283854702356157409' #CPM only
out_trade_no = '01234567890123'
txdtm = current_time
goods_name = 'test1'
mchid = 'ZaMVg*****'
key = client_key


#data ={'txamt': txamt, 'txcurrcd': txcurrcd, 'pay_type': pay_type, 'out_trade_no': out_trade_no, 'txdtm': txdtm, 'goods_name': goods_name, 'mchid': mchid}
data ={'txamt': txamt, 'txcurrcd': txcurrcd, 'pay_type': pay_type, 'out_trade_no': out_trade_no, 'txdtm': txdtm, 'mchid': mchid}

r = requests.post(environment+"/trade/v1/payment",data=data,headers={'X-QF-APPCODE':app_code,'X-QF-SIGN':make_req_sign(data, key)})

print(r.json())

Sample JSON Response

{
"sysdtm": "2020-04-13 10:30:34",
"paydtm": "2020-04-13 10:30:34",
"txcurrcd": "HKD",
"respmsg": "",
"pay_type": "801101",
"cardcd": "",
"udid": "qiantai2",
"txdtm": "2020-04-13 10:30:34",
"txamt": "300",
"resperr": "success",
"out_trade_no": "4K35N374II7UJJ8RGIAE45O2CVHGHFF0",
"syssn": "20200413000300020087033882",
"respcd": "0000",
"pay_url": "https://globalmapi.alipay.com/gateway.do?total_fee=3.0&secondary_merchant_name=###merchant_name###&out_trade_no=20200413000300020087033882&secondary_merchant_industry=7011&service=create_forex_trade&_input_charset=UTF-8&sign=02beb99974ce6167666280b9727c4444&currency=THB&notify_url=https%3A%2F%2Fo2-hk.qfapi.com%2Fonline-test%2Ftrade%2Falipay%2Fv1%2Fonline_notify&order_valid_time=1800&secondary_merchant_id=2565075&sign_type=MD5&partner=2088631377368888&product_code=NEW_OVERSEAS_SELLER&order_gmt_create=2020-04-13+10%3A30%3A34&return_url=&subject=###merchant_name###",
"chnlsn": ""
}

Notes

tip

The QR code or iframe should point to the pay_url in the response.

warning

Do not reuse out_trade_no across payments or refunds.

info

You can use /trade/v1/query to check payment result if respcd = 1143/1145.