这是我第一次集成带区检出,但是我不断获得未定义的带区签名。
对于我的后端,我正在使用firebase云功能(不使用express),对于我的前端,我正在使用react-stripe-checkout。
我需要发送某种报头以便在后端接收它吗?
我现在发送的唯一标头是:
'Content-Type':'application / json',
后端代码:
// @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'});
})
})
朋友代码:
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>
)
}
您会混淆Webhook和服务器上收取令牌费用的路由。它们是完全独立的东西。
您的前端代码使用(已弃用的Stripe Checkout集成,StripeCheckout React组件,其中有一个使用旧版Stripe的旧库)来创建表示客户卡详细信息的Token对象。然后,您的意图是将令牌发布到后端,后端路由将调用Stripe API来创建费用:https : //stripe.com/docs/payments/accept-a-payment-charges#web-create-收费
POST的后端中的实际代码似乎不是,而是Webhook端点。那是一个单独的概念,当付款成功时,Stripe会向您发送一个请求,并将包括该签名标头。但是在这里,您正在处理的请求来自您自己的前端代码,它不是weboook且没有Stripe签名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句