How to open payment UI to receive payments made with Google Pay

To receive payments made with Google Pay, you can use the following options of opening the payment UI:

  • In a browser — in this case, a user is redirected to an external browser from the game to make a payment. The payment UI will look like this:

  • In Android WebView — in this case, the payment UI opens seamlessly within the game application, like this:
  • In Custom Tabs — in this case, the payment UI opens inside the game application, but a user sees a browser’s bar. The payment UI will look like this:

In browser

To open the payment UI in a browser and receive payments via Google Pay, you don’t need to implement additional code. To set it up, follow the instruction for opening the payment UI.

In Android WebView

Android WebView is a pre-installed system component from Google that allows Android applications to display web content. For the payment UI and Google Pay to work correctly in Android WebView, you need to configure WebView and set up the setWebViewClient and setWebChromeClient classes.

Copy
Full screen
Small screen
mWebView.setWebViewClient(new XsollaWebViewClient(this));
mWebView.setWebChromeClient(new XsollaWebChromeClient(this));

To configure opening the payment UI in Android WebView:

  1. Configure WebView according to this example:

Copy
Full screen
Small screen
WebSettings webSettings = mWebView.getSettings();
webSettings.setBuiltInZoomControls(false);
webSettings.setUseWideViewPort(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  1. Implement the XsollaWebViewClient extends WebViewClient class and the shouldOverrideUrlLoading and onPageFinished methods in this class according to this example:
Copy
Full screen
Small screen
class XsollaWebViewClient extends WebViewClient {
    private final Context context;


    public XsollaWebViewClient(Context context) {
        this.context = context;
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.matches(".+://.*")) {
            try {
                Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                intent.addCategory("android.intent.category.BROWSABLE");
                intent.setComponent(null);
                context.startActivity(intent);
            } catch(URISyntaxException e) {
                Log.e("WebView", "Invalid URL format" + url, e);
            } catch (ActivityNotFoundException e) {
                Log.e("WebView", "No activity found to handle URL: " + url, e);
            }
            return true;
        }


        return false;
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }
}
  1. Implement the XsollaWebChromeClient extends WebChromeClient class and the onCreateWindow and onCloseWindow methods within it.
Copy
Full screen
Small screen
public class XsollaWebChromeClient extends WebChromeClient {
    private final Context mContext;


    public XsollaWebChromeClient(Context context) {
        mContext = context;
    }


    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, android.os.Message resultMsg) {
        MainActivity mainActivity = (MainActivity) mContext;
        mainActivity.mChildWebView.setVisibility(View.VISIBLE);


        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(mainActivity.mChildWebView);
        resultMsg.sendToTarget();
        return true;
    }


    @Override
    public void onCloseWindow(WebView window) {
        if (window != null && window instanceof XsollaChildWebView) {
            window.setVisibility(View.GONE);
        }
    }
}
  1. Implement the XsollaChildWebView extends WebView class.
Copy
Full screen
Small screen
public class XsollaChildWebView extends WebView {


    @SuppressLint("SetJavaScriptEnabled")
    public XsollaChildWebView(Context context, AttributeSet attrs) {
        super(context, attrs);


        WebSettings webSettings = getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);


        setWebViewClient(new XsollaWebViewClient(context));
        setWebChromeClient(new XsollaWebChromeClient(context));
    }
}
  1. Implement the XsollaChildWebView mChildWebView field in the MainActivity class:
Copy
Full screen
Small screen
mChildWebView = findViewById(R.id.childWebView);

The second WebView (childWebView) is used for opening new windows in the payment UI:

Example of implementing the activity:

Copy
Full screen
Small screen
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <com.example.app.XsollaChildWebView
        android:id="@+id/childWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
Note
Refer to the GitHub repository for detailed code samples.

In Custom Tabs

Custom Tabs is a feature in Android browsers that allow developers to add a customized browser experience directly within the application. The difference between WebView and Custom Tabs is that user actions in WebView are isolated from their other actions on the Internet, whereas using Custom allows synchronization of user actions with their other activity in Chrome on their Android device.

Code sample for opening the payment UI in Custom Tabs:

Copy
Full screen
Small screen
String url = "https://secure.xsolla.com/paystation4?token=${token}";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
Was this article helpful?
Thank you!
Is there anything we can improve? Message
We’re sorry to hear that
Please explain why this article wasn’t helpful to you. Message
Thank you for your feedback!
We’ll review your message and use it to help us improve your experience.
Rate this page
Rate this page
Is there anything we can improve?

Don’t want to answer

Thank you for your feedback!
Last updated: March 26, 2024

Found a typo or other text error? Select the text and press Ctrl+Enter.

Report a problem
We always review our content. Your feedback helps us improve it.
Provide an email so we can follow up
Thank you for your feedback!