Stripe CLI 触发 invoice.payment_failed 事件的正确方式
在使用 Stripe CLI 触发 invoice.payment_failed 事件时,开发者可能会遇到一个常见问题:当尝试通过 --override 参数指定 subscription 时,命令会失败并返回错误信息 "You may only specify one of these parameters: pending_invoice_items_behavior, subscription"。
问题背景
Stripe CLI 是一个强大的命令行工具,允许开发者在测试环境中模拟各种支付事件。invoice.payment_failed 是一个重要的事件,表示发票支付失败,通常用于测试订阅服务的支付失败处理逻辑。
错误原因分析
这个错误的核心在于 Stripe API 的参数互斥性。默认情况下,Stripe CLI 会设置 pending_invoice_items_behavior 参数为 include,而当开发者尝试通过 --override 指定 subscription 参数时,就违反了 API 的规则:这两个参数不能同时存在。
解决方案
正确的做法是在命令中添加 --remove 开关来移除默认的 pending_invoice_items_behavior 参数。完整的命令示例如下:
stripe trigger \
invoice.payment_failed \
--override \
invoice:customer=cus_123 \
--override \
invoice:subscription=sub_123 \
--override \
payment_method:customer=cus_123 \
--override \
invoiceitem:customer=cus_123 \
--remove \
invoice:pending_invoice_items_behavior
完整实现示例
以下是一个完整的 Node.js 实现示例,展示了如何创建必要的测试数据并生成正确的 CLI 命令:
const stripe = require('stripe')('sk_test_XXX');
async function setupTestEnvironment() {
// 创建测试客户
const customer = await stripe.customers.create({
name: "测试用户",
email: "test@example.com"
});
// 创建支付方式并附加到客户
const paymentMethod = await stripe.paymentMethods.create({
type: "card",
card: { token: "tok_visa" }
});
await stripe.paymentMethods.attach(paymentMethod.id, {
customer: customer.id
});
// 设置默认支付方式
await stripe.customers.update(customer.id, {
invoice_settings: {
default_payment_method: paymentMethod.id
}
});
// 创建测试价格
const price = await stripe.prices.create({
currency: "usd",
unit_amount: 1000,
product_data: { name: "测试产品" },
recurring: { interval: "month" }
});
// 创建测试订阅
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ price: price.id }]
});
// 生成正确的CLI命令
const cliCommand = [
"stripe trigger invoice.payment_failed",
"--override invoice:customer=" + customer.id,
"--override invoice:subscription=" + subscription.id,
"--override payment_method:customer=" + customer.id,
"--override invoiceitem:customer=" + customer.id,
"--remove invoice:pending_invoice_items_behavior"
].join(" ");
console.log("可执行的CLI命令:");
console.log(cliCommand);
}
setupTestEnvironment();
最佳实践建议
-
测试数据隔离:为每个测试场景创建独立的测试数据,避免测试间的相互影响。
-
参数验证:在使用 Stripe CLI 时,仔细检查参数的互斥性,特别是当使用 --override 和 --remove 时。
-
事件顺序:理解 Stripe 事件的生命周期,确保触发的事件顺序符合实际业务场景。
-
错误处理:在自动化测试脚本中加入对 CLI 命令执行结果的检查,确保事件触发成功。
通过遵循这些指导原则,开发者可以更有效地使用 Stripe CLI 来测试支付失败场景,确保应用程序能够正确处理这类事件。
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0197
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0126
MiMo-V2.5-Pro-FP4-DFlashMiMo-V2.5-Pro-FP4-DFlash 是驱动 MiMo-V2.5-Pro-UltraSpeed 的底层模型: FP4 量化骨干网络:对 MoE 专家采用 MXFP4 量化,同时保持模型其他部分的更高精度,在几乎无损质量的前提下,显著减小模型体积并降低内存带宽压力。 BF16 DFlash 草稿生成器:用于块扩散推测解码,每次前向传播可生成一整个块的 tokens,并让骨干网络一步完成验证。 两者协同作用,既降低了每参数的位宽,又减少了骨干网络前向传播的次数,而这两者正是万亿参数模型解码过程中的两大主要成本来源。Python00
JoyAI-EchoJoyAI-Echo,这是一个独立的、仅用于推理的版本,旨在实现分钟级多镜头音视频生成。它采用了经过蒸馏的DMD生成器、配对的跨模态记忆以及故事级别的一致性。其性能的核心在于,一个跨模态视听记忆库能够在长达五分钟的视频中保持角色外观和语音音色的一致性。同时,一个训练后处理流程将基于记忆的强化学习与分布匹配蒸馏相结合,实现了7.5倍的速度提升,显著增强了视觉质量和对齐效果。00
AstrBot✨ 易上手的多平台 LLM 聊天机器人及开发框架 ✨ 平台支持 QQ、QQ频道、Telegram、微信、企微、飞书 | OpenAI、DeepSeek、Gemini、硅基流动、月之暗面、Ollama、OneAPI、Dify 等。附带 WebUI。Python06
handy-ollama动手学Ollama,CPU玩转大模型部署,在线阅读地址:https://datawhalechina.github.io/handy-ollama/Jupyter Notebook07