因此,我正在尝试使用我的沙箱帐户实施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();
答案 0 :(得分:1)
快速摘要:将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方法的类中, 确保您
声明对Microsoft.Extensions.Configuration
的依赖性,即using Microsoft.Extensions.Configuration
;
在类构造函数中,添加一个参数IConfiguration config
,该参数将由asp.net的DI提供,即
class PayPalHandler{
public PayPalHandler(IConfiguration config){
}
}
在
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
中,我们将替换config
由Paypal的ConfigManager.Instance.GetProperties();
与我们自己的
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 },
};
}
}
var accessToken = new OAuthTokenCredential(_payPalConfig).GetAccessToken();
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