Google Pay
Google Pay 可在一次支付交互中确认 PaymentIntent。Android 实现也支持 SetupIntent,Web 实现则不支持。
https://stripe.com/docs/google-pay
在 Web 上,Google Pay 使用 Payment Request Button。开发和生产环境都应通过 HTTPS 提供应用。
平台支持
| 平台 | Google Pay |
|---|---|
| Android | 原生 GooglePayLauncher(需要应用元数据) |
| iOS | 未实现 |
| Web | Payment Request Button(stripe-pwa-elements) |
iOS 会拒绝 isGooglePayAvailable、createGooglePay 和 presentGooglePay。Android 在插件加载时从元数据读取 Google Pay 配置,因此在 Android 上仅调用 initialize 不够。
准备配置
strings.xml
在 android/app/src/main/res/values/strings.xml 中添加:
publishable_key(Stripe 可发布密钥)enable_google_paycountry_codemerchant_display_namegoogle_pay_is_testing
<string name="publishable_key">Your Publishable Key</string>
<bool name="enable_google_pay">true</bool>
<string name="country_code">US</string>
<string name="merchant_display_name">Widget Store</string>
<bool name="google_pay_is_testing">true</bool>
Android Google Pay 的可选 Stripe Connect 配置:
<string name="stripe_account">acct_xxxxxxxxxxxxx</string>
AndroidManifest.xml
在android/app/src/main/AndroidManifest.xml文件中,在manifest > application部分添加以下内容:
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
<meta-data
android:name="com.getcapacitor.community.stripe.enable_google_pay"
android:value="@bool/enable_google_pay"/>
<meta-data
android:name="com.getcapacitor.community.stripe.publishable_key"
android:value="@string/publishable_key"/>
<meta-data
android:name="com.getcapacitor.community.stripe.country_code"
android:value="@string/country_code"/>
<meta-data
android:name="com.getcapacitor.community.stripe.merchant_display_name"
android:value="@string/merchant_display_name"/>
<meta-data
android:name="com.getcapacitor.community.stripe.google_pay_is_testing"
android:value="@bool/google_pay_is_testing"/>
可选的关联账户:
<meta-data
android:name="com.getcapacitor.community.stripe.stripe_account"
android:value="@string/stripe_account"/>
可选项 1:如需获取用户信息,请设置以下内容:
<bool name="email_address_required">true</bool>
<bool name="phone_number_required">true</bool>
<bool name="billing_address_required">true</bool>
<string name="billing_address_format">Full</string>
<meta-data
android:name="com.getcapacitor.community.stripe.email_address_required"
android:value="@bool/email_address_required"/>
<meta-data
android:name="com.getcapacitor.community.stripe.phone_number_required"
android:value="@bool/phone_number_required"/>
<meta-data
android:name="com.getcapacitor.community.stripe.billing_address_required"
android:value="@bool/billing_address_required"/>
<meta-data
android:name="com.getcapacitor.community.stripe.billing_address_format"
android:value="@string/billing_address_format"/>
可选:使用 Google Pay 时,无需用户已有付款方式
如果设为 false,即使客户的 Google Pay 钱包中还没有支付方式,也会将 Google Pay 视为就绪。默认值为 true。
<bool name="google_pay_existing_payment_method_required">false</bool>
<meta-data
android:name="com.getcapacitor.community.stripe.google_pay_existing_payment_method_required"
android:value="@bool/google_pay_existing_payment_method_required"/>
1. isGooglePayAvailable
Google Pay 就绪时 Promise 会兑现,否则会被拒绝。
import { GooglePayEventsEnum, Stripe } from '@capacitor-community/stripe';
try {
await Stripe.isGooglePayAvailable();
} catch {
return;
}
method isGooglePayAvailable()
Google Pay 可用时 Promise 正常完成,不可用时会被拒绝。Google Pay 支持 Android 和兼容的浏览器,不支持 iOS。
isGooglePayAvailable() => Promise<void>
2. createGooglePay
paymentSummaryItems.amount 使用主要货币单位:10.99 表示 10.99 美元。Stripe 服务端 PaymentIntent 使用最小货币单位(同一笔美元金额对应 1099)。请确保界面显示的总额和后端 Intent 的金额一致。
从后端获取 PaymentIntent 客户端密钥。在 Android 上,也可以传入 SetupIntent 客户端密钥。参见服务器集成。无论哪种 Intent,选项名都是 paymentIntentClientSecret。Web 还需要 paymentSummaryItems、merchantIdentifier、countryCode 和 currency。
import { firstValueFrom } from 'rxjs';
const { paymentIntent } = await firstValueFrom(
this.http.post<{
paymentIntent: string;
}>(environment.api + 'intent', {}),
);
await Stripe.createGooglePay({
paymentIntentClientSecret: paymentIntent,
// 仅用于 Web,Android 应用中的 Google Pay 无需此项
paymentSummaryItems: [
{
label: 'Product Name',
amount: 10.99,
},
],
merchantIdentifier: 'merchant.com.getcapacitor.stripe',
countryCode: 'US',
currency: 'USD',
});
method createGooglePay(...)
创建 Google Pay 请求。请在 presentGooglePay() 之前调用。
createGooglePay(options: CreateGooglePayOption) => Promise<void>
interface CreateGooglePayOption
| 属性 | 类型 | 说明 | 起始版本 |
|---|---|---|---|
paymentIntentClientSecret |
string |
要通过 Google Pay 确认的 PaymentIntent 的客户端密钥。 | 3.2.0 |
paymentSummaryItems |
{ label: string; amount: number; }[] |
仅适用于 Web。需要 stripe-pwa-elements ^5.3.0。 | 3.2.0 |
merchantIdentifier |
string |
仅适用于 Web。需要 stripe-pwa-elements ^5.3.0。 | 3.2.0 |
countryCode |
string |
仅适用于 Web。需要 stripe-pwa-elements ^5.3.0。 | 3.2.0 |
currency |
string |
仅适用于 Web。需要 stripe-pwa-elements ^5.3.0。 | 3.2.0 |
SetupIntent 客户端密钥以 seti_ 开头。Android 检测到此前缀后,会调用 presentForSetupIntent,并使用创建选项中的 currency(默认 USD)。不要在 Web 上传入 SetupIntent:Web 实现使用 confirmCardPayment 确认付款。
3. presentGooglePay
const result = await Stripe.presentGooglePay();
if (result.paymentResult === GooglePayEventsEnum.Completed) {
// 这里只更新界面。履行订单之前,请在服务器上通过 webhook 确认付款成功。
}
method presentGooglePay()
展示由 createGooglePay() 创建的 Google Pay 请求。
presentGooglePay() => Promise<{ paymentResult: GooglePayResultInterface; }>
type alias GooglePayResultInterface
GooglePayEventsEnum.Completed | GooglePayEventsEnum.Canceled | GooglePayEventsEnum.Failed
将 Canceled 视为取消,Failed 视为错误。Android 重建 Activity 后,应优先通过结果监听器接收结果。参见事件监听器。
4. addListener
Stripe.addListener(GooglePayEventsEnum.Completed, () => {
console.log('GooglePayEventsEnum.Completed');
});
enum GooglePayEventsEnum
| 成员 | 值 |
|---|---|
Loaded |
'googlePayLoaded' |
FailedToLoad |
'googlePayFailedToLoad' |
Completed |
'googlePayCompleted' |
Canceled |
'googlePayCanceled' |
Failed |
'googlePayFailed' |
参考
<?xml version='1.0' encoding='utf-8'?>
<resources>
<string name="app_name">capacitor-stripe</string>
<string name="title_activity_main">capacitor-stripe</string>
<string name="package_name">io.ionic.starter</string>
<string name="custom_url_scheme">io.ionic.starter</string>
<string name="publishable_key">Your Publishable Key</string>
<bool name="enable_google_pay">true</bool>
<string name="country_code">US</string>
<string name="merchant_display_name">Widget Store</string>
<bool name="google_pay_is_testing">true</bool>
<bool name="email_address_required">true</bool>
<bool name="phone_number_required">true</bool>
<bool name="billing_address_required">true</bool>
<string name="billing_address_format">Full</string>
<bool name="google_pay_existing_payment_method_required">false</bool>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.ionic.starter">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:name="io.ionic.starter.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBarLaunch"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
<meta-data
android:name="com.getcapacitor.community.stripe.enable_google_pay"
android:value="@bool/enable_google_pay"/>
<meta-data
android:name="com.getcapacitor.community.stripe.publishable_key"
android:value="@string/publishable_key"/>
<meta-data
android:name="com.getcapacitor.community.stripe.country_code"
android:value="@string/country_code"/>
<meta-data
android:name="com.getcapacitor.community.stripe.merchant_display_name"
android:value="@string/merchant_display_name"/>
<meta-data
android:name="com.getcapacitor.community.stripe.google_pay_is_testing"
android:value="@bool/google_pay_is_testing"/>
<meta-data
android:name="com.getcapacitor.community.stripe.email_address_required"
android:value="@bool/email_address_required"/>
<meta-data
android:name="com.getcapacitor.community.stripe.phone_number_required"
android:value="@bool/phone_number_required"/>
<meta-data
android:name="com.getcapacitor.community.stripe.billing_address_required"
android:value="@bool/billing_address_required"/>
<meta-data
android:name="com.getcapacitor.community.stripe.billing_address_format"
android:value="@string/billing_address_format"/>
<meta-data
android:name="com.getcapacitor.community.stripe.google_pay_existing_payment_method_required"
android:value="@bool/google_pay_existing_payment_method_required"/>
</application>
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
import { GooglePayEventsEnum, Stripe } from '@capacitor-community/stripe';
(async () => {
try {
await Stripe.isGooglePayAvailable();
} catch {
return;
}
await Stripe.addListener(GooglePayEventsEnum.Completed, () => {
console.log('GooglePayEventsEnum.Completed');
});
// Replace `/your-intent-endpoint` with your backend from Server Integration.
const response = await fetch('/your-intent-endpoint', {
method: 'POST',
});
if (!response.ok) {
throw new Error(`Intent request failed: ${response.status}`);
}
const { paymentIntent } = (await response.json()) as {
paymentIntent: string;
};
// Prepare Google Pay
await Stripe.createGooglePay({
paymentIntentClientSecret: paymentIntent,
// Web only. Google Pay on Android App doesn't need
paymentSummaryItems: [{
label: 'Product Name',
amount: 10.99
}],
merchantIdentifier: 'merchant.com.getcapacitor.stripe',
countryCode: 'US',
currency: 'USD',
});
// Present Google Pay
const result = await Stripe.presentGooglePay();
if (result.paymentResult === GooglePayEventsEnum.Completed) {
// Update UI only. Fulfill orders from a verified server webhook.
}
})();