Stripe 支付集成错误 'No such token'

Stripe Payment integration error 'No such token'

我想将 Stripe 集成到我的 ASP.NET MVC 应用程序中。 所以,作为第一步,我尝试用简单的代码来完成。

下面是我的简单代码,

        var customers = new CustomerService();
        var charges = new ChargeService();

        var publishableKey = "pk_test_51KseuBH1Mw1tyjglBiJls20038FcgbHr";
        StripeConfiguration.SetApiKey(publishableKey);
        StripeConfiguration.ApiKey = "sk_test_51KseuRqoYQ6YGD7TNzPp0051boOPvQ";
        var customer = customers.Create(new CustomerCreateOptions
        {
            Email = "isanka.ad@gmail.com",
            Source = publishableKey
        });

        var charge = charges.Create(new ChargeCreateOptions
        {
            Amount = 500,
            Description = "Sample Charge",
            Currency = "usd",
            Customer = customer.Id
        });

我在有孔虫上尝试过不同的代码。但它总是returns下面同样的错误,

No such token: 'pk_test_51KseuBHaMw1tyjglBiJls20038FcgbHr'

这是我的条纹键,

我是否错误地定义了密钥和可发布密钥?

您的后端代码中不得使用可发布密钥。它适用于您的 client-side 代码,如文档中所述:https://stripe.com/docs/keys. Moreover, the Source property on the customer describes the payment method: https://stripe.com/docs/api/customers/create?lang=dotnet. I can recommend getting started with Stripe with the quick start guide: https://stripe.com/docs/payments/quickstart.

您似乎只复制了 API 键的第一行。

请确保您在应用程序中使用完整的 API 密钥,并且不需要在后端集成中使用可发布的密钥。

source 值仅接受通过令牌或来源 API 创建的支付来源。

在您的示例代码中创建客户时,Source 被分配了无效的 publishableKey,这可能就是返回“没有此类令牌”的原因。

如果您没有预先创建任何来源,则应从您的客户创建请求中删除 Source

我没有创建卡片和令牌 产生。上面我已将可发布令牌指定为源值,而不是生成的令牌。

这是完整的工作代码。

public async Task<ProcessPaymentResult> ProcessPaymentAsync(ProcessPaymentRequest processPaymentRequest)
        {
            var processPaymentResult = new ProcessPaymentResult();

            try
            {    
                var customers = new CustomerService();
                var charges = new ChargeService();
               
                var options = new RequestOptions
                {
                    ApiKey = // your Secret Key
                };

                #region Card payment checkout                              
                var creditCardType = processPaymentRequest.CreditCardType.ToString();              
                var optionToken = new TokenCreateOptions
                {
                    Card = new TokenCardOptions
                    {
                        Number = processPaymentRequest.CreditCardNumber,
                        ExpMonth = processPaymentRequest.CreditCardExpireMonth,
                        ExpYear = processPaymentRequest.CreditCardExpireYear,
                        Cvc = processPaymentRequest.CreditCardCvv2,
                        Name = processPaymentRequest.CreditCardName,
                        Currency = _workContext.WorkingCurrency.CurrencyCode                        
                    },
                };
                var tokenService = new TokenService();
                Token paymentToken = await tokenService.CreateAsync(optionToken, options);

                #endregion

                #region Stripe Customer

                var customer = new Customer();
                var customerEmail = processPaymentRequest.CustomValues["CustomerEmail"].ToString();
                // Search customer in Stripe
                var stripeCustomer = await customers.ListAsync(new CustomerListOptions
                {
                    Email = customerEmail,
                    Limit = 1
                }, options);

                if (stripeCustomer.Data.Count==0)
                {
                    // create new customer
                    customer = await customers.CreateAsync(new CustomerCreateOptions
                    {
                        Source = paymentToken.Id,
                        Phone = processPaymentRequest.CustomValues["Phone"].ToString(),
                        Name = processPaymentRequest.CustomValues["CustomerName"].ToString(),
                        Email = customerEmail,
                        Description = $" { processPaymentRequest.CustomValues["RegisteredEmail"]};{ processPaymentRequest.CustomValues["CustomerName"] }",
                    }, options);

                }
                else
                {
                    // use existing customer
                    customer = stripeCustomer.FirstOrDefault();
                }

                #endregion

                #region Stripe charges                             

                var charge = await charges.CreateAsync(new ChargeCreateOptions
                {
                    Source = paymentToken.Id,//Customer = customer.Id,
                    Amount = (int)(Math.Round(processPaymentRequest.OrderTotal, 2) * 100),
                    Currency = _workContext.WorkingCurrency.CurrencyCode,
                    ReceiptEmail = customer.Email,
                    Description = $" { processPaymentRequest.CustomValues["RegisteredEmail"]};{ processPaymentRequest.CustomValues["CustomerName"] }",

                }, options);


                if (charge.Status.ToLower().Equals("succeeded"))
                {
                    processPaymentResult.NewPaymentStatus = PaymentStatus.Paid;
                    processPaymentResult.CaptureTransactionId = charge.Id;
                }
                else
                {
                    processPaymentResult.AddError("Error processing payment." + charge.FailureMessage);
                }
                #endregion

            }
            catch (Exception ex)
            {
                processPaymentResult.AddError(ex.Message);
            }
            return processPaymentResult;
        }