【发布时间】:2021-07-25 03:07:36
【问题描述】:
这是我的 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 函数,因为下面的常规云函数失败并出现以下错误: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;
});
任何建议表示赞赏。
【问题讨论】:
-
在部署函数时获取函数 URL。然后只需将您的快速应用程序路由附加到末尾
-
是的,我已经使用常规云功能(在部署后获取 URL)完成了此操作,但相同的方法不适用于如上所示的快速端点。我获取了我的基本函数 URL 并将端点添加到它:
https://us-central1-<MY_APP>.cloudfunctions.net/stripe_webhook- 但它不是从 Stripe 触发的 - 所以 URL 一定是错误的。 -
请分享完整代码。你在哪里定义了“http函数”?
-
const app = require('express')();- 显示在第一个代码块的顶部@Dharmaraj -
我的意思是HTTPS功能对不起
标签: node.js firebase express google-cloud-functions stripe-payments