stripe-archive / accept-a-card-payment

Learn how to accept a basic card payment on web, iOS, Android

Home Page:https://stripe.com/docs/payments/accept-a-payment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uncaught (in promise) TypeError: Cannot read property 'json' of undefined

DDholiyan opened this issue · comments

Bug report

script.js:117 Uncaught (in promise) TypeError: Cannot read property 'json' of undefined

Describe the bug

Below line gives above error

return result.json();

Hey @DDholiyan This is almost always because of the .env file not being correctly configured. Can you share the output of the server log when you get this error?

This is on most of the response. See below declined response.
{
"error": {
"code": "card_declined",
"decline_code": "live_mode_test_card",
"doc_url": "https://stripe.com/docs/error-codes/card-declined",
"message": "Your card was declined. Your request was in live mode, but used a known test card.",
"type": "card_error"
}
}

Console:- Uncaught (in promise) TypeError: Cannot read property 'json' of undefined
at script.js line no 118

stripe
.createPaymentMethod("card", card)
.then(function(result) {
if (result.error) {
showError(result.error.message);
} else {
orderData.paymentMethodId = result.paymentMethod.id;

        return fetch("pay.php", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(orderData)
        });
    }
})
.then(function(result) {
    return result.json(); // Line no 118
})

That means the sample was configured with live mode API keys to take real payments, but a test card was used. Test cards only work with test mode API keys. Try using your test mode API keys.

I know what is this mean. I just want to tell you that this error comes every time we got response from stripe.