There are several compelling reasons to consider utilizing the hosted checkout in a WebView rather than integrating directly with the mobile SDK. It is important to note that regardless of the chosen method, the payments are still processed by the same underlying APIs. The distinction lies in the payment experience that you provide to your customers. Here are some advantages of using a WebView:
By utilizing our hosted checkout, Lahza takes complete control of the payment experience. This relieves you from the burden of building a checkout UI within your mobile application, significantly reducing the amount of code required for seamless payment integration.
While the mobile SDK only supports card payments, the checkout offers a broader range of non-card payment options. This includes USSD, QR, EasyLife, and Pay-with-Bank, providing a wider array of payment choices for your customers.
Generating the checkout URL
To begin the integration process, the first step is to initialize the transaction, which will generate a checkout URL for further processing.
When a customer clicks the payment action button, you can initialize a transaction by sending a POST request from your server to our API. Include the required parameters such as email, amount, and any other relevant optional parameters when calling the Initialize
Once the API call is successful, Lahza will provide an authorization URL in the response. You should then retrieve this URL and pass it back to your frontend application. Load the obtained URL in the WebView widget to display the payment interface. Refer to the sample code below for an example implementation:
Allow Redirect
Enable redirects in the WebView widget to ensure smooth bank and 3DS authorization. Blocking redirects may prevent customers from completing transactions.
import React from'react';import { WebView } from'react-native-webview';exportdefaultfunctionApp() {constauthorization_url='https://checkout.lahza.io/kjIjs82H';constcallback_url='https://yourcallback.com';onNavigationStateChange= state => {const { url } = state;if (!url) return;if (url === callback_url) {// get transaction reference from url and verify transaction, then redirectconstredirectTo='window.location = "'+ callback_url +'"';this.webview.injectJavaScript(redirectTo); }if(url ==='https://api.lahza.io/close') {// handle webview removal// You can either unmount the component, or// Use a navigator to pop off the view } };return ( <WebViewsource={{ uri: authorization_url }}style={{ marginTop:40 }}onNavigationStateChange={ this.onNavigationStateChange } /> );}
importWebKitclassCheckoutViewController:UIViewController, WKNavigationDelegate {..........// This is a WKNavigationDelegate func we can use to handle redirectionfuncwebView(_webView: WKWebView, decidePolicyFornavigationAction: WKNavigationAction, decisionHandler: @escaping ((WKNavigationActionPolicy) ->Void) { if let url = navigationAction.request.url {/* We can check here if the 3DS flow is done by checking the redirected URl is the one for 3DS after completition */if url.absoluteString =="https://api.lahza.io/close"{decisionHandler(.cancel) }else{decisionHandler(.allow) } } }}
Keep in mind
When using the WebView, listen for URL redirects to handle customer actions during the payment process.
Successful payments in the WebView are redirected to the callback URL specified in your Lahza dashboard, but you can override it with a custom callback URL.
If you have webhooks set up, Lahza will send a charge.success event to your webhook URL. Utilize this event in your backend to deliver value to the customer.
In the frontend, after the WebView redirects to the callback URL, make a call to the Verify Transaction API to confirm the payment status.
For card transactions with 3DS authentication, the page doesn't close automatically in the WebView as it does in a web browser.
Implement a workaround by having the WebView listen for the redirection to a specific URL (e.g., https://api.lahza.io/close).
Once the WebView detects this URL, continue processing as usual by checking the callback function and any other necessary processes.
By following these steps, you can ensure proper handling of customer actions, confirmation of payment status, and handling of card transactions with 3DS authentication in the WebView.