我有一个ASP .NET Core MVC电子商务应用程序,我试图在其中使用Stripe进行付款。我已经实现了this guide中概述的解决方案,但是由于某种原因,我在计费方法中遇到错误Stripe.StripeException: 'Cannot charge a customer that has no active card'
,该方法在用户启动付款时被调用。
该问题似乎是由于以下事实引起的:作为实际传递给charge方法的两个参数的条纹电子邮件和条纹令牌正在接收空参数。我不确定为什么在调用函数时这些变量会得到空值,因为我遵循了条纹教程中概述的所有步骤。
我已经使用了条纹仪表板中的测试密钥和实时秘密密钥以及可发布的密钥,并将它们正确地包含在appsettings json文件中。相关文件中的代码如下。 什么可能导致此错误?
条带化充电控制器操作
public IActionResult Charge(string stripeEmail, string stripeToken)
{
var customerService = new StripeCustomerService();
var chargeService = new StripeChargeService();
var customer = customerService.Create(new StripeCustomerCreateOptions
{
Email = stripeEmail,
SourceToken = stripeToken
});
var charge = chargeService.Create(new StripeChargeCreateOptions
{
Amount = 500,
Description = "ASP .NET Core Stripe Tutorial",
Currency = "usd",
CustomerId = customer.Id
});
return View();
}
appsettings.json
"Stripe": {
"SecretKey": "sk_test...",
"PublishableKey": "pk_test..."
}
收费视图页面
@using Microsoft.Extensions.Options
@inject IOptions<StripeSettings> Stripe
@{
ViewData["Title"] = "MakePayment";
}
<form action="/Order/Charge" method="post">
<article>
<label>Amount: $5.00</label>
</article>
<script src="//checkout.stripe.com/v2/checkout.js"
class="stripe-button"
data-key="@Stripe.Value.PublishableKey"
data-locale="auto"
data-description="Sample Charge"
data-amount="500">
</script>
</form>
<h2>MakePayment</h2>
条纹设置类
public class StripeSettings
{
public string SecretKey { get; set; }
public string PublishableKey { get; set; }
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Cn")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddDbContext<wywytrav_DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Cn")));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped(sp => ShoppingCart.GetCart(sp));
services.AddScoped<SignInManager<ApplicationUser>, SignInManager<ApplicationUser>>();
services.AddTransient<IOrderRepository, OrderRepository>();
services.AddMvc();
services.AddMemoryCache();
services.AddSession();
services.Configure<StripeSettings>(Configuration.GetSection("Stripe"));//stripe configuration
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession();
app.UseAuthentication();
StripeConfiguration.SetApiKey(Configuration.GetSection("Stripe")["SecretKey"]);
}