简体   繁体   English

如何在您的条带 webhook header 中获取条带签名

[英]how to get stripe-signature in your stripe webhook header

This is the first time I'm integrating stripe checkout but I keep getting stripe-signature as undefined.这是我第一次集成条带结帐,但我一直将条带签名设为未定义。

For my backend I am using firebase cloud functions (without express) and for my frontend I am using react-stripe-checkout.对于我的后端,我使用的是 firebase 云函数(没有 express),对于我的前端,我使用的是 react-stripe-checkout。

Is there some kind of header I need to send in order to receive that on the backend?是否有某种 header 我需要发送才能在后端接收?

The only header I am sending now is:我现在发送的唯一 header 是:

'Content-Type': 'application/json', '内容类型':'应用程序/json',

backend code:后端代码:

    // @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 code:前端代码:

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>
)
}

You're confusing a webhook and the route on your server that charges the token.您混淆了 webhook 和服务器上收取令牌的路由。 They're entirely separate things.它们是完全不同的东西。

Your frontend code uses (a deprecated Stripe Checkout integration , the StripeCheckout React component there is an old library using an old version of Stripe) to create a Token object representing the customer's card details.您的前端代码使用(已弃用的 Stripe Checkout 集成,StripeCheckout React 组件有一个使用旧版本 Stripe 的旧库)来创建代表客户卡详细信息的令牌 object。 The intention then is that you POST that token to your backend and your backend route will call the Stripe API to create a charge: https://stripe.com/docs/payments/accept-a-payment-charges#web-create-charge然后的意图是您将该令牌发布到您的后端,并且您的后端路由将调用 Stripe API 来创建费用: https://stripe.com/docs/payments/accept-a-payment-charges#web-create-收费

The actual code in your backend that you POST to though seems to not be that, it's a webhook endpoint.您发布到的后端中的实际代码似乎并非如此,它是一个 webhook 端点。 That's a separate concept, Stripe would send a request to you when a payment succeeds and will include that signature header.这是一个单独的概念,当付款成功时,Stripe 会向您发送一个请求,并将包含该签名 header。 But here the request you are processing is coming from your own frontend code, it's not a weboook and it has no Stripe signature.但是在这里,您正在处理的请求来自您自己的前端代码,它不是网络书,也没有 Stripe 签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM