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

如何通过Stripe.NET库为自定义账户创建支付链接?

Stripe.NET 为自定义账户创建支付链接的解决方案

问题描述

我想了解如何通过Stripe.NET库为自定义账户创建支付链接。我尝试了以下代码,但生成的产品和支付链接却关联到了我的Stripe关联账户,而非目标自定义账户。希望能获得解决该问题的代码或方案,谢谢。

用户提供的代码:

// create payment link
var plinkOpt = new PaymentLinkCreateOptions
{
    LineItems = new List<PaymentLinkLineItemOptions>
    {
        new PaymentLinkLineItemOptions
        {
            Price = new PriceService().Create(
                new PriceCreateOptions
                {
                    Currency = "usd",
                    Product = new ProductService().Create(new ProductCreateOptions { Name = "myproductname", }).Id,
                    CustomUnitAmount = new PriceCustomUnitAmountOptions { Enabled = true },
                }).Id,
            Quantity = 1,
        },
    },
};

var plinkSer = new PaymentLinkService();
plinkSer.Create(plinkOpt);

问题原因

你的代码未指定目标自定义账户,所有API请求默认使用平台账户(关联账户)的上下文执行,导致产品、价格和支付链接都创建在平台账户下,而非目标自定义账户。

解决方案代码

要针对自定义账户操作,需在每个Stripe服务调用时通过RequestOptions.StripeAccount指定目标账户ID(格式为acct_xxx)。修改后的代码如下:

// 替换为你的目标自定义账户ID
string targetAccountId = "acct_你的自定义账户ID";

// 1. 在目标账户下创建产品
var productService = new ProductService();
var product = productService.Create(
    new ProductCreateOptions { Name = "myproductname" },
    new RequestOptions { StripeAccount = targetAccountId }
);

// 2. 在目标账户下创建价格
var priceService = new PriceService();
var price = priceService.Create(
    new PriceCreateOptions
    {
        Currency = "usd",
        Product = product.Id,
        CustomUnitAmount = new PriceCustomUnitAmountOptions { Enabled = true },
    },
    new RequestOptions { StripeAccount = targetAccountId }
);

// 3. 在目标账户下创建支付链接
var plinkOpt = new PaymentLinkCreateOptions
{
    LineItems = new List<PaymentLinkLineItemOptions>
    {
        new PaymentLinkLineItemOptions
        {
            Price = price.Id,
            Quantity = 1,
        },
    },
};

var plinkSer = new PaymentLinkService();
var paymentLink = plinkSer.Create(
    plinkOpt,
    new RequestOptions { StripeAccount = targetAccountId }
);

关键注意事项

  • 所有归属自定义账户的资源(产品、价格、支付链接),创建时必须指定StripeAccount参数,确保资源归属正确。
  • 拆分创建步骤比嵌套调用更易维护和排查问题。
  • 确保你的API密钥拥有操作该自定义账户的权限,且目标账户已正确关联到你的平台账户。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 18:20:26