Google Payでの支払いを受け取るために決済UIを開く方法

Google Payで支払いを受け取るには、決済UIを開く以下のオプションを使用できます。

  • ブラウザの場合 - ユーザーはゲームから外部ブラウザにリダイレクトされて決済を行います。決済UIは次のように表示されます:

  • Android WebViewの場合 — 決済UIは、以下のようにゲームアプリケーション内でシームレスに開きます:
  • カスタムタブの場合 — 決済UIはゲームアプリケーション内で開きますが、ユーザーにはブラウザのバーが表示されます。決済UIは次のように表示されます:

ブラウザの場合

ブラウザで決済UIを開き、Google Payで支払いを受けるには、追加のコードを実装する必要はありません。設定方法は、決済UIを開く説明を参照してください。

Android WebViewの場合

Android WebViewはGoogleが提供するプリインストールのシステムコンポーネントで、Androidアプリケーションにウェブコンテンツを表示させることができます。Android WebViewで決済UIとGoogle Payを正しく動作させるには、WebViewを構成し、setWebViewClientsetWebChromeClientクラスをセットアップする必要があります。

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

Android WebViewで決済UIを開くように構成するには:

  1. この例に従ってWebViewを構成します:

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. この例に従って、XsollaWebViewClient extends WebViewClientクラスとこのクラスのshouldOverrideUrlLoadingonPageFinishedメソッドを実装します:
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. XsollaWebChromeClient extends WebChromeClientクラスとその中のonCreateWindowonCloseWindowメソッドを実装します。
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. XsollaChildWebView extends WebViewクラスを実装します。
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. MainActivityクラスにXsollaChildWebView mChildWebViewフィールドを実装します:
Copy
Full screen
Small screen
mChildWebView = findViewById(R.id.childWebView);

2番目のWebView(childWebView)は、決済UIで新しいウィンドウを開くために使用されます:

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>
お知らせ
詳細なコードサンプルについては、GitHubリポジトリを参照してください。

カスタムタブの場合

カスタムタブはAndroidブラウザの機能で、開発者はアプリケーション内で直接カスタマイズされたブラウザ体験を追加することができます。WebViewとカスタムタブの違いは、WebViewでのユーザーのアクションがインターネット上の他のアクションから分離されるのに対し、カスタムを使用すると、ユーザーのアクションがAndroidデバイス上のChromeでの他のアクティビティと同期されることです。

カスタムタブで決済UIを開くためのコードサンプル:

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));
この記事は役に立ちましたか?
ありがとうございます!
改善できることはありますか? メッセージ
申し訳ありません
この記事が参考にならなかった理由を説明してください。 メッセージ
ご意見ありがとうございました!
あなたのメッセージを確認し、体験を向上させるために利用させていただきます。
このページを評価する
このページを評価する
改善できることはありますか?

答えたくない

ご意見ありがとうございました!
最終更新日: 2024年3月26日

誤字脱字などのテキストエラーを見つけましたか? テキストを選択し、Ctrl+Enterを押します。

問題を報告する
当社は常にコンテンツを見直しています。お客様のご意見は改善に役立ちます。
フォローアップ用のメールをご提供してください
ご意見ありがとうございました!