C# 为条带付款创建客户时出错

C# 为条带付款创建客户时出错,c#,asp.net,asp.net-core,e-commerce,stripe-payments,C#,Asp.net,Asp.net Core,E Commerce,Stripe Payments,我有一个ASP.NET核心MVC电子商务应用程序,我正在尝试使用Stripe实现支付。我已经实现了中所述的解决方案,但由于某些原因,在用户发起付款时调用的charge方法中出现错误Stripe.StripeException:“无法向没有活动卡的客户收费” 这个问题似乎是由以下事实引起的:stripe-email和stripe-token(实际上被传递到charge方法中的两个参数)接收空参数。我不知道为什么调用函数时这些变量会得到空值,因为我已经完成了stripe教程中概述的所有步骤 我使用了

我有一个ASP.NET核心MVC电子商务应用程序,我正在尝试使用Stripe实现支付。我已经实现了中所述的解决方案,但由于某些原因,在用户发起付款时调用的charge方法中出现错误
Stripe.StripeException:“无法向没有活动卡的客户收费”

这个问题似乎是由以下事实引起的:stripe-email和stripe-token(实际上被传递到charge方法中的两个参数)接收空参数。我不知道为什么调用函数时这些变量会得到空值,因为我已经完成了stripe教程中概述的所有步骤

我使用了stripe仪表板中的test和live密钥以及可发布密钥,并将它们正确地包含在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();
        }
"Stripe": {
    "SecretKey": "sk_test...",
    "PublishableKey": "pk_test..."
  }
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();
        }
"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>
Startup.cs

public class StripeSettings
    {
        public string SecretKey { get; set; }
        public string PublishableKey { get; set; }
    }
       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"]);
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“Cn”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
//添加应用程序服务。
services.AddTransient();
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“Cn”));
services.AddSingleton();
services.AddScoped(sp=>ShoppingCart.GetCart(sp));
services.addScope();
services.AddTransient();
services.AddMvc();
services.AddMemoryCache();
services.AddSession();
services.Configure(Configuration.GetSection(“Stripe”);//Stripe配置
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseSession();
app.UseAuthentication();
SetApiKey(Configuration.GetSection(“Stripe”)[“SecretKey”]);
}

这里的问题可能只是
stripeToken
为空。您需要在代码中对此进行调查,以确认它是否正确设置为tok_XXXX。您的代码看起来很好。确保在脚本标记中插入了正确的条带可发布密钥。尝试使用类似Fiddler的方法检查请求,并确保Stripe正在发送具有正确参数名的良好令牌。调试并逐步完成操作方法。确保正确填写参数,并确保客户创建步骤成功。(正如您现在看到的,如果客户创建失败,您仍然会尝试向实际上未创建的客户收费。)@koopajah是的,条带令牌和条带电子邮件在传递到
charge()
方法时是空的。它们应该设置在哪里?它们来自客户端,当Checkout填充信息并将其发送到服务器时。因此,它们需要在您的路由中设置,或者在该路由中设置接收POST请求的代码case@koopajah实际上,我请求的url是错误的。现在正在请求条带付款的实际视图,正确的条带数据正在发送到控制器。