Écouteurs d’événements
Utilisez les événements de résultat comme parcours de résultat par défaut. Enregistrez les écouteurs de résultat au niveau de l’application une seule fois par démarrage de l’application JavaScript, aussi tôt que possible — par exemple dans main.ts, un initialiseur d’application ou un service singleton initialisé au démarrage — et avant de présenter l’interface Stripe.
import {
ApplePayEventsEnum,
GooglePayEventsEnum,
PaymentFlowEventsEnum,
PaymentSheetEventsEnum,
Stripe,
} from '@capacitor-community/stripe';
await Promise.all([
Stripe.addListener(PaymentSheetEventsEnum.Completed, () => handleCompleted()),
Stripe.addListener(PaymentSheetEventsEnum.Canceled, () => handleCanceled()),
Stripe.addListener(PaymentSheetEventsEnum.Failed, (error) => handleFailed(error)),
]);
interface PluginListenerHandle
| Propriété | Type |
|---|---|
remove |
() => Promise<void> |
Recréation de l’Activity Android
C’est particulièrement important sur Android, où l’Activity et l’environnement JavaScript peuvent être recréés alors que l’interface Stripe est ouverte. Le nouvel environnement JavaScript doit enregistrer ses écouteurs au démarrage.
La Promise JavaScript d’origine et le PluginCall Capacitor ne peuvent pas être restaurés. Si Stripe fournit le résultat natif après la recréation, le plugin conserve l’événement de résultat correspondant jusqu’à ce qu’un écouteur soit disponible. Cela concerne les événements Completed, Canceled et Failed de PaymentSheet, PaymentFlow et Google Pay, ainsi que l’événement Created de PaymentFlow.
Si l’appel d’origine existe encore, le comportement reste identique : la Promise se termine normalement et l’événement est transmis sans être conservé. Ce mécanisme de secours transmet un résultat natif en mémoire ; il ne constitue pas un stockage persistant et ne garantit pas la récupération après l’arrêt du processus par le système.
Gardez les écouteurs de résultat au niveau de l’application enregistrés pendant toute la durée de vie de l’environnement JavaScript. Ne les ajoutez pas dans le gestionnaire d’un bouton pour les supprimer au démontage d’une page si vous avez encore besoin du résultat du paiement après une recréation Android.
Événements PaymentSheet
method addListener(PaymentSheetEventsEnum.Loaded, ...)
Émis lorsque PaymentSheet est créé et prêt à être présenté.
addListener(eventName: PaymentSheetEventsEnum.Loaded, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentSheetEventsEnum.FailedToLoad, ...)
Émis lorsque PaymentSheet n’a pas pu être créé.
addListener(eventName: PaymentSheetEventsEnum.FailedToLoad, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
method addListener(PaymentSheetEventsEnum.Completed, ...)
Émis lorsque le client termine PaymentSheet.
addListener(eventName: PaymentSheetEventsEnum.Completed, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentSheetEventsEnum.Canceled, ...)
Émis lorsque le client ferme PaymentSheet.
addListener(eventName: PaymentSheetEventsEnum.Canceled, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentSheetEventsEnum.Failed, ...)
Émis lorsque PaymentSheet se termine par une erreur.
addListener(eventName: PaymentSheetEventsEnum.Failed, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
enum PaymentSheetEventsEnum
| Membre | Valeur |
|---|---|
Loaded |
'paymentSheetLoaded' |
FailedToLoad |
'paymentSheetFailedToLoad' |
Completed |
'paymentSheetCompleted' |
Canceled |
'paymentSheetCanceled' |
Failed |
'paymentSheetFailed' |
Déroulement habituel de PaymentSheet :
- Enregistrez les écouteurs de résultat au démarrage.
- Appelez
createPaymentSheet(). - Attendez
Loaded, ou traitezFailedToLoad. - Appelez
presentPaymentSheet(). - Recevez
Completed,CanceledouFailed.
Canceled signifie que le client a fermé la feuille. Traitez-le comme une annulation, pas comme une erreur levée. Failed et FailedToLoad contiennent une chaîne d’erreur. N’exécutez pas une commande sur la seule base de l’événement client ; vérifiez l’état de l’Intent sur votre serveur à l’aide d’un webhook.
Événements PaymentFlow
method addListener(PaymentFlowEventsEnum.Loaded, ...)
Émis lorsque PaymentFlow est créé et prêt à être présenté.
addListener(eventName: PaymentFlowEventsEnum.Loaded, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentFlowEventsEnum.FailedToLoad, ...)
Émis lorsque PaymentFlow n’a pas pu être créé.
addListener(eventName: PaymentFlowEventsEnum.FailedToLoad, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
method addListener(PaymentFlowEventsEnum.Opened, ...)
Émis lorsque l’interface PaymentFlow est présentée.
addListener(eventName: PaymentFlowEventsEnum.Opened, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentFlowEventsEnum.Completed, ...)
Émis après la confirmation des informations de paiement recueillies.
addListener(eventName: PaymentFlowEventsEnum.Completed, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentFlowEventsEnum.Canceled, ...)
Émis lorsque le client ferme PaymentFlow.
addListener(eventName: PaymentFlowEventsEnum.Canceled, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(PaymentFlowEventsEnum.Failed, ...)
Émis lorsque la collecte ou la confirmation de PaymentFlow échoue.
addListener(eventName: PaymentFlowEventsEnum.Failed, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
method addListener(PaymentFlowEventsEnum.Created, ...)
Émis après la collecte des informations de paiement et avant la confirmation. Le numéro de carte ne contient que les quatre derniers chiffres.
addListener(eventName: PaymentFlowEventsEnum.Created, listenerFunc: (info: { cardNumber: string; }) => void) => Promise<PluginListenerHandle>
enum PaymentFlowEventsEnum
| Membre | Valeur |
|---|---|
Loaded |
'paymentFlowLoaded' |
FailedToLoad |
'paymentFlowFailedToLoad' |
Opened |
'paymentFlowOpened' |
Created |
'paymentFlowCreated' |
Completed |
'paymentFlowCompleted' |
Canceled |
'paymentFlowCanceled' |
Failed |
'paymentFlowFailed' |
Déroulement habituel de PaymentFlow :
- Enregistrez les écouteurs de résultat au démarrage.
- Appelez
createPaymentFlow(). - Attendez
Loaded, ou traitezFailedToLoad. - Appelez
presentPaymentFlow(). - Recevez
Opened, puisCreatedavec{ cardNumber }, ouCanceled. - Appelez
confirmPaymentFlow(). - Recevez
Completed,CanceledouFailed.
Événements Apple Pay
method addListener(ApplePayEventsEnum.Loaded, ...)
Émis lorsque la demande Apple Pay est prête à être présentée.
addListener(eventName: ApplePayEventsEnum.Loaded, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(ApplePayEventsEnum.FailedToLoad, ...)
Émis lorsque la demande Apple Pay n’a pas pu être créée.
addListener(eventName: ApplePayEventsEnum.FailedToLoad, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
method addListener(ApplePayEventsEnum.Completed, ...)
Émis après la réussite d’Apple Pay.
addListener(eventName: ApplePayEventsEnum.Completed, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(ApplePayEventsEnum.Canceled, ...)
Émis lorsque le client annule Apple Pay.
addListener(eventName: ApplePayEventsEnum.Canceled, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(ApplePayEventsEnum.Failed, ...)
Émis lorsqu’Apple Pay échoue.
addListener(eventName: ApplePayEventsEnum.Failed, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
method addListener(ApplePayEventsEnum.DidSelectShippingContact, ...)
iOS uniquement. Émis lorsque le client sélectionne un contact de livraison. Utilisez le updateId fourni avec updateApplePaySheet().
addListener(eventName: ApplePayEventsEnum.DidSelectShippingContact, listenerFunc: (data: DidSelectShippingContact) => void) => Promise<PluginListenerHandle>
method addListener(ApplePayEventsEnum.DidCreatePaymentMethod, ...)
iOS uniquement. Émis après la création du Stripe PaymentMethod par Apple Pay.
addListener(eventName: ApplePayEventsEnum.DidCreatePaymentMethod, listenerFunc: (data: DidCreatePaymentMethod) => void) => Promise<PluginListenerHandle>
enum ApplePayEventsEnum
| Membre | Valeur |
|---|---|
Loaded |
'applePayLoaded' |
FailedToLoad |
'applePayFailedToLoad' |
Completed |
'applePayCompleted' |
Canceled |
'applePayCanceled' |
Failed |
'applePayFailed' |
DidSelectShippingContact |
'applePayDidSelectShippingContact' |
DidCreatePaymentMethod |
'applePayDidCreatePaymentMethod' |
DidSelectShippingContact contient contact et updateId. Sur iOS, appelez updateApplePaySheet avec cet updateId et les paymentSummaryItems mis à jour. Si JavaScript ne répond pas, la feuille native revient aux derniers éléments acceptés au bout de 25 secondes, en conservant les modes de livraison actuels. updateApplePaySheet n’est pas implémenté sur Android ni sur le Web.
DidCreatePaymentMethod contient le contact de livraison. Apple ne renvoie l’adresse complète qu’après un paiement réussi.
Événements Google Pay
method addListener(GooglePayEventsEnum.Loaded, ...)
Émis lorsque la demande Google Pay est prête à être présentée.
addListener(eventName: GooglePayEventsEnum.Loaded, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(GooglePayEventsEnum.FailedToLoad, ...)
Émis lorsque la demande Google Pay n’a pas pu être créée.
addListener(eventName: GooglePayEventsEnum.FailedToLoad, listenerFunc: (error: string) => void) => Promise<PluginListenerHandle>
method addListener(GooglePayEventsEnum.Completed, ...)
Émis après la réussite de Google Pay.
addListener(eventName: GooglePayEventsEnum.Completed, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(GooglePayEventsEnum.Canceled, ...)
Émis lorsque le client annule Google Pay.
addListener(eventName: GooglePayEventsEnum.Canceled, listenerFunc: () => void) => Promise<PluginListenerHandle>
method addListener(GooglePayEventsEnum.Failed, ...)
Émis lorsque Google Pay échoue.
addListener(eventName: GooglePayEventsEnum.Failed, listenerFunc: () => void) => Promise<PluginListenerHandle>
enum GooglePayEventsEnum
| Membre | Valeur |
|---|---|
Loaded |
'googlePayLoaded' |
FailedToLoad |
'googlePayFailedToLoad' |
Completed |
'googlePayCompleted' |
Canceled |
'googlePayCanceled' |
Failed |
'googlePayFailed' |
Google Pay est disponible sur Android et sur le Web. Il n’est pas implémenté sur iOS.