我尝试使用 AWS Secrets Manager 保护 Stripe Webhook 端点Secret,但 Webhook 签名验证失败。 对于如何从 AWS Secrets Manager 访问 Webhook 端点Secret 的任何帮助,我们不胜感激
const AWS = require('aws-sdk');
// Set the AWS region3
AWS.config.update({ region: 'us-east-2' });
// Create a Secrets Manager client
const secretsManager = new AWS.SecretsManager();
// Specify the secret name
const secretName = 'testSecretKeys';
// Retrieve the secret value
secretsManager.getSecretValue({ SecretId: secretName }, (err, data) => {
if (err) {
console.error(`Error retrieving secret: ${err}`);
} else {
// Parse and use the secret data
secretData = JSON.parse(data.SecretString);
const stripeSKKey = secretData['skXXX'];
const stripePKKey = secretData['pkXXX'];
//const endpointSecret = 'whsec_XXX';
const endpointSecret = secretData['whepXXX'];
const stripe = require('stripe')(stripeSKKey);
const express = require('express');
const app = express();
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
let 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-sig'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}