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を構成し、setWebViewClient
とsetWebChromeClient
クラスをセットアップする必要があります。
Copy
- java
mWebView.setWebViewClient(new XsollaWebViewClient(this));
mWebView.setWebChromeClient(new XsollaWebChromeClient(this));
Android WebViewで決済UIを開くように構成するには:
- この例に従ってWebViewを構成します:
Copy
- java
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);
- この例に従って、
XsollaWebViewClient extends WebViewClient
クラスとこのクラスのshouldOverrideUrlLoading
とonPageFinished
メソッドを実装します:
Copy
- java
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);
}
}
XsollaWebChromeClient extends WebChromeClient
クラスとその中のonCreateWindow
とonCloseWindow
メソッドを実装します。
Copy
- java
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);
}
}
}
XsollaChildWebView extends WebView
クラスを実装します。
Copy
- java
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));
}
}
MainActivity
クラスにXsollaChildWebView mChildWebView
フィールドを実装します:
Copy
- java
mChildWebView = findViewById(R.id.childWebView);
2番目のWebView(childWebView
)は、決済UIで新しいウィンドウを開くために使用されます:
Copy
- xml
<?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
- java
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));
この記事は役に立ちましたか?
ご意見ありがとうございました!
あなたのメッセージを確認し、体験を向上させるために利用させていただきます。このページを評価する
このページを評価する
答えたくない
ご意見ありがとうございました!
誤字脱字などのテキストエラーを見つけましたか? テキストを選択し、Ctrl+Enterを押します。