如何为PayPal订单预填充账单地址(收货地址已配置完成)
解决PayPal支付表单账单地址预填充问题
我已成功实现PayPal支付表单的收货地址预填充,但账单地址始终无法预填充。以下是当前的PayPal按钮实现代码,以及我尝试添加的账单地址配置代码,寻求技术帮助:
当前PayPal按钮代码
paypal.Buttons({ createOrder: function(data, actions) { return actions.order.create({ purchase_units: [{ reference_id: "PUHF", description: "Sporting Goods", custom_id: "CUST-HighFashions", soft_descriptor: "HighFashions", amount: { currency_code: "USD", value: "220.00", breakdown: { item_total: { currency_code: "USD", value: "180.00" }, discount: { currency_code: "USD", value: "10.00" }, shipping: { currency_code: "USD", value: "30.00" }, handling: { currency_code: "USD", value: "10.00" }, tax_total: { currency_code: "USD", value: "20.00" }, shipping_discount: { currency_code: "USD", value: "10.00" } } }, items: [{ name: "T-Shirt", description: "Green XL", sku: "sku01", unit_amount: { currency_code: "USD", value: "90.00" }, tax: { currency_code: "USD", value: "10.00" }, quantity: "1", category: "PHYSICAL_GOODS" }, { name: "Shoes", description: "Running, Size 10.5", sku: "sku02", unit_amount: { currency_code: "USD", value: "45.00" }, tax: { currency_code: "USD", value: "5.00" }, quantity: "2", category: "PHYSICAL_GOODS" } ], shipping: { method: "United States Postal Service", name: { full_name: "John Doe" }, address: { address_line_1: "123 Townsend St", address_line_2: "Floor 6", admin_area_2: "San Francisco", admin_area_1: "CA", postal_code: "94107", country_code: "US" } } }] }) }, onApprove: function(data, actions) { return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', { method: 'post' }).then(function(res) { return res.json(); }).then(function(orderData) { var errorDetail = Array.isArray(orderData.details) && orderData.details[0]; if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') { return actions.restart(); } if (errorDetail) { var msg = 'Sorry, your transaction could not be processed.'; if (errorDetail.description) msg += '\n\n' + errorDetail.description; if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')'; return alert(msg); } console.log('Capture result', orderData, JSON.stringify(orderData, null, 2)); var transaction = orderData.purchase_units[0].payments.captures[0]; alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details'); }); } }).render('#paypal-button-container')
我尝试添加的账单地址配置(未生效)
payer: { email_address: 'customer@domain.com', phone: { phone_number: { national_number: '4543433243', } }, name: { given_name: 'PayPal', surname: 'Customer', }, address: { address_line_1: '123 ABC Street', address_line_2: 'Apt 2', admin_area_2: 'San Jose', admin_area_1: 'CA', postal_code: '95121', country_code: 'US', }, },
解决方案
问题出在payer对象的位置错误——它必须和purchase_units同级,不能嵌套在purchase_units数组内部。以下是修正后的完整代码:
paypal.Buttons({ createOrder: function(data, actions) { return actions.order.create({ // 正确放置payer对象,与purchase_units同级 payer: { email_address: 'customer@domain.com', phone: { phone_number: { national_number: '4543433243', } }, name: { given_name: 'PayPal', surname: 'Customer', }, address: { address_line_1: '123 ABC Street', address_line_2: 'Apt 2', admin_area_2: 'San Jose', admin_area_1: 'CA', postal_code: '95121', country_code: 'US', }, }, purchase_units: [{ reference_id: "PUHF", description: "Sporting Goods", custom_id: "CUST-HighFashions", soft_descriptor: "HighFashions", amount: { currency_code: "USD", value: "220.00", breakdown: { item_total: { currency_code: "USD", value: "180.00" }, discount: { currency_code: "USD", value: "10.00" }, shipping: { currency_code: "USD", value: "30.00" }, handling: { currency_code: "USD", value: "10.00" }, tax_total: { currency_code: "USD", value: "20.00" }, shipping_discount: { currency_code: "USD", value: "10.00" } } }, items: [{ name: "T-Shirt", description: "Green XL", sku: "sku01", unit_amount: { currency_code: "USD", value: "90.00" }, tax: { currency_code: "USD", value: "10.00" }, quantity: "1", category: "PHYSICAL_GOODS" }, { name: "Shoes", description: "Running, Size 10.5", sku: "sku02", unit_amount: { currency_code: "USD", value: "45.00" }, tax: { currency_code: "USD", value: "5.00" }, quantity: "2", category: "PHYSICAL_GOODS" } ], shipping: { method: "United States Postal Service", name: { full_name: "John Doe" }, address: { address_line_1: "123 Townsend St", address_line_2: "Floor 6", admin_area_2: "San Francisco", admin_area_1: "CA", postal_code: "94107", country_code: "US" } } }] }) }, onApprove: function(data, actions) { return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', { method: 'post' }).then(function(res) { return res.json(); }).then(function(orderData) { var errorDetail = Array.isArray(orderData.details) && orderData.details[0]; if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') { return actions.restart(); } if (errorDetail) { var msg = 'Sorry, your transaction could not be processed.'; if (errorDetail.description) msg += '\n\n' + errorDetail.description; if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')'; return alert(msg); } console.log('Capture result', orderData, JSON.stringify(orderData, null, 2)); var transaction = orderData.purchase_units[0].payments.captures[0]; alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details'); }); } }).render('#paypal-button-container')
关键注意点
- 位置正确:payer对象属于订单顶层属性,和purchase_units数组同级,不要嵌套到purchase_units内部
- 字段规范:确保所有地址字段符合PayPal v2 API的命名要求(比如admin_area_1对应州/省,admin_area_2对应城市)
- 账户优先级:如果用户已登录PayPal,平台可能会优先使用其账户内保存的地址,此时预填充的地址不会显示在界面,但订单数据仍会包含你传入的payer信息
内容的提问来源于stack exchange,提问作者Fred
相关产品推荐
相关产品推荐

