在.net MVC应用程序中使用Stripe Checkout

时间:2019-03-18 19:47:29

标签: c# .net stripe-payments

我正尝试使用Stripe Checkout来构建我的第一个MVC应用程序,使用直接从Stripe此处找到的步骤-https://stripe.com/docs/checkout/aspnet

按照这些步骤尝试进行编译时,出现以下错误...

Startup.cs

错误CS0103当前上下文中不存在名称'StripeConfiguration'

HomeController.cs

错误CS0246找不到类型或名称空间名称'ChargeCreateOptions'(您是否缺少using指令或程序集引用?)

错误CS0246找不到类型或名称空间名称'CustomerCreateOptions'(您是否缺少using指令或程序集引用?)

错误CS0246找不到类型或名称空间名称'ChargeService'(您是否缺少using指令或程序集引用?)

错误CS0246找不到类型或名称空间名称'CustomerService'(您是否缺少using指令或程序集引用?)

我已经使用工具> NuGet软件包管理器>管理NuGet解决方案软件包来确保我拥有最新的Stripe软件包。

HomeController.cs代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using StripeCheckoutNPP.Models;

namespace StripeCheckoutNPP.Controllers
{
    public class HomeController : Controller

    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
        public IActionResult Charge(string stripeEmail, string stripeToken)
        {
            var customers = new CustomerService();
            var charges = new ChargeService();

            var customer = customers.Create(new CustomerCreateOptions
            {
                Email = stripeEmail,
                SourceToken = stripeToken
            });

            var charge = charges.Create(new ChargeCreateOptions
            {
                Amount = 500,
                Description = "Sample Charge",
                Currency = "usd",
                CustomerId = customer.Id
            });

            return View();
        }
    }
}

Startup.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace StripeCheckoutNPP
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            StripeConfiguration.SetApiKey(Configuration.GetSection("Stripe")["SecretKey"]);
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

2 个答案:

答案 0 :(得分:0)

根据说明,您的配置方法应为

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        StripeConfiguration.SetApiKey(Configuration.GetSection("Stripe")["SecretKey"]); 
        // the rest of the code....
    }

当前,您在构造函数中拥有StripeConfiguration.SetApiKey()

答案 1 :(得分:0)

在Visual Studio 2017中添加了名为StripeCheckoutNPP的.NET Core-> ASP.NET Core Web应用程序。版本ASP.NET Core 2.2。选择Web应用程序(模型-视图-控制器)。取消选中“配置HTTPS”。重建项目解决方案。添加了Stripe.Net。 stripe.net https://dashboard.stripe.com/login?redirect=%2Faccount%2Fapikeys 将我的密钥和发布密钥添加到appsettings.json

即使在最后一个答案发布后,我的作品仍能正常工作-随心所欲。

似乎您没有正确地进行操作,以致条纹命名空间和类没有得到正确的引用。

我安装了免费的JustDecompile,看到对于类的所有错误 StripeConfiguration ChargeCreateOptions CustomerCreateOptions 收费服务 客户服务 在Stripe.net.dll中

enter image description here