0

Hy am getting this error from last 3 days in this code I think may be all the things are working fine but after payment done when "confirm_payment()" function called I get this error continue in stripe payment gateway using React Native here is my code can anyone help me to get rid out of this error -

when "confirm_payment()" function is called i get this response in my terminal i tried all the things but nothing is working for me.

error : - 
      {"code": "Failed", "declineCode": null, "localizedMessage": "There was an 
       unexpected error -- try again in a few seconds", "message": "There was an 
       unexpected error -- try again in a few seconds", "stripeErrorCode": null, 
       "type": null} 


const { initPaymentSheet, presentPaymentSheet, confirmPaymentSheetPayment } = useStripe();

useEffect(() => {
    initStripe({ publishableKey: "pk_test_51JUm2TSHumMvEDNY3mFcrddvlTqUIzxoHqyIuCT2x8ExzZcbcTLB9kAZWY1eT9ZJnIeBSHtxtkNCq5QjcDf2e2ge00knO9Qx1R" })
}, [])

async function booknow(item) {
    if (item.sm_booking_type == 1) {
        navigation.navigate('DriverServicesScreen', { user_data: user_data_1, item: item })
    } else {

        const additional_data = await AsyncStorage.getItem( 'additional_data');
        var num = JSON.parse(additional_data);
        var dataOk = new FormData();
        dataOk.append('amount', item.sm_price * 100);
        await axios(API_URL, {
            method: 'POST',
            headers: { Accept: 'application/x-www-form-urlencoded', 'Content-type': 'multipart/form-data', timeout: 180000 },
            data: dataOk,
        }).then((result) => {
            if (result['data'].code == 200) {
                paymentInitializeFunction(result['data'].client_secret, result['data'].ephemeralKey, result['data'].customer)
                console.log(" ~ file: Api.js ~ line 16 ~ API ~ result", JSON.stringify(result['data']))
            } else {
                console.log(" ~ file: ServicesScreen.js:167 ~ booknow ~ result:", result)
            }
        }).catch((err) => {
            console.log(" ~ file: Api.js ~ line 24 ~ API ~ err", err)
        })
    }
}

const paymentInitializeFunction = async (client_secret, ephemeralKey, customer) => {

    await initPaymentSheet({
        merchantDisplayName: "Example, Inc.",
        customerId: customer,
        customerEphemeralKeySecret: ephemeralKey,
        paymentIntentClientSecret: client_secret,
        customFlow: true,
        style: 'automatic',
        // allowsDelayedPaymentMethods: true,
        defaultBillingDetails: {
            name: 'Jane Doe',
            email: 'foo@bar.com',
            phone: '555-555-555',
        }
    }).then((res) => {
        console.log(" ~ file: ServicesScreen.js:195 ~ paymentInitializeFunction ~ error1:", res)
    }).catch((res) => {
        console.log(" ~ file: ServicesScreen.js:197 ~ paymentInitializeFunction ~ res:", res)
    })

    await presentPaymentSheet({
        paymentIntentClientSecret: client_secret,
        clientSecret: client_secret,
        customerId: customer,
        customerEphemeralKeySecret: ephemeralKey,
        customFlow: true,
        style: 'automatic',
        // allowsDelayedPaymentMethods: true,
        defaultBillingDetails: {
            name: 'Jane Doe',
            email: 'foo@bar.com',
            phone: '555-555-555',
        }
    }).then((res) => {
        console.log(" ~ file: ServicesScreen.js:144 ~ paymentInitializeFunction ~ res:", res)
        if (res.hasOwnProperty('error')) {
            Error_Alert('The payment has been cancelled.');
        }
        else {
            confirm_payment()
        }
    }).catch((err) => {
        console.log(" ~ file: ServicesScreen.js:268 ~ paymentInitializeFunction ~ err:", err)
        Error_Alert('Something went wrong, Please try again later.');
    })

}

async function confirm_payment() {

    const { error } = await confirmPaymentSheetPayment();
    console.log(" ~ file: ServicesScreen.js:231 ~ confirm_payment ~ error:", error)

    if (error) {
        Alert.alert(`Error code: ${error.code}`, error.message);
    } else {
        Alert.alert('Success', 'The payment was confirmed successfully!');
    }
}
varun
  • 89
  • 6
  • 1
    First, presentPaymentSheet() doesn't take any arguments other than `timeout`, though I don't believe this breaks the SDK here. Second, you are combining await with .then() which is generally superfluous and not recommended and can lead to weird behavior. Lastly, have you logged out your ephemeral key, client secret, and customer ID to ensure they are all aligned and can you confirm exactly which line throws the error? – bismarck Apr 18 '23 at 16:07
  • Actually i am new to react native so i don't not may things about react native can help me to solve this and yes i think confirmPaymentSheetPayment() giving the error @bismarck – varun Apr 18 '23 at 16:13

0 Answers0