From 4f9fa8a28293ce9a47026117c86443e7ba2f831e Mon Sep 17 00:00:00 2001 From: Praful Koppalkar Date: Tue, 16 Apr 2024 23:48:30 +0530 Subject: [PATCH] fix(paymenthelpers, paymentelement): promise being unresolved for mandate payment For end users it looked like Promise is being unresolved as the button was being stuck in the loading state. --- src/PaymentElement.res | 3 ++- src/Utilities/PaymentHelpers.res | 39 +++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/PaymentElement.res b/src/PaymentElement.res index 756b3e19a..4f5048b99 100644 --- a/src/PaymentElement.res +++ b/src/PaymentElement.res @@ -398,7 +398,8 @@ let make = ( React.useEffect(() => { let evalMethodsList = () => switch methodslist { - | SemiLoaded | Loaded(_) => handlePostMessage([("ready", true->JSON.Encode.bool)]) + | SemiLoaded | LoadError(_) | Loaded(_) => + handlePostMessage([("ready", true->JSON.Encode.bool)]) | _ => () } if !displaySavedPaymentMethods { diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index 83937b42d..3042deedd 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -976,26 +976,39 @@ let usePaymentIntent = (optLogger: option, paymentType: p } switch list { + | LoadError(data) | Loaded(data) => let paymentList = data->getDictFromJson->PaymentMethodsRecord.itemToObjMapper let mandatePaymentType = paymentList.payment_type->PaymentMethodsRecord.paymentTypeToStringMapper - switch paymentList.mandate_payment { - | Some(_) => - switch paymentType { - | Card - | Gpay - | Applepay - | KlarnaRedirect - | Paypal - | BankDebits => - intentWithMandate(mandatePaymentType) - | _ => intentWithoutMandate(mandatePaymentType) + if paymentList.payment_methods->Array.length > 0 { + switch paymentList.mandate_payment { + | Some(_) => + switch paymentType { + | Card + | Gpay + | Applepay + | KlarnaRedirect + | Paypal + | BankDebits => + intentWithMandate(mandatePaymentType) + | _ => intentWithoutMandate(mandatePaymentType) + } + | None => intentWithoutMandate(mandatePaymentType) } - | None => intentWithoutMandate(mandatePaymentType) + } else { + postFailedSubmitResponse( + ~errortype="payment_methods_empty", + ~message="Payment Failed. Try again!", + ) + Console.warn("Please enable atleast one Payment method.") } | SemiLoaded => intentWithoutMandate("") - | _ => () + | _ => + postFailedSubmitResponse( + ~errortype="payment_methods_loading", + ~message="Please wait. Try again!", + ) } | None => postFailedSubmitResponse(