Mark Denom
Mark Denom

Reputation: 1057

How do I properly implement PayPal payment in asp.net core 3.0

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

Answers (2)

Ian Mbae
Ian Mbae

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

  1. Declare a dependency to Microsoft.Extensions.Configuration i.e

    using Microsoft.Extensions.Configuration;
    

  1. In the class constructor, add a parameter IConfiguration config which will be provided by asp.net's DI i.e
class PayPalHandler{

 public PayPalHandler(IConfiguration config){

 }
}


in var accessToken = new OAuthTokenCredential(config).GetAccessToken();, we will replace the config provided by Paypal's ConfigManager.Instance.GetProperties(); with our own

  1. Defining our own configuration to pass to 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. Use OAuthTokenCredential to request an access token from PayPal using our custom configuration
var accessToken = new OAuthTokenCredential(_payPalConfig).GetAccessToken();

  1. The complete implementation should look something like
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

Erez Savir
Erez Savir

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

Related Questions