【问题标题】:Error Invalid Token when creating new Stripe source创建新的 Stripe 源时出错 Invalid Token
【发布时间】:2018-11-24 22:49:32
【问题描述】:

使用 Stripe 元素和 Stripe.net。我获得令牌并创建客户和来源并使用以下代码对其收费 - 工作正常

            StripeConfiguration.SetApiKey(stripeKey);
            var charges = new StripeChargeService();
            var charge = charges.Create(new StripeChargeCreateOptions
            {
                Amount = thisCharge.Amount,
                Currency = "usd",
                Description = thisCharge.ProductDesc,
                StatementDescriptor = thisCharge.ProductDesc,
                //SourceTokenOrExistingSourceId = source.Id,

                ReceiptEmail = thisCharge.EmailAddress,
                CustomerId = thisCharge.StripeCustomerID,
                Metadata = new Dictionary<String, String>()
                    {
                        { "Name", thisCharge.CardHolderName }, { "Email",thisCharge.EmailAddress }
                    }
            });

然后我尝试使用另一张卡 (5555 5555 5555 4444) 与同一客户进行第二次收费,但显然我遗漏了一些东西。

使用此代码

    static StripeSource NewSource(string stripeKey, Charge  charge)
    {
        StripeConfiguration.SetApiKey(stripeKey);
        var sourceOptions = new StripeSourceCreateOptions
        {
            Type = StripeSourceType.Card,
            Currency = "usd",
            Amount = charge.Amount,
            Token = charge.Token,              

            Owner = new StripeSourceOwner
            {
                Email = charge.EmailAddress,
                Name = charge.CardHolderName
            }
        };
        var sourceService = new StripeSourceService();
        StripeSource source = sourceService.Create(sourceOptions);
        return source;
    }

但我收到此错误:

无效的令牌 ID:src_....

如果我跳过添加新来源,那么费用会通过,但会针对原始来源。

所以显然遗漏了一些东西......感谢任何帮助。

【问题讨论】:

  • 嗯,在上面我看到你有代码来创建一个费用,然后创建一个源(从一个令牌)。您是否在尝试收费之前将新来源附加到客户?如果您还没有这样做,我认为这可能是您的问题。 stripe.com/docs/api/sources/attach?lang=dotnet
  • @duck - 我想通了 - 当我提交表单时,我得到一个令牌“src_”,它已经是一个来源。所以我不需要创建一个新的来源——也不能用那个令牌。我需要做的是将该来源添加给客户。呵呵。

标签: asp.net-mvc stripe-payments


【解决方案1】:

就我而言,但以防万一其他人有同样的问题。

当您提交元素表单时,它会返回一个令牌,在本例中是源令牌“src_”。因此,您无需创建新来源,只需将其分配给现有客户即可。

            StripeConfiguration.SetApiKey(stripeKey);
            var options = new StripeCustomerUpdateOptions
            {
                SourceToken = charge.Token,
            };

            var service = new StripeCustomerService();
            StripeCustomer customer = service.Update(charge.StripeCustomerID, options);

【讨论】:

  • 感谢您的“duh”时刻。在我读到你的之前,我已经有了自己的 ;-)
猜你喜欢
  • 2022-12-28
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2017-11-09
  • 1970-01-01
  • 2015-12-10
  • 2020-03-14
相关资源
最近更新 更多