【问题标题】:Stripe does not let a charge be associated with a customer with one-time sourceStripe 不允许将费用与一次性来源的客户相关联
【发布时间】:2018-08-21 11:06:37
【问题描述】:

正如标题所说,我正在尝试使用未保存在客户资料中的一次性来源(信用卡)进行付款。我仍然希望能够在 Stripe 上的客户资料中记录该费用。

根据the docs,这可以通过在发送到Stripe.customers.createCharge 的有效负载中传递customer 中的客户id 密钥来完成。但是,在这样做时,我收到一条错误消息,指出该卡未与客户关联(显然不是,而且我不希望这样)。

客户cus_***没有ID tok_visa的卡

为了解决这个问题,我暂时应用了in this answer 提到的修复程序,它基本上涉及为支付创建一个临时卡,然后将其删除。

我想知道当 API 以其他方式明确记录时,它是如何不起作用的,希望有使用 Stripe 经验的人也能提出相同的意见。

这是我要运行的代码:

await stripeService.charges.create({
    source: token, // temporary token received from Checkout
    customer: user.stripeCustomerId, // the Stripe customer ID from my database
    amount,
    currency: 'usd',
    description: 'Test charge'
});

【问题讨论】:

    标签: node.js stripe-payments


    【解决方案1】:

    您明确链接到的一次性源文档仅适用于Sources,但tok_visa 将创建一个Card 对象。我相信这就是您收到错误的原因。

    如果您尝试使用您通过前端的 Elements/Checkout 获得的源代码(它的 ID 类似于“src_xxx”),例如 createSource,它将成功,我刚刚测试过.您也可以使用此代码进行测试:

    const src = await stripe.sources.create({
        type : "card",
        token : "tok_visa"
    });
    
    const charge = await stripe.charges.create({
        source : src.id,
        customer : "cus_xxxx",
        amount : 1000,
        currency : "usd"
    });
    

    【讨论】:

    • 啊,我明白了,是否有任何测试源 ID(例如卡的 tok_visa)以便我进行测试?
    • @aditya_m 不,但您可以从测试令牌创建源 (stripe.com/docs/api#create_source-token) 我编辑了答案以显示更完整的示例
    • 谢谢,我试过了,效果很好。只有两个问题:1)来源Usage显示为reusable,对吗? (Statusconsumed,反正不会再收费了),2)你提供的测试代码(stripe.sources.create),只是用来测试创建测试令牌的吧?在我的生产代码中,我可以直接将收到的令牌传递给charges.create 调用source 键?
    • 1) 没错,源是card 类型,并且卡片是可重复使用的。但是在这种情况下,除非您先附加到客户,否则您不能再次收费,而您没有这样做。你会收到一条错误消息。 2)是的,这只是为了测试。在生产环境中,您应该将您创建的 source 作为source 键传递——正如我所说,这不适用于令牌。因此,您需要确保在前端创建源,方法是使用 Elements 调用 createSource,或者在 Checkout 中使用 source callback
    • 有道理,谢谢。仅供参考,我正在使用 React Native 和 tipsi-stripe 来集成它,将调用 createSourceWithParams (tipsi.github.io/tipsi-stripe/docs/…) 和 type: 'card' 来完成它。将您的答案标记为已接受。
    【解决方案2】:

    我能够通过首先创建源,然后是您指定 customerId 和 sourceId 的费用来实现这一点,如下所示:

                //Create the source first
                var options = new SourceCreateOptions
                {
                    Type = SourceType.Card,
                    Card = new CreditCardOptions
                    {
                        Number = Number,
                        ExpYear = ExpYear,
                        ExpMonth = ExpMonth,
                        Cvc = Cvc
                    },
                    Currency = "gbp"
                };
    
                var serviceSource = new SourceService();
                Source source = serviceSource.Create(options);
    
                //Now do the payment
                var optionsCharge = new ChargeCreateOptions
                {
                    Amount = 500,
                    Currency = "gbp",
                    Description = "Your description",
                    SourceId = source.Id,
                    CustomerId = "yourcustomerid"
                };
                var service = new ChargeService();
                service.Create(optionsCharge);
    
                result = "success";
    

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 2017-09-19
      • 2018-06-22
      • 2012-09-15
      • 2016-10-15
      • 2015-08-30
      • 2020-12-13
      • 2017-02-26
      • 2023-03-27
      相关资源
      最近更新 更多