You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Stripe Checkout支付后将元数据传入Stripe Dashboard支付元数据区

问题:Stripe Checkout元数据未显示在支付记录Metadata区域

我用以下Django代码尝试将元数据传递至Stripe Dashboard的Payments板块Metadata区域:

# "views.py"

from django.shortcuts import redirect
import stripe

def test(request):
    checkout_session = stripe.checkout.Session.create(
        line_items=[
            {
                "price_data": {
                    "currency": "USD",
                    "unit_amount_decimal": 1000,
                    "product_data": {
                        "name": "T-shirt",
                    },
                },
                "quantity": 2,
            }
        ],
        metadata = { # 此处设置元数据
            "name": "Joho Smith",
            "age": "36",
            "gender": "Male",
        },
        mode='payment',
        success_url='http://localhost:8000',
        cancel_url='http://localhost:8000'
    )
    return redirect(checkout_session.url, code=303)

完成Stripe Checkout支付后,我在Stripe Dashboard的Payments中点击该支付记录,发现Metadata区域为空,但Events and logs板块中存在对应的元数据。请问如何让元数据显示在Payments的Metadata区域中?


解决方案

你现在把元数据设置在了Checkout Session对象上,但Stripe Payments板块展示的是**PaymentIntent(支付意向)**的信息,Session的元数据不会自动同步到PaymentIntent里,所以才会只在日志里看到,而支付记录的Metadata区域为空。

要解决这个问题,需要把元数据通过payment_intent_data参数传递给PaymentIntent,修改后的代码如下:

# "views.py"

from django.shortcuts import redirect
import stripe

def test(request):
    # 先定义元数据变量,方便复用
    customer_metadata = {
        "name": "Joho Smith",
        "age": "36",
        "gender": "Male",
    }
    
    checkout_session = stripe.checkout.Session.create(
        line_items=[
            {
                "price_data": {
                    "currency": "USD",
                    "unit_amount_decimal": 1000,
                    "product_data": {
                        "name": "T-shirt",
                    },
                },
                "quantity": 2,
            }
        ],
        # 保留Session的元数据(可选,不影响支付记录展示)
        metadata = customer_metadata,
        # 关键:将元数据传递给PaymentIntent
        payment_intent_data={
            "metadata": customer_metadata
        },
        mode='payment',
        success_url='http://localhost:8000',
        cancel_url='http://localhost:8000'
    )
    return redirect(checkout_session.url, code=303)

这样设置后,支付完成后,你在Stripe Dashboard的Payments板块点击对应支付记录,就能在Metadata区域看到你设置的元数据了。


内容的提问来源于stack exchange,提问作者Super Kai - Kazuya Ito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 16:42:45