如何调用 Google Cloud Functions 中的 Express 函数?

Zor*_*gan 1 node.js express stripe-payments firebase google-cloud-functions

这是我的快速端点,它是通过Stripe 的 webhook调用的:

\n
const functions = require(\'firebase-functions\');\nconst bodyParser = require(\'body-parser\')\nconst app = require(\'express\')();\nconst stripe = require(\'stripe\')(functions.config().stripe.test.secret);\nconst endpointSecret = functions.config().stripe.test.webhookkey;\n\napp.post(\'/stripe_webhook\', bodyParser.raw({type: \'application/json\'}), async (request, response) => {\n    const event = request.body;\n    // Only verify the event if you have an endpoint secret defined.\n    // Otherwise use the basic event deserialized with JSON.parse\n    if (endpointSecret) {\n    // Get the signature sent by Stripe\n    const signature = request.headers[\'stripe-signature\'];\n    try {\n      const eventConstruct = stripe.webhooks.constructEvent(\n        request.body,\n        signature,\n        endpointSecret\n      );\n    } catch (err) {\n      console.log(`\xe2\x9a\xa0\xef\xb8\x8f  Webhook signature verification failed.`, err.message);\n      return response.sendStatus(400);\n    }\n    }\n    console.log(`stripeEvent: ${event.type}`)\n    // Handle the event\n    const paymentIntent = event.data.object;\n    if (event.type === \'payment_intent.succeeded\'){\n        const uid = await getCustomerUID(paymentIntent.customer)\n        return actionRequest(uid, paymentIntent.client_secret)\n    } \n  // Return a 200 response to acknowledge receipt of the event\n  response.send();\n});\n
Run Code Online (Sandbox Code Playgroud)\n

我想添加此端点的 URL,以便我的 Stripe 帐户可以触发它。但是,与常规 Cloud Function 一样,它没有 URL(这是 Stripe 访问 Webhook 所需的 URL):

\n

在此输入图像描述

\n

有没有办法获取 URL 或以其他方式调用此端点?

\n

PS:我正在使用快速函数,因为下面的常规云函数失败并出现以下错误:Webhook signature verification failed. No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?\n代码:

\n
exports.stripeEvent = functions.https.onRequest(async (request, response) => {\n    // Get the signature from the request header | Verify it\'s coming from Stripe\n    let signature = request.headers["stripe-signature"];\n    // Verify the request against our endpointSecret\n    console.log(`signature: ${signature}`)\n    console.log(`endpointSecret: ${endpointSecret}`)\n    console.log(`request.rawBody: ${request.rawBody}`)\n    try {\n        let event = stripe.webhooks.constructEvent(request.rawBody, signature, endpointSecret)\n    } catch (err) {\n        console.log(`\xe2\x9a\xa0\xef\xb8\x8f  Webhook signature verification failed.`, err.message);\n        return response.status(400).end();\n    }\n    const event = request.body;\n    console.log(`stripeEvent: ${event.type}`)\n    // Handle the event\n    const paymentIntent = event.data.object;\n    if (event.type === \'payment_intent.succeeded\'){\n        const uid = await getCustomerUID(paymentIntent.customer)\n        return actionRequest(uid, paymentIntent.client_secret)\n    } \n  // Return a 200 response to acknowledge receipt of the event\n  response.json({received: true});\n  return 200;\n});\n
Run Code Online (Sandbox Code Playgroud)\n

任何建议表示赞赏。

\n

Dha*_*raj 5

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); 
Run Code Online (Sandbox Code Playgroud)

部署该函数时,您可以在仪表板中看到该函数“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。