Stripe定期支付与发票:自定义金额月度捐赠表单实现问询
嘿,针对你开发自定义金额月度定期捐赠表单的需求,结合Stripe生态,我来详细拆解你提到的选项1实现方案,顺便补充一些实践细节:
方案1:0美元订阅+可变金额扣费流程
这个方案核心是用0美元订阅绑定客户的支付方式,然后每月通过添加可变金额的发票项来实现自定义捐赠扣费,流程清晰且符合Stripe的订阅逻辑:
步骤1:创建Stripe客户
首先需要将用户的支付信息(通过Stripe Token)关联到一个客户对象,方便后续订阅和扣费操作:
$customer = Customer::create(array( "card" => $_POST['stripe_token'], // 前端Stripe.js生成的支付令牌 "description" => "Monthly Donation Customer", "email" => $_POST['email'], "metadata" => array( "donor_name" => $_POST['name'], // 存储捐赠者信息,方便后续追溯 "preferred_donation_amount" => $_POST['donation_amount'] // 记录用户自定义的捐赠金额 ) ));
注意:这里的metadata非常有用,可以存储捐赠者的自定义信息,后续生成发票或者对账时能快速关联
步骤2:创建并订阅0美元计划
你需要先在Stripe后台创建一个0美元的月度订阅计划(比如命名为monthly-donation-base),然后将客户订阅到这个计划:
$subscription = Subscription::create(array( "customer" => $customer->id, "plan" => "monthly-donation-base", // 后台创建的0美元月度计划ID "payment_behavior" => "default_incomplete", // 处理支付失败的策略,可选 "expand" => ["latest_invoice.payment_intent"] ));
这个0美元计划的作用是绑定客户的支付方式到定期扣费周期,相当于一个“容器”来承载每月的可变捐赠金额。
步骤3:添加可变金额发票项并生成发票
订阅创建后,你可以给当前周期的发票添加自定义金额的发票项,然后完成扣费:
// 获取当前订阅的最新发票 $invoice = Invoice::retrieve($subscription->latest_invoice->id); // 添加自定义捐赠金额的发票项 InvoiceItem::create(array( "customer" => $customer->id, "invoice" => $invoice->id, "amount" => $_POST['donation_amount'] * 100, // Stripe金额单位为分,所以要乘以100 "currency" => "usd", "description" => "Monthly Donation - Custom Amount" )); // 最终确认并支付发票 $invoice->pay();
后续月度自动扣费逻辑
每个月Stripe会自动生成新的0美元订阅发票,此时你需要通过Stripe Webhook监听invoice.created事件,在事件触发时自动添加对应金额的发票项,然后让系统自动完成支付:
// 示例Webhook处理逻辑(伪代码) if ($event->type == 'invoice.created') { $invoice = $event->data->object; $customer = Customer::retrieve($invoice->customer); // 从客户元数据中获取预设的捐赠金额 $donationAmount = $customer->metadata['preferred_donation_amount'] * 100; // 添加发票项 InvoiceItem::create(array( "customer" => $invoice->customer, "invoice" => $invoice->id, "amount" => $donationAmount, "currency" => "usd", "description" => "Monthly Donation - Recurring Custom Amount" )); // 可选:自动支付发票 $invoice->pay(); }
方案优势
- 完全符合Stripe的订阅模型,后续管理(比如修改捐赠金额、取消订阅)都可以通过Stripe Dashboard或API轻松操作
- 利用Webhook实现自动化,无需手动触发每月扣费
- 可以通过客户元数据灵活调整捐赠金额,支持用户后续修改
内容的提问来源于stack exchange,提问作者daugaard47
相关产品推荐
相关产品推荐

