I have a Google Cloud Function which does a Stripe Checkout API post call. But everytime the functions is called, in DevTools i see that it returns a “500 Internal Server Error” : {“error”:{“message”:”INTERNAL”,”status”:”INTERNAL”}}
The code looks likte this:
const functions = require('firebase-functions');
const http = require('http');
// This Cloud Function handles requests from clients and creates a Stripe checkout session for an evening shop.
exports.stripeAvondwinkel = functions.region('europe-west3')
.runWith({
memory: '128MB'
}).https.onCall((data, context) => {
// Extract the converted list of items from the request data
const convertedList = data.convertedList;
// Define constants for the checkout session parameters
const successUrl = 'https://example.com/succes';
const mode = 'payment';
const automaticTaxEnabled = 'true';
const bezorgkosten = 'XXXXXXXX';
// Convert the convertedList into line items for the checkout session
const lineItems = convertedList.map(item => {
const [prijsID, aantal] = item.split(',');
return {
price: prijsID,
quantity: aantal
};
});
// Construct the form data for the checkout session
const formData = {
'success_url': successUrl,
'mode': mode,
'automatic_tax[enabled]': automaticTaxEnabled,
'shipping_options[0][shipping_rate]': bezorgkosten
};
// Add line items to the form data
lineItems.forEach((item, index) => {
formData[`line_items[${index}][price]`] = item.price.toString();
formData[`line_items[${index}][quantity]`] = item.quantity;
});
// Log the constructed form data
console.log('Form Data:', formData);
// Add API post call logic
return new Promise((resolve, reject) => {
const req = http.request({
hostname: 'api.stripe.com',
path: '/v1/checkout/sessions',
method: 'POST',
headers: {
'Authorization': 'Bearer XXXXXXXXXX',
'Content-Type': 'application/x-www-form-urlencoded'
}
}, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
// Log the response data
console.log('Response Data:', data);
// Resolve or reject the promise based on the response status code
if (res.statusCode === 200) {
const url = JSON.parse(data).url;
resolve(url);
} else {
const errorMessage = JSON.parse(data).error.message;
reject(new Error(`Failed to create checkout session: ${errorMessage}`));
}
});
});
// Handle request errors
req.on('error', error => {
console.error('Request Error:', error);
reject(new Error(`Request error: ${error}`));
});
// Write form data to request body and end the request
req.write(new URLSearchParams(formData).toString());
req.end();
});
});
I attempted to execute the provided code for a Google Cloud Function, expecting it to successfully create a checkout session with the Stripe API based on the provided input data. However, upon execution, I encountered a 500 Internal Server Error. I double-checked the code for any syntax errors or misconfigurations, but couldn’t identify the root cause. Any insights or suggestions on resolving this issue would be greatly appreciated.