Mobile Website Browser - Toss Pay
Accept Toss Pay payment from the shopper’s mobile browser by redirecting them to the Toss Pay page.
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": 1000,4 "currency": "KRW",5 "merchant_order_id": "85d7b0e0-7235-11ea-862e-9f6aa1adfca6",6 "return_url": "https://www.airwallex.com"7}
Step 2. Redirect to Toss Pay to complete payment
When a shopper selects to pay with Toss Pay on their mobile browser, call the Confirm a Payment Intent API API to get a redirect URL.
POST /api/v1/pa/payment_intents/{id}/confirm
JSON1{2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",3 "payment_method": {4 "type": "toss_pay"5 }6}
You will get a response similar to the following.
JSON1{2 // ... other fields omitted.3 "next_action": {4 "type": "redirect",5 "method": "GET",6 "url": "https://api-dev.airwallex.com/pa/redirect/sg/sgdvbl9nzgwda1q79km_1okyay?checksum=df2346229a46"7 }8}
You should redirect the shopper to the Toss Pay page with the next_action.url returned in the confirm Payment Intent response.
Note, if you use the mobile browser flow in your App’s Webview and pull up the Toss Pay app. Multiple redirects may happen after visiting next_action.url. When handling redirection to a non-HTTP URL, please add schemes when you want to pull up the Toss Pay app on the shopper’s mobile phone. For exmpale:
Android
Kotlin1webViewClient = object : WebViewClient() {2 override fun shouldOverrideUrlLoading(3 view: WebView?,4 request: WebResourceRequest?5 ): Boolean {6 if (!url.startsWith("http")) {7 val intent = Intent.parseUri(url, 0)8 try {9 startActivity(intent)10 finish()11 } catch (e: ActivityNotFoundException) {12 return doFallback(view, intent)13 }14 }15 return true16 }17 fun doFallback(view: WebView, parsedIntent: Intent?): Boolean {18 if (parsedIntent == null) {19 return false20 }21 val packageName = parsedIntent.getPackage()22 if (packageName != null) {23 startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)))24 return true25 }26 // "URL" is set by merchant27 val fallbackUrl = parsedIntent.getStringExtra("URL")28 if (fallbackUrl != null) {29 view.loadUrl(fallbackUrl)30 return true31 }32 return false33 }34 }
iOS
Swift1(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {2 NSString *absoluteString = navigationAction.request.URL.absoluteString;3 if (![absoluteString hasPrefix:@"http"]) {4 NSURL *url = [[NSURL alloc] initWithString:absoluteString];5 if (url) {6 [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];7 }8 decisionHandler(WKNavigationActionPolicyCancel);9 } else {10 decisionHandler(WKNavigationActionPolicyAllow);11 }12}
Step 3. 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 website or 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.