Capacitor pluging for Google Pay Example:
npm install @jackobo/capacitor-google-pay
npm install @types/googlepay --save-dev
npx cap sync
import {CapacitorGooglePay, PaymentErrorStatusCodeEnum} from "@jackobo/capacitor-google-pay";
async function pay() {
await CapacitorGooglePay.initializeClient({
environment: "TEST" //or PRODUCTION
});
const {result} = await CapacitorGooglePay.isReadyToPay(isReadyToPayRequest);
if (!result) { // it means Google Pay is not available
return;
}
try {
const paymentData = await CapacitorGooglePay.startPayment(startPaymentRequest)
await callYourServer(paymentData.paymentMethodData)
} catch (err) {
if (err?.code !== PaymentErrorStatusCodeEnum.Canceled) {
//show error to user here
console.log(err?.message);
}
}
}
initializeClient(options: InitializeClientOptions) => Promise<void>
Initialize the Google Pay PaymentsClient object. You must call this at least once before calling any other methods of the plugin.
Param | Type |
---|---|
options |
InitializeClientOptions |
isReadyToPay(request: IsReadyToPayRequest) => Promise<IsReadyToPayResponse>
Checks if the Google Pay is available.
Param | Type |
---|---|
request |
IsReadyToPayRequest |
Returns: Promise<IsReadyToPayResponse>
startPayment(request: StartPaymentRequest) => Promise<StartPaymentResponse>
Starts the payment process
Param | Type |
---|---|
request |
PaymentDataRequest |
Returns: Promise<PaymentData>
It is used to initialize a PaymentClient object. This is basically a google.payments.api.PaymentOptions object but with the paymentDataCallbacks property omitted. Payment callbacks are not supported. See documentation https://developers.google.com/pay/api/web/reference/request-objects#PaymentOptions
Omit<google.payments.api.PaymentOptions, 'paymentDataCallbacks'>
Construct a type with the properties of T except for those in type K.
From T, pick a set of properties whose keys are in the union K
{
[P in K]: T[P];
}
Exclude from T those types that are assignable to U
T extends U ? never : T
This is a google.payments.api.IsReadyToPayRequest object. See documentation https://developers.google.com/pay/api/web/reference/request-objects#IsReadyToPayRequest
google.payments.api.IsReadyToPayRequest
This is a google.payments.api.IsReadyToPayResponse. See documentation https://developers.google.com/pay/api/web/reference/response-objects#IsReadyToPayResponse
google.payments.api.IsReadyToPayResponse
This is a google.payments.api.PaymentDataRequest object. See documentation https://developers.google.com/pay/api/web/reference/request-objects#PaymentDataRequest
google.payments.api.PaymentDataRequest
This is a google.payments.api.PaymentData object. See documentation https://developers.google.com/pay/api/web/reference/response-objects#PaymentData
google.payments.api.PaymentData