Mobile App - Octopus
Accept Octopus from your mobile app on your shopper’s Android or iOS phone.
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
JSON1{2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",3 "amount": 10,4 "currency": "HKD",5 "merchant_order_id": "85d7b0e0-7235-11ea-862e-9f6aa1adfca6",6 "return_url": "your_app_scheme://api/paymentId=...&status=..."7}
Step 2. Obtain the payment URL
When a shopper selects to pay with Octopus on their mobile app, call the Confirm a Payment Intent API to get a payment URL, which you can redirect the shopper to the Octopus app to complete the payment.
POST /api/v1/pa/payment_intents/{id}/confirm
JSON1{2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",3 "payment_method": {4 "type": "octopus",5 "octopus": {6 "flow": "mobile_app",7 "os_type": "android"8 }9 }10}
You will get a response similar to the following. Note that package_name is only available for Android.
JSON1{2 // ... other fields omitted.3 "next_action": {4 "type": "redirect",5 "method": "GET",6 "url": "octopus://payment?token=bmdzJfY&return=https%3A%2F%2Fwww.airwallex.com%2F",7 "package_name": "com.octopuscards.nfc_reader",8 "fallback_url": "https://web.online.octopus.com.hk/oos/payment/?token=bmdzJfY&return=https%3A%2F%2Fwww.airwallex.com%2F"9 // ... other fields omitted10 }11}
Step 3. Redirect to Octopus App to complete payment
You can use the url returned in the confirm Payment Intent response to redirect the shopper to the Octopus app. Shoppers will complete payment in the Octopus mobile app.
We recommend using the native method to check if the Octopus app is installed. On success, try invoking it natively. If invocation fails or if Octopus is not installed, switch to a WebView client to open the 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.
Kotlin1<?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.octopuscards.nfc_reader" />8 </queries>9 ...10</manifest>
Redirect shoppers to the wallet app.
Kotlin1val intent = Intent(Intent.ACTION_VIEW, url)2intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)3intent.setPackage("com.octopuscards.nfc_reader")4val activities = activity.packageManager.queryIntentActivities(intent, 0)5// Check if the package is installed6if (activities.size > 0) {7 // Installed, launch the app in Android native way8 try {9 startActivity(intent)10 } catch (e: Exception) {11 // Exception is caught,launch the app within the Webview12 startWebviewActivity(url)13 }14} else {15 // Uninstalled, open the URL in the WebView16 // Note: If the redirection occurs in the WebView, no further action is needed, simply load the url i.e, webView.loadUrl(url)17 startWebviewActivity(url)18}19fun startWebviewActivity(url: Uri) {20 val webviewIntent = Intent(activity, WebviewActivity::class.java)21 webviewIntent.data = url22 startActivity(webviewIntent)23}
iOS
Redirect shoppers to the wallet app.
Swift1// Check if the app is installed2if UIApplication.shared.canOpenURL(url) {3 // Installed, launch the app in IOS native way4 UIApplication.shared.open(url)5} else {6 // Uninstalled, open the applink URL in the WebView7 // Note: If the redirection occurs in the WebView, no further action is needed, simply load the applink url i.e, webView.load(url)8 let safariVC = SFSafariViewController.init(url: url)9 safariVC.delegate = self10 self.navigationController?.present(safariVC, animated: true)11}
After the customer completes payment, Octopus 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.