Stripe payments 条带-只需两次调用即可创建客户和保存汇总卡数据

Stripe payments 条带-只需两次调用即可创建客户和保存汇总卡数据,stripe-payments,Stripe Payments,我正在使用stripe将信用卡保存为付款方式,然后将信用卡摘要详细信息保存到我自己的数据库中 下面的代码正在执行此操作 但是,它需要三个电话 当我创建一个客户时,有没有一种方法可以从中获取汇总卡数据,或者有没有其他方法可以在两次呼叫中实现这一点 stripe.customers.create({ payment_method: req.body.paymentMethodID }).then(stripeCustomer => {

我正在使用stripe将信用卡保存为付款方式,然后将信用卡摘要详细信息保存到我自己的数据库中

下面的代码正在执行此操作

但是,它需要三个电话

当我创建一个客户时,有没有一种方法可以从中获取汇总卡数据,或者有没有其他方法可以在两次呼叫中实现这一点

    stripe.customers.create({
        payment_method: req.body.paymentMethodID
        }).then(stripeCustomer => {     

            stripe.paymentMethods.list({customer: stripeCustomer.id, type: 'card'}).then(cardDetails => {

                User.findByIdAndUpdate(purchaser, {
                    stripeCustomerID: stripeCustomer.id,
                    savedPaymentDetails: {
                        card: cardDetails.data[0].card.brand,
                        last4: cardDetails.data[0].card.last4,
                        expiryMonth: cardDetails.data[0].card.exp_month,
                        expiryYear: cardDetails.data[0].card.exp_year
                    },

                }).then()               
            })
        })
前端的代码是:

const cardElement = this.props.elements.getElement('card');

    axios.get(`${process.env.REACT_APP_API}/saveCardDetails`).then(res => {

        this.props.stripe.confirmCardSetup(res.data.client_secret, {payment_method: {card: cardElement}}).then( confirmCardSetupRes => {

            if (confirmCardSetupRes.setupIntent.status === 'succeeded'){

                    axios.post(`${process.env.REACT_APP_API}/paymentIntent`, objectToSend).then(res => {                    
                        this.props.upDateMessage(res.data.message)

                    })
            }
        })
    })
返回使用包含
setupIntent
错误的对象解析的承诺。如果成功,该
setupIntent
对象将有一个属性
payment\u method
,默认情况下,该属性是新PaymentMethod的ID(形式为“pm\u xxx”的标记化支付详细信息)

通常情况下,企业希望向客户展示与用于购买的信用卡相关的最新信息、品牌或其他非敏感支付详细信息。正如您所指出的,您可以通过按客户和卡类型筛选来检索这些详细信息

在服务器上进行单独的fetch调用的另一种方法是使用API的功能“水合物”或“扩展”PaymentMethod的完整对象。我们知道PaymentMethod是可扩展的,通过查看SetupIntent的完整API参考,并看到PaymentMethod旁边的“可扩展”标签

我建议更新对
stripe.confirmCardSetup
的调用,为
payment\u方法
添加扩展参数,这样,生成的
setupIntent
对象将具有生成的PaymentMethod的完整卡详细信息

作为参考,这可能类似于:

stripe.confirmCardSetup(
  '{{CLIENT_SECRET}}', {
  payment_method: {
    card: cardElement,
    billing_details: {
      name: 'Jenny Rosen',
    },
  },
  expand: ['payment_method'],
});
使用
expand:['payment\u method']
生成的SetupIntent将包含更多数据。以下是在成功案例中解决承诺的目标示例:

{
    "id": "seti_1GrRu6CZ6qsJgndJLpV8r95t",
    "object": "setup_intent",
    "cancellation_reason": null,
    "client_secret": "seti_1GrRu6CZ6qsJgndJLpV8r95t_secret_HQIVFzRdJsagSr88ElPvQiHViWZlPm4",
    "created": 1591549406,
    "description": null,
    "last_setup_error": null,
    "livemode": false,
    "next_action": null,
    "payment_method": {
        "id": "pm_1GrRuSCZ6qsJgndJhegm4jig",
        "object": "payment_method",
        "billing_details": {
            "address": {
                "city": null,
                "country": null,
                "line1": null,
                "line2": null,
                "postal_code": "23232",
                "state": null
            },
            "email": null,
            "name": "Jenny Rosen",
            "phone": null
        },
        "card": {
            "brand": "visa",
            "checks": {
                "address_line1_check": null,
                "address_postal_code_check": null,
                "cvc_check": null
            },
            "country": "US",
            "exp_month": 2,
            "exp_year": 2032,
            "funding": "credit",
            "generated_from": null,
            "last4": "4242",
            "three_d_secure_usage": {
                "supported": true
            },
            "wallet": null
        },
        "created": 1591549429,
        "customer": null,
        "livemode": false,
        "metadata": {},
        "type": "card"
    },
    "payment_method_types": [
        "card"
    ],
    "single_use": null,
    "status": "succeeded",
    "usage": "off_session"
}
对于您的具体情况,我建议将PaymentMethod的ID之外的一些细节传递回服务器。e、 g:

objectToSend.paymentMethodID = confirmCardSetupRes.setupIntent.payment_method.id;
objectToSend.card = confirmCardSetupRes.setupIntent.payment_method.card;

axios.post(`${process.env.REACT_APP_API}/paymentIntent`, objectToSend).then(res => {                    
  this.props.upDateMessage(res.data.message)
})

您能否共享传递paymentMethodID的前端代码?当您在客户机上标记付款方式时,您应该能够提取该摘要数据,并且不需要再次列出付款方式。感谢您的回复。我已使用前端代码更新了问题。注意到此修复程序存在问题。它不适用于需要身份验证的卡。这些卡的附加数据在SetUpIntent中不可用。有没有办法解决这个问题,或者我必须在后面重新介绍这个额外的电话end@Colfah我也在同一个十字路口,但我想我可能会将payment_方法传递给服务器,在服务器路径中,我将进行payment_retrieve()以获取所有这些数据,然后将这些详细信息保存到db或其他任何地方。看起来它现在也适用于需要身份验证的卡,因此如果使用此修复程序,您可以保存自己的通话