ApplePay
Apple Pay 可在一次界面展示中确认 PaymentIntent。
https://stripe.com/docs/apple-pay
平台支持
| 平台 | Apple Pay |
|---|---|
| iOS | 原生 STPApplePayContext |
| Android | 未实现 |
| Web | Payment Request Button(stripe-pwa-elements) |
updateApplePaySheet以及运输信息更新的功能仅适用于 iOS 系统。对于updateApplePaySheet功能,网络请求会被视为未实现。而 Android 系统则拒绝处理isApplePayAvailable、createApplePay和presentApplePay这些请求。
准备配置
- 注册 Apple Merchant ID
- 创建 Apple Pay 证书
- 在 Xcode 中启用 Apple Pay
https://stripe.com/docs/apple-pay#merchantid
createApplePay 的 merchantIdentifier 必须与 Apple Developer 账户和 Xcode 中注册的商户 ID 相同。此处不要传入 merchantDisplayName;该选项属于 PaymentSheet 和 PaymentFlow。
1. isApplePayAvailable
创建请求前先检查设备。Apple Pay 可用时 Promise 正常完成,不可用时则被拒绝。
import { ApplePayEventsEnum, Stripe } from '@capacitor-community/stripe';
try {
await Stripe.isApplePayAvailable();
} catch {
return;
}
method isApplePayAvailable()
Apple Pay 可用时 Promise 正常完成,不可用时会被拒绝。Apple Pay 支持 iOS 和兼容的浏览器,不支持 Android。
isApplePayAvailable() => Promise<void>
2. createApplePay
paymentSummaryItems.amount 使用主要货币单位:10.99 表示 10.99 美元。Stripe 服务端 PaymentIntent 使用最小货币单位(同一笔美元金额对应 1099)。请确保界面显示的总额和后端 Intent 的金额一致。
从后端获取 PaymentIntent 客户端密钥,参见服务器集成。随后传入 paymentIntentClientSecret、paymentSummaryItems、merchantIdentifier、countryCode 和 currency。
import { firstValueFrom } from 'rxjs';
const { paymentIntent } = await firstValueFrom(
this.http.post<{
paymentIntent: string;
}>(environment.api + 'intent', {}),
);
await Stripe.createApplePay({
paymentIntentClientSecret: paymentIntent,
paymentSummaryItems: [
{
label: 'Product Name',
amount: 10.99,
},
],
merchantIdentifier: 'merchant.com.getcapacitor.stripe',
countryCode: 'US',
currency: 'USD',
});
method createApplePay(...)
创建 Apple Pay 请求。请在 presentApplePay() 之前调用。
createApplePay(options: CreateApplePayOption) => Promise<void>
interface CreateApplePayOption
| 属性 | 类型 | 说明 | 起始版本 |
|---|---|---|---|
paymentIntentClientSecret |
string |
要通过 Apple Pay 确认的 PaymentIntent 的客户端密钥。 | 3.1.0 |
paymentSummaryItems |
PaymentSummaryItem[] |
Apple Pay 支付面板中显示的明细项。 | 3.1.0 |
merchantIdentifier |
string |
为应用配置的 Apple 商户标识符。 | 3.1.0 |
countryCode |
string |
付款请求使用的两字母 ISO 3166-1 国家代码。 | 3.1.0 |
currency |
string |
付款请求使用的三字母 ISO 4217 货币代码。 | 3.1.0 |
requiredShippingContactFields |
('postalAddress' | 'phoneNumber' | 'emailAddress' | 'name')[] |
Apple Pay 必须收集的收货联系人字段。仅适用于 iOS。 | 4.1.0 |
allowedCountries |
string[] |
允许配送的国家或地区的两字母代码。仅适用于 iOS。 | 5.4.3 |
allowedCountriesErrorDescription |
string |
所选收货国家或地区不受支持时显示的消息。仅适用于 iOS。 | 5.4.3 |
requiredShippingContactFields 向 Apple Pay 请求邮寄地址、电话号码、电子邮箱或姓名。allowedCountries 会拒绝不在列表中的配送目的地国家或地区。
3. presentApplePay
const result = await Stripe.presentApplePay();
if (result.paymentResult === ApplePayEventsEnum.Completed) {
// 这里只更新界面。履行订单之前,请在服务器上通过 webhook 确认付款成功。
}
method presentApplePay()
展示由 createApplePay() 创建的 Apple Pay 请求。
presentApplePay() => Promise<{ paymentResult: ApplePayResultInterface; }>
type alias ApplePayResultInterface
ApplePayEventsEnum.Completed | ApplePayEventsEnum.Canceled | ApplePayEventsEnum.Failed | ApplePayEventsEnum.DidSelectShippingContact | ApplePayEventsEnum.DidCreatePaymentMethod
将 Canceled 视为取消,Failed 视为错误。
4. addListener
请在应用启动时注册监听器。参见事件监听器。
Stripe.addListener(ApplePayEventsEnum.Completed, () => {
console.log('ApplePayEventsEnum.Completed');
});
enum ApplePayEventsEnum
| 成员 | 值 |
|---|---|
Loaded |
'applePayLoaded' |
FailedToLoad |
'applePayFailedToLoad' |
Completed |
'applePayCompleted' |
Canceled |
'applePayCanceled' |
Failed |
'applePayFailed' |
DidSelectShippingContact |
'applePayDidSelectShippingContact' |
DidCreatePaymentMethod |
'applePayDidCreatePaymentMethod' |
5. updateApplePaySheet
在 iOS 上,DidSelectShippingContact 包含 contact 和 updateId。请重新计算总额,并用本次的 updateId 调用 updateApplePaySheet。JavaScript 在 25 秒内未响应时,原生面板会回退到最近一次已接受的金额明细;更新会保留当前的配送方式。
配送示例假定后端也会将 Intent 总额更新为 15.99 美元。updateApplePaySheet 只修改界面显示的汇总信息,不会更新后端的 PaymentIntent。
paymentSummaryItems 必须是数组。空数组([])表示确认当前选择,但保留已有明细;非空数组中的每一项都必须有非空字符串标签和有限数值金额。折扣可以为负数,但最终总额不能为负。如果输入无效,调用会被拒绝,但不会消耗待处理的更新,因此可在超时前修正后重试。只接受最近一次选择联系人的 updateId;重复或过期的更新会被拒绝。
Stripe.addListener(ApplePayEventsEnum.DidSelectShippingContact, async (data) => {
await Stripe.updateApplePaySheet({
updateId: data.updateId,
paymentSummaryItems: [
{ label: 'Product Name', amount: 10.99 },
{ label: 'Shipping', amount: 5.0 },
{ label: 'Total', amount: 15.99 },
],
});
});
method updateApplePaySheet(...)
在收货联系人回调后更新原生 Apple Pay 支付面板。仅适用于 iOS,Web 不支持此方法。
updateApplePaySheet(options: UpdateApplePaySheetOption) => Promise<void>
interface DidSelectShippingContact
Apple Pay 的收货联系人信息。
| 属性 | 类型 | 说明 | 起始版本 |
|---|---|---|---|
contact |
ShippingContact |
Apple Pay 中选择的收货联系人。 | 4.1.0 |
updateId |
string |
针对此回调传给 updateApplePaySheet() 的标识符。 |
4.1.0 |
interface PaymentSummaryItem
| 属性 | 类型 | 说明 | 起始版本 |
|---|---|---|---|
label |
string |
Apple Pay 支付面板中显示的明细项标签。 | 3.1.0 |
amount |
number |
以该币种主要单位表示的十进制金额,例如 10.99。 |
3.1.0 |
DidCreatePaymentMethod 包含了在 Apple 创建支付方式后所关联的送货信息。在成功完成支付之前,Apple 不会返回完整的地址信息。
interface DidCreatePaymentMethod
| 属性 | 类型 | 说明 | 起始版本 |
|---|---|---|---|
contact |
ShippingContact |
Apple Pay 付款方式关联的联系人。 | 4.1.0 |
interface ShippingContact
| 属性 | 类型 | 说明 | 起始版本 |
|---|---|---|---|
givenName |
string |
联系人的名字。仅适用于 Apple Pay。 | 4.1.0 |
familyName |
string |
联系人的姓氏。仅适用于 Apple Pay。 | 4.1.0 |
middleName |
string |
联系人的中间名。仅适用于 Apple Pay。 | 4.1.0 |
namePrefix |
string |
联系人的姓名前缀。仅适用于 Apple Pay。 | 4.1.0 |
nameSuffix |
string |
联系人的姓名后缀。仅适用于 Apple Pay。 | 4.1.0 |
nameFormatted |
string |
联系人的按格式排列的全名。仅适用于 Apple Pay。 | 4.1.0 |
phoneNumber |
string |
联系人的电话号码。仅适用于 Apple Pay。 | 4.1.0 |
nickname |
string |
联系人的昵称。仅适用于 Apple Pay。 | 4.1.0 |
street |
string |
联系人邮寄地址中的街道。仅适用于 Apple Pay。 | 4.1.0 |
city |
string |
联系人邮寄地址中的城市。仅适用于 Apple Pay。 | 4.1.0 |
state |
string |
联系人邮寄地址中的州或省。仅适用于 Apple Pay。 | 4.1.0 |
postalCode |
string |
联系人地址中的邮政编码。仅适用于 Apple Pay。 | 4.1.0 |
country |
string |
联系人地址中的国家或地区名称。仅适用于 Apple Pay。 | 4.1.0 |
isoCountryCode |
string |
联系人地址中的 ISO 国家代码。仅适用于 Apple Pay。 | 4.1.0 |
subAdministrativeArea |
string |
联系人地址中的次级行政区域。仅适用于 Apple Pay。 | 4.1.0 |
subLocality |
string |
联系人地址中城镇以下的地区。仅适用于 Apple Pay。 | 4.1.0 |
参考
import { ApplePayEventsEnum, Stripe } from '@capacitor-community/stripe';
(async () => {
try {
await Stripe.isApplePayAvailable();
} catch {
return;
}
await Stripe.addListener(ApplePayEventsEnum.Completed, () => {
console.log('ApplePayEventsEnum.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 Apple Pay
await Stripe.createApplePay({
paymentIntentClientSecret: paymentIntent,
paymentSummaryItems: [{
label: 'Product Name',
amount: 10.99
}],
merchantIdentifier: 'merchant.com.getcapacitor.stripe',
countryCode: 'US',
currency: 'USD',
});
// Present Apple Pay
const result = await Stripe.presentApplePay();
if (result.paymentResult === ApplePayEventsEnum.Completed) {
// Update UI only. Fulfill orders from a verified server webhook.
}
})();
