@jackobo/capacitor-apple-pay
v1.0.3
Published
Capacitor plugin for Apple Pay
Downloads
109
Readme
@jackobo/capacitor-apple-pay
Capacitor plugin for Apple Pay
Install
npm install @jackobo/capacitor-apple-pay
npx cap sync
Usage
async function payWithApplePay() {
await CapacitorApplePay.addListener('validateMerchant', async (event: ValidateMerchantEvent) => {
const merchantSession = await validateMerchantWithYourServer(event.validationURL);
await CapacitorApplePay.completeMerchantValidation({
merchantSession: merchantSession
})
});
await CapacitorApplePay.addListener('authorizePayment', async (event: AuthorizePaymentEvent) => {
try {
const success = await processPaymentWithYourServer(event.payment);
if(success) {
await CapacitorApplePay.paymentAuthorizationSuccess();
} else {
await CapacitorApplePay.paymentAuthorizationFail();
}
} finally {
await CapacitorApplePay.removeAllListeners();
}
}
await CapacitorApplePay.addListener('cancel', async () => {
await CapacitorApplePay.removeAllListeners();
});
await CapacitorApplePay.startPayment(paymentRequestObject);
}
API
canMakePayments()
addListener('validateMerchant', ...)
addListener('authorizePayment', ...)
addListener('cancel', ...)
startPayment(...)
completeMerchantValidation(...)
paymentAuthorizationSuccess()
paymentAuthorizationFail()
removeAllListeners()
- Interfaces
- Type Aliases
canMakePayments()
canMakePayments() => Promise<CanMakePaymentsResult>
Checks if Apple Pay is available
Returns: Promise<CanMakePaymentsResult>
addListener('validateMerchant', ...)
addListener(eventName: 'validateMerchant', handler: ValidateMerchantEventHandler) => Promise<PluginListenerHandle>
Subscribe to the validateMerchant event
| Param | Type |
| --------------- | ------------------------------------------------------------------------------------- |
| eventName
| 'validateMerchant' |
| handler
| ValidateMerchantEventHandler |
Returns: Promise<PluginListenerHandle>
addListener('authorizePayment', ...)
addListener(eventName: 'authorizePayment', handler: AuthorizePaymentEventHandler) => Promise<PluginListenerHandle>
Subscribe to the authorizePayment event
| Param | Type |
| --------------- | ------------------------------------------------------------------------------------- |
| eventName
| 'authorizePayment' |
| handler
| AuthorizePaymentEventHandler |
Returns: Promise<PluginListenerHandle>
addListener('cancel', ...)
addListener(eventName: 'cancel', handler: CancelPaymentEventHandler) => Promise<PluginListenerHandle>
Subscribe to the cancel event (when the payment is canceled by the user by closing Apple Pay payment sheet)
| Param | Type |
| --------------- | ------------------------------------------------------------------------------- |
| eventName
| 'cancel' |
| handler
| CancelPaymentEventHandler |
Returns: Promise<PluginListenerHandle>
startPayment(...)
startPayment(request: PaymentRequest) => Promise<void>
Starts the payment process
| Param | Type |
| ------------- | --------------------------------------------------------- |
| request
| PaymentRequest |
completeMerchantValidation(...)
completeMerchantValidation(request: CompleteMerchantValidationRequest) => Promise<void>
You call this from your code after you performed merchant validation with your server.
| Param | Type |
| ------------- | ----------------------------------------------------------------------------------------------- |
| request
| CompleteMerchantValidationRequest |
paymentAuthorizationSuccess()
paymentAuthorizationSuccess() => Promise<void>
You call this from your code after the payment processing was successful
paymentAuthorizationFail()
paymentAuthorizationFail() => Promise<void>
You call this from your code after when the payment processing failed
removeAllListeners()
removeAllListeners() => Promise<void>
Unsubscribe from all events
Interfaces
CanMakePaymentsResult
Response returned by the canMakePayments method
| Prop | Type | Description |
| --------------------- | -------------------- | ------------------------------------ |
| canMakePayments
| boolean | It is true if Apple Pay is available |
PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| remove
| () => Promise<void> |
ValidateMerchantEvent
The validateMerchant event payload
| Prop | Type | Description |
| ------------------- | ------------------- | ----------------------------------------------------------------------------------------- |
| validationURL
| string | The URL that your backend needs to use in order to perform merchant validation with Apple |
AuthorizePaymentEvent
authorizePayment event payload
| Prop | Type |
| ------------- | ---------------- |
| payment
| any |
PaymentRequest
The payload for startPayment method
| Prop | Type |
| -------------------------- | ------------------------------------------------------------------- |
| merchantId
| string |
| countryCode
| string |
| currencyCode
| string |
| supportedNetworks
| PaymentNetwork[] |
| merchantCapabilities
| MerchantCapability[] |
| total
| PaymentRequestTotal |
PaymentRequestTotal
| Prop | Type | Description |
| ------------ | --------------------------------- | ---------------------------------------------------------------------------- |
| label
| string | The text that appears next to the amount. Usually this is your merchant name |
| amount
| string | The amount formatted as string yourAmount.toFixed(2) |
| type
| 'final' | 'pending' | Defaults to final if not specified |
CompleteMerchantValidationRequest
completeMerchantValidation method request
| Prop | Type | Description |
| --------------------- | ------------------- | ------------------------------------------------------------------------------------------ |
| merchantSession
| string | Contains the merchant session obtained from your server in the merchant validation process |
Type Aliases
ValidateMerchantEventHandler
validateMerchant event callback
(event: ValidateMerchantEvent): void
AuthorizePaymentEventHandler
authorizePayment event callback
(event: AuthorizePaymentEvent): void
CancelPaymentEventHandler
cancel event callback
(): void
PaymentNetwork
All available payment networks
'amex' | 'bancomat' | 'bancontact' | 'cartesBancaires' | 'chinaUnionPay' | 'dankort' | 'discover' | 'eftpos' | 'electron' | 'elo' | 'girocard' | 'interac' | 'jcb' | 'mada' | 'maestro' | 'masterCard' | 'mir' | 'privateLabel' | 'visa' | 'vPay'
MerchantCapability
Merchant capabilities
'supports3DS' | 'supportsCredit' | 'supportsDebit' | 'supportsEMV'