KR.onOrderUpdate()
Description
KR.onOrderUpdate() permet d'intercepter toutes les transactions nouvellement créées. Les transactions peuvent avoir un statut accepté, refusé, annulé ou remboursé . KR.onOrderUpdate() accepte en paramètre soit un callback, soit une promesse (Promise).
La callback reçoit un object avec 2 paramètes :
- KR : Référence à la librairie
- event : Object qui contient la transaction nouvellement créée.
Il vous est possible d'arrêter la chaine d'exécution en retournant false à la fin du traitement :
Valeur de retour | Comportement |
---|---|
false | l'exécution est interrompue. La gestion d'erreur, ou la redirection n'a pas lieu. |
true | l'exécution continue normalement lorsque la callback est executée. |
Structure de l'objet
{ clientAnswer: ClientAnswer hash: string hashAlgorithm: string hashKey: string serverDate: string form: any extra: any rawClientAnswer: string formId: string / null _type: 'string' _name: 'payment' / 'billingPartialPayment' / 'partialPaymentCancelledOrRefunded' }
Pour le champ name
:
Name | Description |
---|---|
payment | Transaction créée avec le statut acceptée ou refusée. |
billingPartialPayment | Transaction partielle créée avec le statut acceptée ou refusée. |
partialPaymentCancelledOrRefunded | Transaction partielle créée avec le statut annulée ou remboursée. |
Exemple d'une transaction partielle annulée
{ clientAnswer: ClientAnswer hash: string hashAlgorithm: string hashKey: string type: 'V4/Charge/ProcessPaymentAnswer' _name: 'partialPaymentCancelledOrRefunded' _cancelledTransactionUuid: string }
Exemples
Exemple 1
Ce code ne fait rien pour les transactions partielles mais si le paiement est complété par un paiemet par cartes, vous stoppez la redirection.
KR.onOrderUpdate(({_name, clientAnswer}) => { // Partial payment, do nothing if(_name === 'billingPartialPayment') return const { detailedStatus, paymentMethodType } = clientAnswer.transactions[0] // The merchant want to stop the redirection to the page defined in kr-post-url-success // and manage buyer interaction himself if(detailedStatus === 'AUTHORISED' && paymentMethodType === 'CARDS') return false })
Exemple 2
Ce code permet de récupérer certaines données (montant, ...) d'une transaction annulée.
KR.onOrderUpdate(({clientAnswer, _name,_cancelledTransactionUuid }) => { if(_name === 'partialPaymentCancelledOrRefunded'){ // transaction with uuid_cancelledTransactionUuid has been cancelled and see the log the cancelled amount const transaction = clientAnswer.transactions.find(t => t.uuid === _cancelledTransactionUuid) // in currencies with cents, the amount will be in cents console.log(`Transaction with amount ${transaction.amount} has been cancelled`) } })
Exemple 3
Ce code permet de récupérer certaines données (montant, devise, ... ) d'une transaction partielle acceptée.
KR.onOrderUpdate(({clientAnswer, _name }) => { if(_name === 'billingPartialPayment'){ // datat transaction const amount = clientAnswer.transactions[0].amount const currency = clientAnswer.transactions[0].currency // in currencies with cents, the amount will be in cents console.log(`Transaction with amount ${amount} ${currency} has been partial paid`) } })