Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading