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

使用Stripe Checkout Session如何将新创建的客户ID传回成功URL

实现Stripe Checkout Session成功后传回客户ID的两种方案

可以实现你的需求,根据是否需要提前创建客户,有两种常用方案:

方案1:提前创建Stripe客户(适合需提前关联系统用户的场景)

如果你希望在跳转Checkout页面之前就拿到客户ID,可以先创建Stripe客户,再将其绑定到Session,并把客户ID拼入SuccessUrl:

修改后的代码如下:

// 先创建Stripe客户
var customerOptions = new CustomerCreateOptions();
var customerService = new CustomerService(this.client);
var customer = await customerService.CreateAsync(customerOptions);

// 构建Session时关联该客户,同时把客户ID拼到成功跳转URL
var options = new SessionCreateOptions
{
    SuccessUrl = $"{successUrl}?customerId={customer.Id}",
    CancelUrl = cancelUrl,
    Mode = "subscription",
    AllowPromotionCodes = true,
    Customer = customer.Id, // 绑定提前创建的客户
    LineItems = new List<SessionLineItemOptions>
    {
        new SessionLineItemOptions
        {
            Price = Request.Form["priceId"],
            Quantity = 1,
        },
    }
};

var service = new SessionService(this.client);
try
{
    var session = await service.CreateAsync(options);
    Response.Headers.Add("Location", session.Url);
    return new StatusCodeResult(303);
}

用户支付成功后,跳转的SuccessUrl会携带customerId参数,你可以直接从URL中提取并存储到自己的系统。

方案2:通过Stripe Webhook获取客户ID(适合让Stripe自动创建客户的场景)

如果不想提前创建客户,让Stripe在用户完成支付后自动生成客户,就必须通过Webhook监听事件来获取客户ID:

  1. 先在Stripe后台配置checkout.session.completed事件的Webhook端点,确保端点能接收Stripe的POST请求并验证签名(必须做签名验证,防止伪造请求)。
  2. 后端编写Webhook处理逻辑,从事件中提取客户ID:
[HttpPost("stripe-webhook")]
public async Task<IActionResult> StripeWebhook()
{
    var json = await new StreamReader(Request.Body).ReadToEndAsync();
    Event stripeEvent;
    try
    {
        stripeEvent = EventUtility.ConstructEvent(
            json,
            Request.Headers["Stripe-Signature"],
            "你的Webhook签名密钥" // 从Stripe后台获取
        );
    }
    catch (Exception)
    {
        return BadRequest();
    }

    // 处理支付完成事件
    if (stripeEvent.Type == Events.CheckoutSessionCompleted)
    {
        var session = stripeEvent.Data.Object as Session;
        var customerId = session.CustomerId; // 拿到自动创建的客户ID
        var yourUserId = session.Metadata["your_user_id"]; // 从元数据取系统用户标识

        // 在这里将customerId与你的系统用户关联存储
    }

    return Ok();
}

如果需要在SuccessUrl页面直接使用客户ID,可以在创建Session时添加元数据,存入你的系统用户标识:

var options = new SessionCreateOptions
{
    SuccessUrl = successUrl,
    CancelUrl = cancelUrl,
    Mode = "subscription",
    AllowPromotionCodes = true,
    Metadata = new Dictionary<string, string>
    {
        { "your_user_id", "当前登录用户的唯一ID" }
    },
    LineItems = new List<SessionLineItemOptions>
    {
        new SessionLineItemOptions
        {
            Price = Request.Form["priceId"],
            Quantity = 1,
        },
    }
};

Webhook拿到客户ID后更新系统用户记录,前端Success页面再调用你的接口查询该用户对应的客户ID即可。

注:当你不指定Customer参数时,Stripe是在用户完成支付后才创建客户的,所以创建Session时无法直接获取客户ID,必须通过Webhook获取。

内容的提问来源于stack exchange,提问作者DarkW1nter

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 17:31:04