Stripe PaymentIntent成功事件无Invoice ID,如何获取?
解决Stripe payment_intent.succeeded事件中invoice字段为null的问题
你当前使用的是Checkout的payment模式(一次性支付),这种模式默认不会自动生成发票,因此Payment Intent没有关联的Invoice,导致payment_intent.succeeded事件里的invoice字段为null。要让该事件包含Invoice ID,只需在创建Checkout Session时启用自动生成发票的配置即可。
修改后的Checkout Session创建代码:
const session = await stripe.checkout.sessions.create({ mode: 'payment', customer: item.stripe_customer_id, line_items: [ { price: price.id, quantity: 1, }, ], payment_intent_data: { description: 'Extra messages' }, // 启用自动创建发票,并可自定义发票信息 invoice_creation: { enabled: true, invoice_data: { description: 'Invoice for extra messages', footer: 'Thanks for your purchase' } }, success_url: `${process.env.FRONTEND_URL}/dashboard/invoices?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${process.env.FRONTEND_URL}`, });
原理说明:
添加invoice_creation.enabled: true后,Stripe会在用户完成支付时自动生成对应的Invoice,并将其关联到本次交易的Payment Intent上。此时你收到的payment_intent.succeeded事件中,invoice字段就会填充该发票的ID。
你也可以通过invoice_data字段自定义发票的描述、页脚等信息,根据业务需求调整即可。
内容的提问来源于stack exchange,提问作者Gia Nebieridze
相关产品推荐
相关产品推荐

