我正在將 Stripe Gateway 集成到我的電子商務網站 in.net core。
這是一個單頁結帳頁面。我正在結帳向導的第三步收集客戶卡的詳細資訊。并且需要在最后一步處理 Stripe 支付。
有沒有辦法在 C# 中處理付款并獲取 tokenID,而不是像下面這樣,
<form action="/Home/Charge" method="POST">
<article>
<label>Amount: $5.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;
}
我需要處理和 retvie TokenID on >>>> var paymentToken = ????????? “我的方法中的行
uj5u.com熱心網友回復:
最后,我做到了。為具有相同要求的開發人員發布我的最終代碼。
這是完整的作業代碼。
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/466833.html
上一篇:在WebSocket聊天中廣播
