简体   繁体   English

如何将信息传递给 stripe.checkout.sessions.create() 函数

[英]How to pass information to the stripe.checkout.sessions.create() function

What I want to do: I use firebase so as soon as someone purchases a Product over stripe I want to write that on the document of the user that bought it.我想要做什么:我使用 firebase,所以只要有人购买了条纹产品,我就想将其写在购买它的用户的文档上。 I want to use a stripe webhook for that.我想为此使用条纹 webhook。 This webhook looks like this:这个网络钩子看起来像这样:

在此处输入图片说明

It essentially makes a https request to firebase functions as soon as the payment went trough.它基本上会在付款完成后立即向 firebase 功能发出 https 请求。 It triggers a https function that should write to the user's document that he has purchased the product.它会触发一个 https 函数,该函数应该写入用户已购买产品的文档。 But to do that the webhook needs to contain the firebase user uid of the user that bought the product.但是要做到这一点,webhook 需要包含购买该产品的用户的 firebase 用户 uid。

But to pass the user uid to the webhook I need to pass it to the stripe.checkout.sessions.create() function that looks like this:但是要将用户 uid 传递给 webhook,我需要将其传递给 stripe.checkout.sessions.create() 函数,如下所示:

    exports.createCheckoutSession = functions.https.onCall(async(data, context) => {
    const YOUR_DOMAIN = 'http://localhost:4242';
    const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        line_items: [{
            price_data: {
                currency: 'usd',
                product_data: {
                    name: 'Stubborn Attachments',
                    images: ['https://i.imgur.com/EHyR2nP.png'],
                },
                unit_amount: 2000,
            },
            quantity: 1,
        }, ],
        mode: 'payment',
        success_url: `${YOUR_DOMAIN}/success.html`,
        cancel_url: `${YOUR_DOMAIN}/cancel.html`,
    });
    console.log(session.id)
    return session.id
})

(the user calls this cloud function to get a checkout session returned) But how can I pass the user uid ( context.user.uid ) in so I can later send it with the webhook. (用户调用此云函数以获得返回的结帐会话)但是我如何传递用户 uid ( context.user.uid ) 以便稍后我可以使用 webhook 发送它。

I hope it is clear what I want to do if not please ask.我希望很清楚我想要做什么,如果没有请询问。 Thank you for your time.感谢您的时间。

You can add Firebase UID of user or any other information in metadata of stripe checkout session like this:您可以在条带结帐会话的元数据中添加用户的 Firebase UID 或任何其他信息,如下所示:

const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        // line items and other stuff
        metadata: {firebaseUID: uid, product_id: "ProductID"},
    });

You can read them in the checkout session completed webhook or any success webhook.您可以在结帐会话完成的 webhook 或任何成功的 webhook 中阅读它们。

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

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