【问题标题】:How do I properly implement PayPal payment in asp.net core 3.0如何在 asp.net core 3.0 中正确实施 PayPal 支付
【发布时间】:2019-06-12 12:59:30
【问题描述】:

因此,我正在尝试使用我的沙盒帐户实施 PayPal 付款方式进行测试,并且我能找到的所有文档都适用于仍然使用 AppConfig 和/或 Webconfig 的 ASP.NET Core 版本。 我只有 appsettings.json,所以我不知道如何在这里实现这部分

<configuration>
  <configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
  </configSections>

  <!-- PayPal SDK settings -->
  <paypal>
    <settings>
      <add name="mode" value="sandbox" />
      <add name="clientId" value="__CLIENT_ID__" />
      <add name="clientSecret" value="__CLIENT_SECRET__" />
    </settings>
  </paypal>
</configuration>

显示在他们的GitHub page

我尝试将其添加到我的 appsettings.json

  "PayPal": {
    "mode": "sandbox",
    "clientId": "xxxx",
    "clientSecret": "xxxx"
  }

然后这个

// Get a reference to the config
            var config = ConfigManager.Instance.GetProperties();

            // Use OAuthTokenCredential to request an access token from PayPal
            var accessToken = new OAuthTokenCredential(config).GetAccessToken();

            var apiContext = new APIContext(accessToken);

            var payment = Payment.Get(apiContext, "PAY-0XL713371A312273YKE2GCNI");

这引发了我这个异常

FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

抛出它的那一行是var config = ConfigManager.Instance.GetProperties();

【问题讨论】:

    标签: asp.net razor asp.net-core paypal


    【解决方案1】:
    仅适用于 Asp.net 核心

    快速总结:将 PayPal SDK 提供的config 替换为自定义的


    将 PayPal 部分添加到 appsettings.json 文件

    例如

    "paypal": {
      "settings": {
        "business": "your_business_email@provider.ext",
        "mode": "sandbox",
        "merchantId": "MERCHANT_ID",
        "clientId": "CLIENT_ID",
        "clientSecret": "CLIENT_SECRET"
      }
    },
    

    在你希望调用paypal的GetAccessToken方法的类中, 确保您

    1. 声明对Microsoft.Extensions.Configuration的依赖 即

      using Microsoft.Extensions.Configuration;
      

    1. 在类构造函数中,添加一个参数IConfiguration config,它将由asp.net的DI提供,即
    class PayPalHandler{
    
     public PayPalHandler(IConfiguration config){
    
     }
    }
    
    

    var accessToken = new OAuthTokenCredential(config).GetAccessToken();,我们将替换 config 由 Paypal 提供 ConfigManager.Instance.GetProperties();我们自己的

    1. 定义我们自己的配置以传递给 PayPal
    using PayPal.Api;
    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;
    class PayPalHandler{
      
      //@ Declare a R/O property to store our own custom configuration for PayPal
      private readonly Dictionary<string,string> _payPalConfig;
    
      //@ The class constructor
      public PayPalHandler(IConfiguration config){
    
        //@ Fetch the `appsettings.json` and pack it into the custom configuration
        //@ Only the *clientId*,*clientSecret* and *mode* are required to get an access token (as of the time of writing this)
        _payPalConfig = new Dictionary<string, string>()
        {
            { "clientId" , config.GetSection("paypal:settings:clientId").Value },
            { "clientSecret", config.GetSection("paypal:settings:clientSecret").Value },
            { "mode", config.GetSection("paypal:settings:mode").Value },
            { "business", config.GetSection("paypal:settings:business").Value },
            { "merchantId", config.GetSection("paypal:settings:merchantId").Value },
        };
    
      }
    }
    

    1. 使用 OAuthTokenCredential 通过我们的自定义配置从 PayPal 请求访问令牌
    var accessToken = new OAuthTokenCredential(_payPalConfig).GetAccessToken();
    

    1. 完整的实现应该类似于
    using PayPal.Api;
    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;
    class PayPalHandler{
      
      //@ Declare a R/O property to store our own custom configuration for PayPal
      private readonly Dictionary<string,string> _payPalConfig;
    
      //@ The class constructor
      public PayPalHandler(IConfiguration config){
    
        //@ Fetch the `appsettings.json` and pack it into the custom configuration
        //@ Only the *clientId*,*clientSecret* and *mode* are required to get an access token (as of the time of writing this)
        _payPalConfig = new Dictionary<string, string>()
        {
            { "clientId" , config.GetSection("paypal:settings:clientId").Value },
            { "clientSecret", config.GetSection("paypal:settings:clientSecret").Value },
            { "mode", config.GetSection("paypal:settings:mode").Value },
            { "business", config.GetSection("paypal:settings:business").Value },
            { "merchantId", config.GetSection("paypal:settings:merchantId").Value },
        };
    
        //@ Use OAuthTokenCredential to request an access token from PayPal using our custom configuration
        var accessToken = new OAuthTokenCredential(_payPalConfig).GetAccessToken();
    
        //@ Proceed as desired 
    
      }
    }
    

    【讨论】:

      【解决方案2】:

      为了访问配置属性添加到ConfigureServices:

      services.Configure&lt;PayPal&gt;(Configuration.GetSection("PayPal"));

      您可以在其中使用 appsettings 文件中的属性创建一个名为 PayPal 的新类。

      然后你可以通过

      注入到控制器中

      IOptions&lt;PayPal&gt; paypalOptions

      【讨论】:

        猜你喜欢
        • 2021-05-07
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        • 2014-07-29
        • 2020-05-29
        • 2012-05-27
        • 2013-08-08
        • 1970-01-01
        相关资源
        最近更新 更多