Stripe集成问题:如何获取付款方账单/收货地址?
解决Stripe PaymentIntent账单/收货地址为Null的问题
你遇到的核心问题是:Stripe不会自动将仪表板中设置的地址同步到自定义集成的PaymentIntent中。不管是账单地址还是收货地址,都需要你主动在创建PaymentIntent时传递这些信息——要么从前端用户收集后传给后端,要么后端从自有数据库读取后设置。
下面是具体的解决步骤和代码修改示例:
1. 前端收集并传递账单地址(可选但推荐)
如果你的支付表单有收集用户地址的字段,可以在创建PaymentMethod时把地址信息加入billing_details中,这样地址会和PaymentMethod绑定,后续后端可以直接复用:
stripe.createPaymentMethod({ type: 'card', card: card, billing_details: { name: 'Jenny Rosen', // 新增账单地址字段,根据你的表单实际字段调整 address: { line1: document.getElementById('billing-line1').value, line2: document.getElementById('billing-line2').value, // 可选 city: document.getElementById('billing-city').value, postal_code: document.getElementById('billing-postcode').value, country: document.getElementById('billing-country').value, } }, }).then(stripePaymentMethodHandler);
2. 后端创建PaymentIntent时设置地址信息
你有两种方式在后端给PaymentIntent添加地址:
方式一:直接从PaymentMethod中读取前端传递的地址
如果前端已经把地址绑定到PaymentMethod,你可以先检索该PaymentMethod,然后将其BillingDetails赋值给PaymentIntent:
public ActionResult PayStripe(string payment_method_id) { StripeConfiguration.ApiKey = "sk_test_51Gv0ngD3zt5RrIg0KmTYo92QYmujb9Gp3dv8zz7fOJYjbLna3gRPOkHzZMSVMISHNgmPSrSncUtKL2DS86R4DEJI00mVv9GusU"; var paymentIntentService = new PaymentIntentService(); var paymentMethodService = new PaymentMethodService(); PaymentIntent paymentIntent = null; using (var ctx = new maharaja_bpDBEntities()) { try { // 先获取PaymentMethod,拿到前端传递的账单地址 var paymentMethod = paymentMethodService.Get(payment_method_id); var options = new PaymentIntentCreateOptions { PaymentMethod = payment_method_id, Amount = 1099, Currency = "gbp", ConfirmationMethod = "manual", Confirm = true, // 复用PaymentMethod中的账单地址 BillingDetails = paymentMethod.BillingDetails }; paymentIntent = paymentIntentService.Create(options); } catch (StripeException e) { return Json(new { error = e.StripeError.Message }); } return generatePaymentResponse(paymentIntent); } }
方式二:后端手动设置地址(比如从数据库读取用户地址)
如果你的数据库中存储了用户的账单/收货地址,可以直接在PaymentIntentCreateOptions中显式设置:
public ActionResult PayStripe(string payment_method_id) { StripeConfiguration.ApiKey = "sk_test_51Gv0ngD3zt5RrIg0KmTYo92QYmujb9Gp3dv8zz7fOJYjbLna3gRPOkHzZMSVMISHNgmPSrSncUtKL2DS86R4DEJI00mVv9GusU"; var paymentIntentService = new PaymentIntentService(); PaymentIntent paymentIntent = null; using (var ctx = new maharaja_bpDBEntities()) { try { // 假设从数据库获取用户地址 var user = ctx.Users.FirstOrDefault(u => u.Id == currentUserId); var options = new PaymentIntentCreateOptions { PaymentMethod = payment_method_id, Amount = 1099, Currency = "gbp", ConfirmationMethod = "manual", Confirm = true, // 设置账单地址 BillingDetails = new BillingDetailsOptions { Name = user.FullName, Address = new AddressOptions { Line1 = user.BillingLine1, City = user.BillingCity, PostalCode = user.BillingPostcode, Country = user.BillingCountry } }, // 如果需要收货地址,添加Shipping字段 Shipping = new ShippingOptions { Name = user.FullName, Address = new AddressOptions { Line1 = user.ShippingLine1, City = user.ShippingCity, PostalCode = user.ShippingPostcode, Country = user.ShippingCountry } } }; paymentIntent = paymentIntentService.Create(options); } catch (StripeException e) { return Json(new { error = e.StripeError.Message }); } return generatePaymentResponse(paymentIntent); } }
补充说明
你在Stripe仪表板设置的地址通常是用于Stripe Checkout的默认值,或者是一些全局模板设置,但对于自定义Elements集成(像你现在用的这种),Stripe不会自动填充这些信息,必须主动传递。
完成上述修改后,你的PaymentIntent对象中就会包含对应的账单和收货地址了。
内容的提问来源于stack exchange,提问作者redoc01




