如何在 Google Cloud Functions 中调用 Express 函数?
How to call an Express function in Google Cloud Functions?
这是我的 express 端点,它是通过 webhook from Stripe:
调用的
const functions = require('firebase-functions');
const bodyParser = require('body-parser')
const app = require('express')();
const stripe = require('stripe')(functions.config().stripe.test.secret);
const endpointSecret = functions.config().stripe.test.webhookkey;
app.post('/stripe_webhook', bodyParser.raw({type: 'application/json'}), async (request, response) => {
const event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
const eventConstruct = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}
}
console.log(`stripeEvent: ${event.type}`)
// Handle the event
const paymentIntent = event.data.object;
if (event.type === 'payment_intent.succeeded'){
const uid = await getCustomerUID(paymentIntent.customer)
return actionRequest(uid, paymentIntent.client_secret)
}
// Return a 200 response to acknowledge receipt of the event
response.send();
});
我想为此端点添加 URL,以便我的 Stripe 帐户可以触发它。但是,与常规 Cloud Function 一样,它没有 URL(这是 Stripe 访问 webhook 所需要的):
有没有办法获取 URL 或以其他方式调用此端点?
PS:我正在使用 express 函数,因为下面的常规 Cloud Function 失败并出现以下错误:Webhook signature verification failed. No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
代码:
exports.stripeEvent = functions.https.onRequest(async (request, response) => {
// Get the signature from the request header | Verify it's coming from Stripe
let signature = request.headers["stripe-signature"];
// Verify the request against our endpointSecret
console.log(`signature: ${signature}`)
console.log(`endpointSecret: ${endpointSecret}`)
console.log(`request.rawBody: ${request.rawBody}`)
try {
let event = stripe.webhooks.constructEvent(request.rawBody, signature, endpointSecret)
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.status(400).end();
}
const event = request.body;
console.log(`stripeEvent: ${event.type}`)
// Handle the event
const paymentIntent = event.data.object;
if (event.type === 'payment_intent.succeeded'){
const uid = await getCustomerUID(paymentIntent.customer)
return actionRequest(uid, paymentIntent.client_secret)
}
// Return a 200 response to acknowledge receipt of the event
response.json({received: true});
return 200;
});
感谢任何建议。
const functions = require("firebase-functions");
const app = require("express")();
app.get("/test", function (req, res) {
return res.sendStatus(200);
})
app.post("/stripe-event", function (req, res) {
//Do the processing
return res.sendStatus(200);
})
// This is the Firebase HTTPS function
exports.api = functions.https.onRequest(app);
当您部署该函数时,您可以在仪表板中看到该函数“api”的 URL,它应该类似于:https://<region><project-id>.cloudfunctions.net/api
.
现在,如果您想调用端点 /test
,则必须调用此 URL:"https://<region><project-id>.cloudfunctions.net/api/test"
确保你有这一行:exports.api = functions.https.onRequest(app);
如果你使用它,你的函数的名称将是“api”,然后将 express 应用程序的 API 端点附加到那个 URL.
这是我的 express 端点,它是通过 webhook from Stripe:
调用的const functions = require('firebase-functions');
const bodyParser = require('body-parser')
const app = require('express')();
const stripe = require('stripe')(functions.config().stripe.test.secret);
const endpointSecret = functions.config().stripe.test.webhookkey;
app.post('/stripe_webhook', bodyParser.raw({type: 'application/json'}), async (request, response) => {
const event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
const eventConstruct = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}
}
console.log(`stripeEvent: ${event.type}`)
// Handle the event
const paymentIntent = event.data.object;
if (event.type === 'payment_intent.succeeded'){
const uid = await getCustomerUID(paymentIntent.customer)
return actionRequest(uid, paymentIntent.client_secret)
}
// Return a 200 response to acknowledge receipt of the event
response.send();
});
我想为此端点添加 URL,以便我的 Stripe 帐户可以触发它。但是,与常规 Cloud Function 一样,它没有 URL(这是 Stripe 访问 webhook 所需要的):
有没有办法获取 URL 或以其他方式调用此端点?
PS:我正在使用 express 函数,因为下面的常规 Cloud Function 失败并出现以下错误:Webhook signature verification failed. No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
代码:
exports.stripeEvent = functions.https.onRequest(async (request, response) => {
// Get the signature from the request header | Verify it's coming from Stripe
let signature = request.headers["stripe-signature"];
// Verify the request against our endpointSecret
console.log(`signature: ${signature}`)
console.log(`endpointSecret: ${endpointSecret}`)
console.log(`request.rawBody: ${request.rawBody}`)
try {
let event = stripe.webhooks.constructEvent(request.rawBody, signature, endpointSecret)
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.status(400).end();
}
const event = request.body;
console.log(`stripeEvent: ${event.type}`)
// Handle the event
const paymentIntent = event.data.object;
if (event.type === 'payment_intent.succeeded'){
const uid = await getCustomerUID(paymentIntent.customer)
return actionRequest(uid, paymentIntent.client_secret)
}
// Return a 200 response to acknowledge receipt of the event
response.json({received: true});
return 200;
});
感谢任何建议。
const functions = require("firebase-functions");
const app = require("express")();
app.get("/test", function (req, res) {
return res.sendStatus(200);
})
app.post("/stripe-event", function (req, res) {
//Do the processing
return res.sendStatus(200);
})
// This is the Firebase HTTPS function
exports.api = functions.https.onRequest(app);
当您部署该函数时,您可以在仪表板中看到该函数“api”的 URL,它应该类似于:https://<region><project-id>.cloudfunctions.net/api
.
现在,如果您想调用端点 /test
,则必须调用此 URL:"https://<region><project-id>.cloudfunctions.net/api/test"
确保你有这一行:exports.api = functions.https.onRequest(app);
如果你使用它,你的函数的名称将是“api”,然后将 express 应用程序的 API 端点附加到那个 URL.