我在应用程序中使用Stripe,并且还将付款意向ID用作存储客户订单明细的标识符。我写了一个webhook端点来更改客户订单状态,我想先在本地测试。
如何使用特定的payment_intent ID触发payment_intent.succeeded事件,以便可以从数据库中找到客户订单?
答案 0 :(得分:2)
无法用这样的特定付款方式触发它;您要做的是使用CLI的转发,但要按照流程实际创建测试付款。这将导致{/ {1}}事件的预期/已知的付款意图。
答案 1 :(得分:0)
在付款页面上成功付款后,编写UPDATE查询以更改订单状态。
为此,您需要在创建“付款意图”时存储order_id。使用元数据字段。
\Stripe\PaymentIntent::create([
'amount' => 1099,
'currency' => 'usd',
'payment_method_types' => ['card'],
'metadata' => [
'order_id' => '6735',
],
]);
当付款成功更新订单状态时,从“付款意图”中检索order_id。
答案 2 :(得分:0)
我有一个类似的问题,我想测试订购失败的付款。查看Stripe CLI固定装置,我使用了它们使用的默认json并创建了以下脚本。
/* eslint-disable @typescript-eslint/no-var-requires */
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')
const args = process.argv.slice(2)
if (args[0] !== '-customer') {
console.log('-customer argument missing')
process.exit(22)
}
if (args[1] === undefined) {
console.log('customer id required')
process.exit(22)
}
const failedInvoiceJson = {
_meta: {
template_version: 0
},
fixtures: [
{
name: 'invoiceitem',
path: '/v1/invoiceitems',
method: 'post',
params: {
amount: 2000,
currency: 'usd',
customer: `${args[1]}`,
description: '(created by Stripe CLI)'
}
},
{
name: 'invoice',
path: '/v1/invoices',
method: 'post',
params: {
customer: `${args[1]}`,
description: '(created by Stripe CLI)'
}
},
{
name: 'invoice_pay',
path: '/v1/invoices/${invoice:id}/pay',
method: 'post'
}
]
}
fs.writeFileSync(
path.join(process.cwd(), './test/failedInvoice.json'),
JSON.stringify(failedInvoiceJson)
)
exec('stripe fixtures test/failedInvoice.json', (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`)
fs.unlinkSync(path.join(process.cwd(), './test/failedInvoice.json'))
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
})
使用此脚本,我可以运行命令node test/failedInvoice.ts -customer xxx
,其中xxx
是数据库中现有条带用户的ID。
答案 3 :(得分:0)
从 the repository 中获取模板并更改 ${invoice:id}
之类的变量以使用环境变量,即 ${.env:INVOICE_ID}
。然后,您可以调用 INVOICE_ID=10 stripe fixtures yourfile.json
。