如何在asp.net core 3.0中正确实施PayPal付款

时间:2019-01-18 09:26:24

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

因此,我正在尝试使用我的沙箱帐户实施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();

2 个答案:

答案 0 :(得分: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;


  2. 在类构造函数中,添加一个参数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 

  }
}

答案 1 :(得分:0)

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

services.Configure<PayPal>(Configuration.GetSection("PayPal"));

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

然后您可以通过

注入控制器

IOptions<PayPal> paypalOptions