【问题标题】:Stripe -- Customer's default payment method is not in the card objectStripe -- 客户的默认付款方式不在卡对象中
【发布时间】:2021-03-29 10:45:08
【问题描述】:

我对如何组织代码有疑问,并且在过去 48 小时内无法弄清楚如何解决此问题。我的大脑基本上被卡住了,我想帮助我找出解决方案。我在服务器上使用 expressjs,在前台使用 Svelte。

问题: 我能够创建客户、paymentMethods、paymentIntents 并将 paymentMethod 附加到客户,将所述 paymentMethod 设置为其客户的默认 paymentMethod...等等

要显示客户的付款方式,我必须使用 Stripe.paymentMethods.list 来返回一组卡片。卡对象不包含任何表明该卡是客户默认付款方式的字段。

存储默认付款方式的 default_payment_method 位于客户对象中。所以我需要获取客户对象,这就是我需要帮助的地方。我该如何解决这个问题,如何访问 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({})) 有两个单独的函数,我可以将它们结合起来并返回一个返回吗?

【问题讨论】:

    标签: javascript express stripe-payments


    【解决方案1】:

    看起来您已经完成了 80% 的任务。您绝对可以返回这两个对象,但我可能建议只返回您需要的特定信息。不要在.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}))

    【讨论】:

    • 谢谢。有用。我最终得到了两个服务器处理程序来返回每个对象,并将它们组合在前面,但工作量很大。现在我知道如何以简单的方式做到这一点:) ...我尝试更多地使用 async/await,但我仍然没有 100% 意识到它的好处。星号很受欢迎——盯着屏幕看了这么久,我找不到我的错字(我写这个问题很沮丧,我没有注意我输入的内容)......非常感谢你。
    • 很高兴它有帮助:)
    猜你喜欢
    • 2021-02-14
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2016-11-05
    • 2015-03-01
    相关资源
    最近更新 更多