我有一个ASP .NET Core MVC电子商务应用程序,我按照this guide中的说明实现了解决方案,但出于某种原因,我在收费方法中收到错误 Stripe.StripeException: 'Cannot charge a customer that has no active card'
,当用户发起付款时调用该方法 .
问题似乎是由条带电子邮件和条带令牌(它们是实际传递到计费方法的两个参数)接收空参数引起的 . 我不确定为什么这些变量在调用函数时会得到空值,因为我已经按照条带教程中列出的所有步骤进行操作 .
我已经使用条带仪表板中的测试和实时秘密以及可发布的密钥,并将它们正确地包含在appsettings json文件中 . 相关文件的代码如下 . What could be causing this error here?
stripe charge controller action
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..."
}
charge view page
@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>
Stripe settings class
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"]);
}