简体   繁体   English

Stripe 不会让费用与具有一次性来源的客户相关联

[英]Stripe does not let a charge be associated with a customer with one-time source

As the title says, I am trying to make a payment using a one-time source (credit card) that is not being saved in the customer's profile.正如标题所说,我正在尝试使用未保存在客户个人资料中的一次性来源(信用卡)进行付款。 I still want to be able to record that charge on the customer's profile on Stripe.我仍然希望能够在 Stripe 上的客户个人资料中记录该费用。

According to the docs , this can be done by passing the customer's id in customer key in the payload sent to Stripe.customers.createCharge .根据文档,这可以通过在发送到Stripe.customers.createCharge的有效负载中的customer密钥中传递客户的idStripe.customers.createCharge However, on doing that, I receive an error stating the card is not linked with the customer (which obviously is not, and I don't want it to be).但是,在这样做时,我收到一条错误消息,指出该卡未与客户相关联(显然不是,而且我不希望它如此)。

Customer cus_*** does not have card with ID tok_visa客户cus_***没有 ID tok_visa 的卡

To get around this, temporarily I have applied the fix mentioned in this answer which basically involves creating a temporary card for the payment and then deleting it.为了解决这个问题,我暂时应用了这个答案中提到的修复程序它基本上涉及为付款创建一个临时卡,然后将其删除。

I was wondering how does the API not work when it is clearly documented otherwise, hope someone experienced with Stripe chimes in on the same.我想知道当 API 清楚地记录下来时,它是如何不起作用的,希望有使用 Stripe 经验的人也能提出同样的意见。

Here is the code I'm trying to get to run:这是我试图运行的代码:

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'
});

The single-use sources document that you link to explicitly only works with Sources , but tok_visa will create a Card object instead.您明确链接到的一次性源文档仅适用于Sources ,但tok_visa将创建一个Card对象。 I believe that's why you get the error.我相信这就是你得到错误的原因。

If you try the code with a Source(it has an ID like 'src_xxx') that you obtain through Elements/Checkout on your frontend with, for example, createSource , it will succeed, I've just tested it.如果您尝试使用通过前端的 Elements/Checkout 获得的 Source(它有一个像“src_xxx”这样的 ID)的代码,例如createSource ,它将成功,我刚刚对其进行了测试。 You can also test with this code :您还可以使用此代码进行测试:

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"
});

I was able to achieve this by creating the source first followed by the charge where you specify the customerId and sourceId as below:我能够通过首先创建源然后在指定 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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM