Alipay
Configure Alipay (mainland China, optional)
Optional — customers in China. Alipay is for selling to customers in China. You need a Chinese business entity and an Alipay merchant account (an enterprise Alipay account and business verification).
Alipay is one of the main payment methods in mainland China.
Setup
-
Register on the Alipay Open Platform
- Go to the Alipay Open Platform
- Sign in or register with an enterprise Alipay account
- Finish business verification (business license and related documents)
-
Create an application
- In the console, create a Web/Mobile App (网页/移动应用)
- Enable PC website payment (电脑网站支付)
- Submit the app for review
-
Configure keys
- Under App details (应用详情) → Development settings (开发设置), configure the keys
- Use RSA2 (SHA256)
- Generate an app key pair and store it
- Upload the application public key to Alipay and copy the Alipay public key (支付宝公钥) that Alipay returns
-
Collect the parameters
- App ID: the application's unique ID
- App private key: signs your API requests
- Alipay public key: verifies signatures on Alipay callbacks
-
Set the callback URL
- Set the authorization callback in the app settings
- The URL must be reachable from the public internet
Environment variables
Add these to .env:
# Alipay
ALIPAY_APP_ID=2021000000000000 # App ID
# Keys are raw Base64 strings (no PEM headers)
# App private key: signs API requests (from the key generator)
ALIPAY_APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC..."
# Alipay public key: verifies callback signatures.
# This is the key Alipay returns, not your app public key.
ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgatiwfGM3RTw..."
# Async notify URL. Must be publicly reachable.
# For local development, use a tunnel.
ALIPAY_NOTIFY_URL=https://yourdomain.com/api/payment/webhook/alipay
# Sandbox (optional, default false)
ALIPAY_SANDBOX=falseThe webhook path is https://yourdomain.com/api/payment/webhook/alipay.
Key format
Alipay expects a raw Base64 string. Do not include PEM headers (-----BEGIN...-----).
Where each key comes from
-
App private key
- Generate it with Alipay's key tool
- Choose RSA2 (SHA256)
- Copy the private key as raw Base64
-
Alipay public key
- Upload your app public key on the Alipay Open Platform
- Alipay returns an "Alipay public key"
- Copy that value as raw Base64
Format
Correct (raw Base64):
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgatiwfGM3RTwpedahWmpzO...
Incorrect (PEM headers):
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgatiwfGM3RTwpedahWmpzO...
-----END PUBLIC KEY-----Which key goes where
| Key | Source | Purpose | Variable |
|---|---|---|---|
| App private key | Key generator | Sign API requests | ALIPAY_APP_PRIVATE_KEY |
| App public key | Key generator | Upload to Alipay | Leave it unset |
| Alipay public key | Returned by Alipay | Verify callback signatures | ALIPAY_PUBLIC_KEY |
The app public key and the Alipay public key are different. You generate the app public key and upload it. Alipay returns the Alipay public key, and that is the value used to verify signatures.
Sandbox
Alipay provides a sandbox so you can test without a real charge.
Turn the sandbox on
-
Set the environment variable
ALIPAY_SANDBOX=true -
Get sandbox credentials
- Open the Alipay sandbox
- Copy the sandbox App ID and keys
- Use the sandbox Alipay app or a sandbox buyer account
-
What the sandbox does
- Gateway:
https://openapi-sandbox.dl.alipaydev.com/gateway.do - Balances are virtual. Nothing is charged
- Behavior matches production
- Gateway:
Sandbox flow
- Configure the app with the sandbox App ID
- Start a payment. The app redirects to the sandbox checkout page
- Sign in with a sandbox buyer account and pay
- Confirm the webhook and the order status update
Reference: Alipay sandbox guide
Notes
- Alipay accepts CNY only
- One-time payments and credit packs only. Subscriptions are not supported
- Checkout uses PC website payment (
alipay.trade.page.pay). The buyer completes payment on Alipay's page - The callback URL must be HTTPS
- Keys are secrets. Keep them in environment variables
- The webhook response is plain text
successorfail - Signature checks use
checkNotifySignV2
Plan example
The config/payment.ts example below applies only to static pricing (the default). With dynamic pricing, you do not need to edit config/payment.ts. Create the plan in the admin panel at /admin/pricing.
amount is in yuan.
monthlyAlipay: {
provider: 'alipay',
id: 'monthlyAlipay', // Each plan needs its own id
amount: 0.01, // Yuan
currency: 'CNY',
duration: {
months: 1,
type: 'one_time' // Alipay is one-time only
},
i18n: {
'en': {
name: 'Alipay Monthly Plan',
description: 'Monthly one-time payment via Alipay',
duration: 'month',
features: ['All premium features', 'Priority support']
},
}
}Payment flow
PC website payment
Buyer selects a plan → order is created → payment form is generated →
redirect to Alipay → buyer signs in and pays →
Alipay redirects to returnUrl → webhook notify →
order status updates → access is grantedWhat the integration does
- Redirect checkout:
pageExecutebuilds an HTML form that submits to Alipay - Return URL: after payment, the buyer is sent to
returnUrl - Webhook: Alipay posts to
notifyUrl. The handler must respond withsuccess - Order query:
queryOrdercan read the order status directly
Back to Payment overview