ASP.NET MVC中Stripe Connect Direct Charges功能实现故障排查
Stripe Connect Direct Charges跳转空白问题排查与修复
我正在进行概念验证(POC)项目,需使用Stripe Connect的Direct Charges功能在付款时向关联账户扣费。我对支付网关经验有限,但已完成Stripe账户与应用的连接并成功收款,却在实现Direct Charges功能时遇到问题:遵循官方文档使用Stripe托管页面向客户收费,但点击跳转按钮后无反应,页面空白。
故障代码
var options = new Stripe.Checkout.SessionCreateOptions { LineItems = new List<Stripe.Checkout.SessionLineItemOptions> { new Stripe.Checkout.SessionLineItemOptions { PriceData = new Stripe.Checkout.SessionLineItemPriceDataOptions { Currency = "gbp", ProductData = new Stripe.Checkout.SessionLineItemPriceDataProductDataOptions { Name = "T-shirt", }, UnitAmount = 1000, }, Quantity = 1, }, }, PaymentIntentData = new Stripe.Checkout.SessionPaymentIntentDataOptions { ApplicationFeeAmount = 123, }, Mode = "payment", SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}", }; var requestOptions = new RequestOptions { StripeAccount = "ACCOUNTID", }; var service = new Stripe.Checkout.SessionService(); service.Create(options, requestOptions); return new StatusCodeResult(303);
正常对比代码
public IActionResult CheckOut(ProductViewModel product) { var domain = "http://localhost:5184/"; var options = new Stripe.Checkout.SessionCreateOptions { SuccessUrl = domain + $"CheckOut/Confirm", CancelUrl = domain + $"CheckOut/Cancel", LineItems = new List<Stripe.Checkout.SessionLineItemOptions> { new Stripe.Checkout.SessionLineItemOptions { PriceData = new Stripe.Checkout.SessionLineItemPriceDataOptions { Currency = "gbp", ProductData = new Stripe.Checkout.SessionLineItemPriceDataProductDataOptions { Name = "T-shirt", }, UnitAmount = 1000, }, Quantity = 1, }, }, Mode = "payment" }; var service = new Stripe.Checkout.SessionService(); Stripe.Checkout.Session session = service.Create(options); Response.Headers.Add("Location", session.Url); return new StatusCodeResult(303); }
问题原因与修复方案
- 缺少CancelUrl配置:Stripe Checkout Session创建时必须同时指定
SuccessUrl和CancelUrl,缺失后者会导致Session创建失败,无法生成有效跳转地址。 - 未处理Session返回值:故障代码中调用
service.Create后没有保存返回的Session对象,也未将Session的Url添加到Response的Location头中,303重定向需要该头指定跳转目标。 - 关联账户状态验证:确保
StripeAccount参数对应的关联账户已完成Stripe Connect入驻流程,账户状态正常。
修复后的代码示例
var options = new Stripe.Checkout.SessionCreateOptions { LineItems = new List<Stripe.Checkout.SessionLineItemOptions> { new Stripe.Checkout.SessionLineItemOptions { PriceData = new Stripe.Checkout.SessionLineItemPriceDataOptions { Currency = "gbp", ProductData = new Stripe.Checkout.SessionLineItemPriceDataProductDataOptions { Name = "T-shirt", }, UnitAmount = 1000, }, Quantity = 1, }, }, PaymentIntentData = new Stripe.Checkout.SessionPaymentIntentDataOptions { ApplicationFeeAmount = 123, }, Mode = "payment", SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}", CancelUrl = "https://example.com/cancel", // 新增取消跳转地址 }; var requestOptions = new RequestOptions { StripeAccount = "ACCOUNTID", // 替换为实际关联账户ID }; var service = new Stripe.Checkout.SessionService(); Stripe.Checkout.Session session = service.Create(options, requestOptions); // 获取创建后的Session Response.Headers.Add("Location", session.Url); // 设置重定向地址 return new StatusCodeResult(303);
内容的提问来源于stack exchange,提问作者Abdullah Ergun
相关产品推荐
相关产品推荐

