简体   繁体   中英

Bad request returned from google cloud function

I'm trying to get a google cloud function to work to open a stripe checkout, currently it isn't working however.

This is the function:


const functions = require('firebase-functions');
const express = require('express');
const app = express();
const cors = require('cors')({origin: true});
app.use(cors);
app.use(express.json())
app.use(express.static('public'))

app.use(
  express.urlencoded({
    extended: false,
  })
)


const stripe = require('stripe')(functions.config().stripe.secret_key);


exports.createStripeCheckout2 = functions.region('europe-west2').https.onCall(async(req, res) => {

  const cust = req.body.customerId
  const price = req.body.priceId
  const quantity = req.body.students
  const quantityA = Number(quantity)

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    mode: 'subscription',
    allow_promotion_codes: true,
    customer: 'cus_MQrpENhbL84RFD',  
    line_items: [
      {
        price: 'price_1LaJC1CKMdmWgnyspBbFet9v',
        // For metered billing, do not pass quantity
        quantity: 1,
      },
    ],
    success_url: `https://learning-platform-835f9.web.app/`,
    cancel_url: `https://learning-platform-835f9.web.app/`,
  });
  res.redirect(session.url);
});

Returns the following:

{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

With this in the console: Failed to load resource: the server responded with a status of 400 ()

The onCall() is different than onRequest() and the parameters in the function are not Request and Response from Express. It seems you are trying to call this function like a REST API using fetch or equivalent. If so, you must sent the request with the details mentioned in the documentation like the Content-Type header must be present and the body must have a data property.

In this case, using onRequest function should work keeping rest of the code as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM