如何通过API创建Stripe支付链接时预填充客户姓名、邮箱与电话
实现Stripe支付链接预填充客户信息的方案
你可以通过两种方式实现支付页面预填充客户姓名、邮箱和电话,同时允许客户按需编辑:
方法一:创建Stripe客户对象并关联(推荐留存客户信息场景)
先通过已有客户信息创建Stripe Customer对象,再将其关联到支付链接,支付页面会自动填充该客户的信息并允许编辑。
修改你的代码如下:
// 1. 先创建Stripe客户对象 const customer = await stripe.customers.create({ name: req.body.customer.name, email: req.body.customer.email, phone: req.body.customer.phone, // 注意格式需包含国家代码,如+44xxxxxx }); const products = await stripe.products.create(req.body.product) const price = await stripe.prices.create({ ...req.body.price, currency: "gbp", product: products.id, }); const paymentLink = await stripe.paymentLinks.create({ line_items: [ { price: price.id, quantity: 1, } ], metadata: { phone_order_id: String(req.body.phone_order.id) }, restrictions: { completed_sessions: { limit: 1 } }, // 2. 关联客户并允许编辑信息 customer: customer.id, customer_creation: "always", // 强制允许客户编辑预填充的信息 ...(franchisee ? { transfer_data: { destination: process.env.WE_GREET_STRIPE_AC_ID as string, amount: req.body.transfer_data.amount } } : {}) });
方法二:直接在支付链接URL后追加预填充参数(轻量临时场景)
无需提前创建客户对象,直接在生成的支付链接URL后拼接预填充参数即可,同时确保支付链接允许客户信息编辑。
修改代码如下:
const products = await stripe.products.create(req.body.product) const price = await stripe.prices.create({ ...req.body.price, currency: "gbp", product: products.id, }); const paymentLink = await stripe.paymentLinks.create({ line_items: [ { price: price.id, quantity: 1, } ], metadata: { phone_order_id: String(req.body.phone_order.id) }, restrictions: { completed_sessions: { limit: 1 } }, // 确保允许客户编辑信息 customer_creation: "always", ...(franchisee ? { transfer_data: { destination: process.env.WE_GREET_STRIPE_AC_ID as string, amount: req.body.transfer_data.amount } } : {}) }); // 拼接预填充参数,注意编码特殊字符 const prefilledPaymentUrl = `${paymentLink.url}?prefilled_name=${encodeURIComponent(req.body.customer.name)}&prefilled_email=${encodeURIComponent(req.body.customer.email)}&prefilled_phone=${encodeURIComponent(req.body.customer.phone)}`;
关键注意事项
- 电话格式必须包含国际国家代码(如英国号码为
+44xxxxxx),否则无法正确预填充。 - 使用
encodeURIComponent对客户信息进行编码,避免特殊字符(如空格、@)导致URL失效。 customer_creation: "always"确保客户可以修改预填充的信息,若省略则默认值为if_required,仅当支付流程要求客户信息时生效。
内容的提问来源于stack exchange,提问作者Siam Ahnaf
相关产品推荐
相关产品推荐

