.net 核心中的条纹支付结账

Stripe payment checkout in .net core

我正在将 Stripe Gateway 集成到我的电子商务站点 in.net 核心。

这是一页结帐页面。 我正在结帐向导的第 3 步收集客户卡详细信息。并且需要在最后一步处理 Stripe 支付。

有没有一种方法可以在 C# 中处理付款并获取 tokenID,而不是像下面那样,

<form action="/Home/Charge" method="POST">
    <article>
        <label>Amount: .00</label>
    </article>
    <script src="//checkout.stripe.com/v2/checkout.js"
            class="stripe-button"
            data-key="@ViewBag.StripePublishKey"
            data-locale="auto"
            data-description="Sample Charge"
            data-amount="500">
    </script>
</form>

这是我的付款方式。我需要处理付款并获取 TokenID 并创建客户等。

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

            try
            {
                var customers = new CustomerService();
                var charges = new ChargeService();
                var apiKey = "sk_test_51KseuBH17D7TNzPp0051boOPvQ";
                var options = new RequestOptions
                {
                    ApiKey = apiKey
                };


                #region Payment process                              
                var creditCardType = processPaymentRequest.CustomValues["CreditCardType"].ToString();
                var cardholderName = processPaymentRequest.CustomValues["CardholderName"].ToString();
                var cardNumber = processPaymentRequest.CustomValues["CardNumber"].ToString();
                var expireMonth = processPaymentRequest.CustomValues["ExpireMonth"].ToString();
                var eExpireYear = processPaymentRequest.CustomValues["ExpireYear"].ToString();
                var cardCode = processPaymentRequest.CustomValues["CardCode"].ToString();



              >>> var paymentToken = ????????? ProcessPaymentAsync Stripe Payment and get token

                #endregion



                var customerEmail = processPaymentRequest.CustomValues["CustomerEmail"].ToString();
                var name = processPaymentRequest.CustomValues["CustomerName"].ToString();
                var phone = processPaymentRequest.CustomValues["Phone"].ToString();
                var description = $" { processPaymentRequest.CustomValues["RegisteredEmail"]};{ processPaymentRequest.CustomValues["CustomerName"] }";


                var customer = await customers.CreateAsync(new CustomerCreateOptions
                {
                    Email = customerEmail,
                    Description = description,
                    Name = name,
                    Phone = phone,
                    Source = paymentToken
                }, options);


                var charge = await charges.CreateAsync(new ChargeCreateOptions
                {
                    Amount = (int)(Math.Round(processPaymentRequest.OrderTotal, 2) * 100),
                    Currency = _workContext.WorkingCurrency.CurrencyCode,
                    Customer = customer.Id,
                    ReceiptEmail = customer.Email,
                    Description = description
                }, options);


                if (charge.Status.ToLower().Equals("succeeded"))
                {
                    processPaymentResult.NewPaymentStatus = PaymentStatus.Paid;
                    processPaymentResult.CaptureTransactionId = charge.Id;
                }
                else
                {
                    processPaymentResult.AddError("Error processing payment." + charge.FailureMessage);
                }
            }
            catch (Exception ex)
            {
                processPaymentResult.AddError(ex.Message);
            }
            return processPaymentResult;
        }

我需要在我的方法中的“>>> var paymentToken = ??????????”行处理和检索 TokenID

终于,我做到了。为有相同需求的开发人员发布我的最终代码。

这是完整的工作代码。

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