如何向Stripe传递客户及账单地址信息?
问题描述
我已经实现了Stripe支付功能,但Stripe的文档比较晦涩。现在需要向Stripe传递账单与客户信息,不清楚该在哪里添加地址。我尝试过在PaymentIntent中添加地址,现有后端代码如下:
// Create a PaymentIntent with amount and currency $paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => Cart::total(2, '.', '') * 100, 'currency' => 'usd', 'automatic_payment_methods' => [ 'enabled' => true, ], ]);
我的业务流程是:收货地址 > 账单地址 > 支付,前端相关代码如下:
<script src="https://js.stripe.com/v3/"></script> <script defer> const stripe_key = 'pk_test_51IX6wjDNgrti9hItzFXHGdxF32r6lQDsQJa1KI6746HgnnJEVY7XbX0a7o9fSFuGeWzZTcAzJXNuSFzekqmWSroO00QFB9NpUn'; const payment_intent_route = 'https://jewelrycadfiles.com/payment/intent'; const _token = 'UE5r6J5c7hhLLpFjMrA2hCmeZ4YuG2LECPAu7SJu'; const place_order_route = 'https://jewelrycadfiles.com/checkout'; const order_cancel_route = 'https://jewelrycadfiles.com/payment/cancel'; const finish_page = 'https://jewelrycadfiles.com/payment/finished'; const buy_now_mode = '0'; </script>
解决方案
方式一:直接在PaymentIntent中传入账单地址
你可以在创建PaymentIntent时,通过billing_details字段直接传入客户的账单地址信息,修改后端代码如下:
$paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => Cart::total(2, '.', '') * 100, 'currency' => 'usd', 'automatic_payment_methods' => [ 'enabled' => true, ], // 新增账单地址字段 'billing_details' => [ 'name' => $_POST['billing_name'], // 从前端表单获取的客户姓名 'email' => $_POST['billing_email'], // 客户邮箱 'address' => [ 'line1' => $_POST['billing_line1'], // 街道地址 'line2' => $_POST['billing_line2'], // 公寓/房间号(可选) 'city' => $_POST['billing_city'], // 城市 'state' => $_POST['billing_state'], // 州/省份 'postal_code' => $_POST['billing_postal_code'], // 邮政编码 'country' => $_POST['billing_country'] // 国家代码(如US、CN) ] ] ]);
注:这里的地址数据需要从前端账单地址表单收集,在调用后端创建PaymentIntent的接口时一并传递。
方式二:关联Stripe Customer对象(推荐长期留存客户信息场景)
如果你的业务需要长期保存客户信息,建议先创建Stripe Customer对象,再将其关联到PaymentIntent:
- 创建Customer并传入地址
// 先创建Stripe客户对象 $customer = \Stripe\Customer::create([ 'name' => $_POST['billing_name'], 'email' => $_POST['billing_email'], 'address' => [ 'line1' => $_POST['billing_line1'], 'line2' => $_POST['billing_line2'], 'city' => $_POST['billing_city'], 'state' => $_POST['billing_state'], 'postal_code' => $_POST['billing_postal_code'], 'country' => $_POST['billing_country'] ] ]); // 创建关联该客户的PaymentIntent $paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => Cart::total(2, '.', '') * 100, 'currency' => 'usd', 'automatic_payment_methods' => [ 'enabled' => true, ], 'customer' => $customer->id // 关联客户ID ]);
前端配合处理示例
在账单地址页面添加表单,收集信息后提交给后端接口:
// 假设账单表单ID为billing-form document.getElementById('billing-form').addEventListener('submit', async (e) => { e.preventDefault(); // 收集表单数据 const formData = new FormData(e.target); const billingInfo = { billing_name: formData.get('name'), billing_email: formData.get('email'), billing_line1: formData.get('line1'), billing_line2: formData.get('line2'), billing_city: formData.get('city'), billing_state: formData.get('state'), billing_postal_code: formData.get('postal_code'), billing_country: formData.get('country') }; // 调用后端创建PaymentIntent的接口 const response = await fetch(payment_intent_route, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': _token }, body: JSON.stringify(billingInfo) }); const data = await response.json(); // 后续执行Stripe支付流程(如初始化元素、确认支付等) });
内容的提问来源于stack exchange,提问作者varri
相关产品推荐
相关产品推荐

