![](/img/trans.png)
[英]How to disable default payment method (Credit Card) in Stripe Checkout
[英]Stripe — Customer's default payment method is not in the card object
我對如何組織代碼有疑問,並且在過去 48 小時內無法弄清楚如何解決此問題。 我的大腦基本上被卡住了,我想幫助我找出解決方案。 我將 expressjs 用於服務器,將 Svelte 用於前端。
問題:我能夠創建客戶、paymentMethods、paymentIntents 並將 paymentMethod 附加到客戶,將所述 paymentMethod 設置為其客戶的默認 paymentMethod...等
要顯示客戶付款方式,我必須使用返回一組卡片的 Stripe.paymentMethods.list。 object 卡不包含任何表明該卡是客戶默認付款方式的字段。
存儲默認支付方式的 default_payment_method 位於客戶 object 中。 所以我需要找到客戶 object,這就是我需要幫助的地方。 我該如何解決這個問題,如何訪問 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% 的路。 您絕對可以同時返回 object,盡管我可能建議只返回您需要的特定信息。 不要在.then()
塊中響應,而是使用async/await
來獲取兩個響應。
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}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.