Reputation: 1057
So I am trying to implement a PayPal payment method for testing using my sandbox account and all the documents I can find is for ASP.NET Core versions that still uses AppConfig and or Webconfig. I only have appsettings.json so I am not sure how to implement this section right here
<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>
which is shown on their GitHub page
I tried adding this to my appsettings.json
"PayPal": {
"mode": "sandbox",
"clientId": "xxxx",
"clientSecret": "xxxx"
}
And then this
// 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");
Which threw me this exception
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.
and the line that throws it is this one var config = ConfigManager.Instance.GetProperties();
Upvotes: 2
Views: 1567
Reputation: 146
For Asp.net core only
Quick summary: Replace the
config
provided by the PayPal SDK with a custom one
Add a PayPal section to the appsettings.json
file
e.g
"paypal": {
"settings": {
"business": "[email protected]",
"mode": "sandbox",
"merchantId": "MERCHANT_ID",
"clientId": "CLIENT_ID",
"clientSecret": "CLIENT_SECRET"
}
},
In the class that you wish to invoke the GetAccessToken
method of paypal,
Ensure that you
Declare a dependency to Microsoft.Extensions.Configuration
i.e
using Microsoft.Extensions.Configuration;
IConfiguration config
which will be provided by asp.net's DI i.eclass PayPalHandler{
public PayPalHandler(IConfiguration config){
}
}
in
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
, we will replace theconfig
provided by Paypal'sConfigManager.Instance.GetProperties();
with our own
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
}
}
Upvotes: 3
Reputation: 74
In order to access the configuration attributes add to ConfigureServices
:
services.Configure<PayPal>(Configuration.GetSection("PayPal"));
Where you create a new class called PayPal with the attributes from the appsettings file.
Then you can inject into the controller via
IOptions<PayPal> paypalOptions
Upvotes: 0