Airwallex logo
Airwallex logo

Mobile App - Alipay CN

Accept Alipay from your shopper’s Android or iOS phone on your mobile app.

Step 1. Initialize a Payment Intent

Create a Payment Intent with the Create a Payment Intent API API.

POST /api/v1/pa/payment_intents/create

JSON
1{
2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",
3 "amount": 100,
4 "currency": "CNY",
5 "merchant_order_id": "85d7b0e0-7235-11ea-862e-9f6aa1adfca6",
6 "return_url": "your_app_scheme://api/paymentId=xxx&status=xxx"
7}

Step 2. Obtain the payment URL

When a shopper selects to pay with Alipay on their mobile app, call the Confirm a Payment Intent API API to get a payment URL, which you can redirect the shopper to their Alipay app to complete the payment.

POST /api/v1/pa/payment_intents/{id}/confirm

JSON
1{
2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",
3 "payment_method": {
4 "type": "alipaycn",
5 "alipaycn": {
6 "flow": "mobile_app",
7 "os_type": "android"
8 }
9 }
10}

You will get a response similar to the following. Note that the package_name is only available for Android.

JSON
1{
2 // ... other fields omitted.
3 "next_action": {
4 "type": "redirect",
5 "method": "GET",
6 "url": "alipays://platformapi/startApp?appId=...",
7 "package_name": "com.eg.android.AlipayGphone",
8 "fallback_url": "https://render.alipay.com/p/s/i/?scheme=alipays..."
9 }
10}

Step 3. Redirect to the Alipay App to complete the payment

You can use the url returned in the confirm Payment Intent response to redirect the shopper to the Alipay app. Shoppers will complete payment in the Alipay mobile app.

We recommend using the native method to check if the Alipay app is installed. On success, try invoking it natively. If invocation fails or if Alipay is not installed, switch to a WebView client to open the fallback URL. The logic is illustrated by the following code sample:

Android

Specify the package name in the <queries> tags of AndroidManifest.xml to enable access to the corresponding app.

Kotlin
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 package="com.llw.scandemo">
5 ...
6 <queries>
7 <package android:name="com.eg.android.AlipayGphone" />
8 </queries>
9 ...
10</manifest>

Redirect shoppers to the app.

Kotlin
1val intent = Intent(Intent.ACTION_VIEW, url)
2intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
3intent.setPackage("com.eg.android.AlipayGphone")
4val activities = activity.packageManager.queryIntentActivities(intent, 0)
5
6// Check if the package is installed
7if (activities.size > 0) {
8 // Installed, launch the app in Android native way
9 try {
10 startActivity(intent)
11 } catch (e: Exception) {
12 // Exception is caught,launch the app within the Webview
13 startWebviewActivity(fallbackUrl)
14 }
15} else {
16 // Uninstalled, open the fallback URL in the WebView
17 // Note: If the redirection occurs in the WebView, no further action is needed, simply load the fallback url i.e, webView.loadUrl(fallbackUrl)
18 startWebviewActivity(fallbackUrl)
19}
20
21fun startWebviewActivity(url: Uri) {
22 val webviewIntent = Intent(activity, WebviewActivity::class.java)
23 webviewIntent.data = url
24 startActivity(webviewIntent)
25}

iOS

Redirect shoppers to the app.

Swift
1// Check if the app is installed
2if UIApplication.shared.canOpenURL(url) {
3 // Installed, launch the app in IOS native way
4 UIApplication.shared.open(url)
5} else {
6 // Uninstalled, oppen the fallback URL in the WebView
7 // Note: If the redirection occurs in the WebView, no further action is needed, simply load the fallback url i.e, webView.load(fallbackUrl)
8 let safariVC = SFSafariViewController.init(url: fallbackUrl)
9 safariVC.delegate = self
10 self.navigationController?.present(safariVC, animated: true)
11}

After the customer completes payment, Alipay will redirect the customer to your app, as dictated by the scheme URL specified in your return URL in the Create a Payment Intent API request.

Step 4. Query the payment result status

To get the payment result, we suggest you poll the status of the Payment Intent via the Retrieve a Payment Intent API API. You may start polling the Payment Intent status after the shopper is redirected back to your mobile app, i.e., the return_url passed when creating the Payment Intent.

GET /api/v1/pa/payment_intents/{id}

In addition, Airwallex will notify you of the payment result asynchronously via the webhooks. Please refer to the webhook documentation to set up your webhook accordingly. Although subscribing to webhook events is optional, it is recommended to subscribe to the payment_intent.succeeded webhook which indicates that the shopper has paid the order.

Was this page helpful?