javascriptstripe-paymentsstripe-payments-js

how to catch Uncaught (in promise) IntegrationError in STRIPE


I have a stripe integration that works. However there is one situation where i am unable generate/catch the error and based on the error i need to do some ajax action. My code is:

createCheckoutSession(formdata.get("plan"),formdata.get("email")).then(
    function(responseData) {
      // Call Stripe.js method to redirect to the new Checkout page
        stripe.redirectToCheckout({
            sessionId: responseData.id
        }).then(function (result) {
            console.log("hello2---"+result);
            if (result.error) {
              // Error scenario 1
              // If `redirectToCheckout` fails due to a browser or network
              // error, display the localized error message to your customer.
              console.log(result.error.message);
            }
        }).catch(function (error) {
            console.log("hello3---"+result);
            if (error) {
              // Error scenario 2
              // If the promise throws an error
              console.log("We are experiencing issues connecting to our"
              + " payments provider. " + error);
            }
        });
    }
);

I get an error "Uncaught (in promise) IntegrationError: stripe.redirectToCheckout: the provided sessionId is for a test mode Checkout Session, whereas Stripe.js was initialized with a live mode publishable key.". THIS error has been generated on purpose to test the above code.

However this error does not get outputted in either the THEN or CATCH part of the code, which i thought it should. It is an error that gets generated by https://js.stripe.com/v3/.

What i would like to do is that if such an error is generated then make an ajax call to do roll back a few db entries ? How do i go about doing it if i cant catch the error ? or i guess how do i go about catching the above error ?

just to add clarify the checkout session object gets created but obviously does not get transferred to stripe.com because of the above error.

thanks


Solution

  • put trycatch block

    createCheckoutSession(formdata.get("plan"), formdata.get("email")).then(
        function (responseData) {
            try {
                // Call Stripe.js method to redirect to the new Checkout page
                stripe.redirectToCheckout({
                    sessionId: responseData.id
                }).then(function (result) {
                    console.log("hello2---" + result);
                    if (result.error) {
                        // Error scenario 1
                        // If `redirectToCheckout` fails due to a browser or network
                        // error, display the localized error message to your customer.
                        console.log(result.error.message);
                    }
                }).catch(function (error) {
                    console.log("hello3---" + result);
                    if (error) {
                        // Error scenario 2
                        // If the promise throws an error
                        console.log("We are experiencing issues connecting to our"
                            + " payments provider. " + error);
                    }
                });
            }
            catch (e) {
                console.log("inner error", e);
            }
        }
    ).catch((e) => {
        console.log('e: ', e);
    })