Node.js 如何在stripe webhook标题中获取stripe签名
这是我第一次集成条带签出,但我一直得到未定义的条带签名 对于我的后端,我使用firebase云函数(没有express),对于我的前端,我使用react stripe checkout 是否需要发送某种标头才能在后端接收该标头 我现在发送的唯一标题是: “内容类型”:“应用程序/json” 后端代码:Node.js 如何在stripe webhook标题中获取stripe签名,node.js,stripe-payments,Node.js,Stripe Payments,这是我第一次集成条带签出,但我一直得到未定义的条带签名 对于我的后端,我使用firebase云函数(没有express),对于我的前端,我使用react stripe checkout 是否需要发送某种标头才能在后端接收该标头 我现在发送的唯一标题是: “内容类型”:“应用程序/json” 后端代码: // @ts-ignore const stripe = new Stripe('sk_test_123'); export const stripeHook = functions.h
// @ts-ignore
const stripe = new Stripe('sk_test_123');
export const stripeHook = functions.https.onRequest(async (request, response) => {
cors(request, response, async () => {
const sig = request.headers['stripe-signature']; // this is always undefined
// let sig = request.get('stripe-signature'); //also tried streaming
const endpointSecret = 'whsec_123';
let event;
try {
event = stripe.webhooks.constructEvent(request.rawBody.toString('utf8'), sig, endpointSecret);
} catch (err) {
console.log(err)
response.send({status: 'error'});
return;
}
// Handle Type of webhook
const intent:any = event.data.object;
switch (event.type) {
case 'payment_intent.succeeded':
console.log("Succeeded:", intent.id);
break;
case 'payment_intent.payment_failed':
const message = intent.last_payment_error && intent.last_payment_error.message;
console.log('Failed:', intent.id, message);
break;
}
response.send({status: 'success'});
})
})
fronend代码:
import React, {useState, useEffect} from 'react';
import StripeCheckout from 'react-stripe-checkout';
import { ToastContainer, toast } from "react-toastify";
import 'react-toastify/dist/ReactToastify.css';
import api from '../../services/apiMiddleware';
function Assessment(props: any) {
const [product] = React.useState({
name: "Subscription",
price: 99.99,
description: "Monthly Subscription"
});
const handleToken = async (token: string) => {
const response = await api.post(
"stripeHook",
{ token, product }
);
if (response.status === "success") {
toast("Success! Check email for details", { type: "success" });
} else {
toast("Something went wrong", { type: "error" });
}
}
return (
<div>
<div className="paymentButtonTextWrapper">
<ToastContainer />
<div className="paymentButtonWrapper">
<StripeCheckout
// @ts-ignore
token={handleToken}
stripeKey="pk_test_123"
/>
</div>
</div>
</div>
)
}
import React,{useState,useffect}来自“React”;
从“react stripe checkout”导入StripeCheckout;
从“react toastify”导入{ToastContainer,toast};
导入“react-toastify/dist/react-toastify.css”;
从“../../services/apimmediater”导入api;
功能评估(道具:任何){
常量[产品]=React.useState({
名称:“订阅”,
价格:99.99,
说明:“每月订阅”
});
const handleToken=async(令牌:字符串)=>{
const response=wait api.post(
“脱衣书”,
{令牌,产品}
);
如果(response.status==“success”){
toast(“成功!查看电子邮件了解详细信息”{type:“Success”});
}否则{
toast(“出错了”{type:“error”});
}
}
返回(
)
}
您混淆了webhook和服务器上对令牌收费的路由。它们是完全不同的东西
前端代码使用(a,StripeCheckout React组件,有一个使用旧版本Stripe的旧库)创建表示客户卡详细信息的令牌对象。这样做的目的是将该令牌发布到后端,后端路由将调用条带API来创建费用:
您发布到后端的实际代码似乎不是这样,而是一个webhook端点。这是一个单独的概念,Stripe将在支付成功时向您发送请求,并将包括该签名头。但在这里,您正在处理的请求来自您自己的前端代码,它不是Webook,也没有条带签名。您是否也在stipe仪表板中设置了webhook端点?是的,并且得到了签名钩子秘密“whsec_mySecret”你是对的react库已经过时,当你使用“@stripe/react stripe js”库时,webhook会自动调用