条带 -- 客户的默认付款方式不在卡对象中



我遇到了如何组织代码的问题,在过去的48小时里一直无法解决这个问题。我的大脑基本上被卡住了,我想帮助如何找到解决方案。我使用expressjs作为服务器,使用Svelte作为前端。

问题:我可以创建customers、paymentMethod、paymentIntents并将paymentMethod附加到customer,将所述paymentMethod设置为其客户的默认paymentMethod。。。etc

为了显示客户的付款方式,我必须使用Stripe.paymentmethods.list,它返回一组卡片。卡对象不包括任何指示该卡是客户默认支付方式的字段。

存储默认付款方法的default_payment_method位于customer对象中。所以我需要得到客户的支持,而这正是我需要帮助的地方。我该如何解决这个问题,如何访问Stripe.paymentMethods.list((来获得卡的列表和Stripe.customer.retrieve((,以便我可以访问服务器句柄中的defaultpaymentmethod,以便将所有信息返回到前端?

// get cards
stripe.paymentMethods.list({
customer: customer,
type: 'card'
}).then(cards => {
//I can return cards but not customer
return res.end(JSON.stringify({ cards: cards}));
}).catch(err => { console.log("err message :", err.message)});
});    
// now get customer
stripe.customers.retrieve(
'cus_Ib01d7QtMdz2ez'
).then(customer=>{ 
// I can return customer but not cards
return res.end(JSON.stringify({customer : customer}));
});
// next step, send customer and cards to the frontst 
return res.end(JSON.Stringfy({cards : cards; customer : customer}))
?????

如何返回";卡片和顾客";对象,这样我就可以向客户展示他/她的卡,以及哪一种是默认的支付方式。如何使用res.end(JSON.stringfy({}((对于这两个单独的函数,我可以将它们合并并返回一个返回吗?

看起来你已经完成了80%的工作。你肯定可以同时返回两个对象,不过我可能建议你只返回你需要的特定信息。使用async/await获取两个响应,而不是在.then()块中进行响应。

const cards = await stripe.paymentMethods.list({
customer: 'cus_123',
type: 'card'
})
const customer = stripe.customers.retrieve(
'cus_123'
)
return res.send(JSON.Stringify({cards : cards, customer : customer}))
// or, send json directly
// return res.json({cards : cards, customer : customer})

(另外,请注意,我用逗号,替换了分号;,并更正了拼写错误(:

return res.end(JSON.String**i**fy({cards : cards**,** customer : customer}))

最新更新