English 中文(简体)
How to fix a Firebase Cloud Functions Bad Request in React-Native?
原标题:

I m fairly new to Firebase cloud functions and I ve been trying to integrate Stripe into my RN project using FB but keep getting a bad request response. I ll see a few things when I try to run this:

  1. Request is missing body.
  2. Invalid request, unable to process.
  3. Bad Request (400).

Does anyone know where I should go from here?

In my index.js file:

exports.payWithStripe = functions.https.onCall(async (data, response) => {

try {
    const paymentIntent = await stripe.paymentIntents.create({
        data: {
            amount: 100,
            currency: "usd",
            payment_method_types: ["card"]
        }
    });

    const clientSecret = paymentIntent.client_secret;

    response.json({
        clientSecret: clientSecret,
    })
} catch (e) {
    console.log(e.message);
    response.json({ error: e.message });
}

})

On the app side:

const fetchPaymentIntentClientSecret = async () => {
    const response = await fetch(`${FUNCTION_URL}/create-payment-intent`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
    });

    const { clientSecret, error } = await response.json();
    return { clientSecret, error };

}
最佳回答

Your Cloud Function is a Callable Cloud Function, as we can see from the code: functions.https.onCall(). As explained in the doc "HTTPS callable functions are similar but not identical to HTTP functions"

You should call it from your app by using the specific methods of the JS SDK, as explained here.

It is possible to call it via fetch (or any other library used to make XMLHttpRequests from the browser) but then you need to follow the Protocol specification for https.onCall which is not straightforward. So it is highly recommended to use the SDK.

问题回答

In your cloud function , You use HTTP callable function which is not the same as HTTP function. You can read more from https://firebase.google.com/docs/functions/callable?gen=2nd

You should call HTTP callable function with client SDK httpsCallable() in your client





相关问题
• 如何关闭卢布特弗雷的分部?

我设立了一个新的账户,以便能够更好地管理我在消防基地的项目,但我已开始使用一个不同的账户来开会,我如何能够在......时结束这一账户的欺骗。

How to write to second tree in Firebase Realtime Database

I have a Firebase Realtime Database and I want to write to it, that s simple enough, but I have added more than one tree to my Database and want to know how to write to the second tree. Right now I ...

热门标签