Stripe v14.18.0中stripe.customers.cancel不存在的排查求助
运行Cypress测试调用Stripe用户清理函数时,出现以下500错误:
Body: { "status": 500, "message": "Error while cleaning the stripe test data", "error": "stripe.customers.cancel is not a function" }
之前看到相关讨论提到需要使用9.x及以上版本的stripe-node,但在包安装目录执行npm list确认当前版本是v14.18.0,且已排查无全局包冲突,这个问题已经阻塞了测试,求可行的排查方向。
附调用的测试数据清理函数:
const cleanAllCustomersExcept = async function cleanAllCustomersExcept(customersToKeep) { // Delete customers const customers = await stripe.customers.list(); console.log(stripe.customers); const customersList = customers.data; await Promise.all( customersList.map( async (customer) => (customersToKeep.includes(customer.id) ? null : stripe.customers.cancel(customer.id) .then(() => { console.log(`Deleting customer ${customer.id}`); })), ), ); if (customers.has_more) { return cleanAllCustomersExcept(customersToKeep); } console.log('Done removing all customers'); return null; }
排查方向
核心问题:用错了API方法
Stripe Node SDK里删除客户的正确方法是stripe.customers.del(customer.id),cancel是订阅对象(stripe.subscriptions.cancel)的方法,根本不是客户对象的API,这才是报错的真正原因,和版本无关。验证SDK可用方法
可以通过代码里的console.log(stripe.customers)输出,确认客户对象的可用方法,你会发现只有del没有cancel,直接验证方法名错误。修复递归分页逻辑
当前函数的递归调用没处理分页参数,stripe.customers.list()默认只返回前10条数据,需要传递starting_after参数遍历所有客户,否则会漏删。修正后的调用应该是:const customers = await stripe.customers.list({ starting_after: customersList.length ? customersList.at(-1).id : undefined });确认Cypress运行环境的SDK版本
虽然本地npm list显示版本正确,但Cypress可能在独立进程(比如插件或测试沙箱)中运行,建议在测试代码里打印console.log(stripe.VERSION),确认实际运行时的版本是否和预期一致。
内容的提问来源于stack exchange,提问作者ipenguin67

