跳转至正文

快速入门

GitHub

你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:

终端窗口
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下 prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-pay` plugin in my project.

如果你更倾向于手动配置,请运行以下命令安装插件,并参考下方的各平台特定说明:

终端窗口
bun add @capgo/capacitor-pay
bunx cap sync

导入

导入
import { Pay } from '@capgo/capacitor-pay';

快速入门

快速入门
import { Pay } from '@capgo/capacitor-pay';
const result = await Pay.isPayAvailable();
console.log(result);

示例:requestPayment

示例:requestPayment
import { Pay } from '@capgo/capacitor-pay';
const result = await Pay.requestPayment({
apple: {
merchantIdentifier: 'merchant-identifier-123',
countryCode: 'US',
currencyCode: 'USD',
paymentSummaryItems: [
{
label: 'Hello from Capacitor',
amount: 'amount',
},
],
supportedNetworks: ['AmEx'],
},
});
// The result holds sensitive values: use it without logging it.
import { Pay } from '@capgo/capacitor-pay';
await Pay.updateApplePayShipping({
selectionId: 'selection-id-123',
paymentSummaryItems: [
{
label: 'Hello from Capacitor',
amount: 'amount',
},
],
});
  • 监听器: 在页面卸载时移除句柄。
  • 原生同步: 运行 bunx cap sync 在插件升级后。
  • 单一事实来源: 信任 src/definitions.ts 位于 GitHub 仓库中。

检查当前平台是否支持原生支付。 iOS 端会检查 Apple Pay,Android 端会检查 Google Pay。

import { Pay } from '@capgo/capacitor-pay';
const result = await Pay.isPayAvailable();
console.log(result);

弹出当前平台的原生支付界面。 在 iOS 上需提供 Apple Pay 配置,在 Android 上需提供 Google Pay 配置。

该 Promise 是两个平台的完成回调路径。

import { Pay } from '@capgo/capacitor-pay';
const result = await Pay.requestPayment({
apple: {
merchantIdentifier: 'merchant-identifier-123',
countryCode: 'US',
currencyCode: 'USD',
paymentSummaryItems: [
{
label: 'Hello from Capacitor',
amount: 'amount',
},
],
supportedNetworks: ['AmEx'],
},
});
// The result holds sensitive values: use it without logging it.
export interface PayAvailabilityOptions {
apple?: ApplePayAvailabilityOptions;
google?: GooglePayAvailabilityOptions;
}
export interface PayAvailabilityResult {
available: boolean;
platform: PayPlatform;
apple?: ApplePayAvailabilityResult;
google?: GooglePayAvailabilityResult;
}
export interface PayPaymentOptions {
apple?: ApplePayPaymentOptions;
google?: GooglePayPaymentOptions;
}
export type PayPaymentResult = ApplePayRequestPaymentResult | GooglePayRequestPaymentResult;

ApplePayAvailabilityOptions

“ApplePayAvailabilityOptions” 章节
export interface ApplePayAvailabilityOptions {
/**
* Optional list of payment networks you intend to use.
* Passing networks determines the return value of `canMakePaymentsUsingNetworks`.
*/
supportedNetworks?: ApplePayNetwork[];
}

GooglePayAvailabilityOptions

“GooglePayAvailabilityOptions” 章节
export interface GooglePayAvailabilityOptions {
/**
* Environment used to construct the Google Payments client. Defaults to `'test'`.
*/
environment?: GooglePayEnvironment;
/**
* Raw `IsReadyToPayRequest` JSON as defined by the Google Pay API.
* Supply the card networks and auth methods you intend to support at runtime.
*
* @see https://developers.google.com/pay/api/android/reference/request-objects#IsReadyToPayRequest
*/
isReadyToPayRequest?: GooglePayIsReadyToPayRequest;
}
export type PayPlatform = 'ios' | 'android' | 'web';

ApplePayAvailabilityResult

“ApplePayAvailabilityResult” 章节
export interface ApplePayAvailabilityResult {
/**
* Indicates whether the device can make Apple Pay payments in general.
*/
canMakePayments: boolean;
/**
* Indicates whether the device can make Apple Pay payments with the supplied networks.
*/
canMakePaymentsUsingNetworks: boolean;
}

GooglePayAvailabilityResult

“GooglePayAvailabilityResult” 章节
export interface GooglePayAvailabilityResult {
/**
* Whether the user is able to provide payment information through the Google Pay payment sheet.
*/
isReady: boolean;
/**
* The current user's ability to pay with one or more of the payment methods specified in `IsReadyToPayRequest.allowedPaymentMethods`.
*
* This property only exists if `IsReadyToPayRequest.existingPaymentMethodRequired` was set to `true`. The property value will always be `true` if the request is configured for a test environment.
*/
paymentMethodPresent: boolean | undefined;
}
export interface ApplePayPaymentOptions {
/**
* Merchant identifier created in the Apple Developer portal.
*/
merchantIdentifier: string;
/**
* Two-letter ISO 3166 country code.
*/
countryCode: string;
/**
* Three-letter ISO 4217 currency code.
*/
currencyCode: string;
/**
* Payment summary items displayed in the Apple Pay sheet.
*/
paymentSummaryItems: ApplePaySummaryItem[];
/**
* Card networks to support.
*/
supportedNetworks: ApplePayNetwork[];
/**
* Merchant payment capabilities. Defaults to ['3DS'] when omitted.
*/
merchantCapabilities?: ApplePayMerchantCapability[];
/**
* Contact fields that must be supplied for shipping.
*/
requiredShippingContactFields?: ApplePayContactField[];
/**
* Contact fields that must be supplied for billing.
*/
requiredBillingContactFields?: ApplePayContactField[];
/**
* Controls the shipping flow presented to the user.
*/
shippingType?: ApplePayShippingType;
/**
* Optional ISO 3166 country codes where the merchant is supported.
*/
supportedCountries?: string[];
/**
* Optional opaque application data passed back in the payment token.
*/
applicationData?: string;
/**
* Recurring payment configuration (iOS 16+).
*/
recurringPaymentRequest?: ApplePayRecurringPaymentRequest;
}
export interface GooglePayPaymentOptions {
/**
* Environment used to construct the Google Payments client. Defaults to `'test'`.
*/
environment?: GooglePayEnvironment;
/**
* Raw `PaymentDataRequest` JSON as defined by the Google Pay API.
* Provide transaction details, merchant info, and tokenization parameters.
*
* @see https://developers.google.com/pay/api/android/reference/request-objects#PaymentDataRequest
*/
paymentDataRequest: GooglePayPaymentDataRequest;
}

ApplePayRequestPaymentResult

“ApplePayRequestPaymentResult” 章节
export interface ApplePayRequestPaymentResult {
/**
* Platform that resolved the payment request.
*/
platform: 'ios';
/**
* Apple Pay payment payload.
*/
apple: ApplePayPaymentResult;
}

本页面由该插件的 src/definitions.ts当上游的公共 API 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 来规划 dashboard 和 API 操作,请将其连接至 使用 @capgo/capacitor-pay 用于“使用 @capgo/capacitor-pay”中的原生能力 API 概览 关于 API 概览中的实现细节, 简介 有关实现细节,请参阅 Introduction, API 密钥 有关 API Keys 的实现细节,请参阅该章节,以及 设备 有关 Devices 的实现细节,请参阅该章节。