Quickstart

Learn how to quickly set up and test the Ping Payments C# SDK.

You need the following for the Quickstart

Before you begin with the Quickstart you need a Tenant ID, for resource permissions. This ID is given to you (or your organization) upon registration. Do the following:

  1. Make sure your organization is a Ping Payment registered customer.
  2. Get access to your tenant ID.

📘

Before you start!

Follow option 1 if you are using Visual Studio. Follow option 2 if you are using another IDE.


Payments API

Option 1, C# SDK Quickstart (Windows platform)

You perform this Quickstart exercise on a Windows computer using Visual Studio and .NET Core. Make sure you have the following:


Create a project 🔨

  1. Open Visual Studio and create a new project using the C# Console Application template. Name the project TestPingPaymentsSDK.

  2. After the project loads, right-click on the project and choose Manage NuGet packages...

  3. Search the following NuGet packages and install them into the project:

PingPayments.Payments
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration
  1. Add an appsettings.json file to the project.
  • Right-click the project, choose Add, choose New Item, and then choose Text File. Specify the file name as appsettings.json.
  • Add the following JSON:
{
  "PingPayments": {
    "ApiUrl": "https://sandbox.pingpayments.com/payments/",
    "TenantId": "{YOUR-TENNAT-ID}"
  }
}
  • Update the preceding JSON by providing your Tenant ID.
  • Update the file properties. Right-click appsettings.json, and then choose Properties. In the Copy to Output Directory drop-down list, choose Copy if newer.

Write code 👩‍💻

  1. In the TestPingPaymentsSDK project, find and open Program.cs.
  2. Replace the contents with the following code and save the file:
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using PingPayments.PaymentsApi;

namespace TestPingPaymentsSDK
{
    public class Program
    {
        private static IPingPaymentsApiClient api;
        private static IConfigurationRoot config;

        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile($"appsettings.json", true, true);
          	config = builder.Build();

            var tenantId = new Guid(config["PingPayments:TenantId"]);
            var apiUrl =  new Uri(config["PingPayments:ApiUrl"]);
          
            Console.WriteLine($"Your Tenant ID is: {tenantId} and you are using url: {apiUrl}");

            var httpClient = new HttpClient().ConfigurePingPaymentsClient(apiUrl, tenantId);
            api = new PingPaymentsApiClient(httpClient);

            PingTheApiAsync().Wait(); 
        }

        static async Task PingTheApiAsync()
        {
            var response = await api.Ping.V1.Ping();
          
            if (response.IsSuccessful)
            {
                Console.WriteLine($"Ping... {response.Body?.SuccesfulResponseBody?.Text}");
            }
            else 
            {
                Console.WriteLine($"Error: {response.Body?.ErrorResponseBody?.Errors}");
            }
        }
    }
}

Run the application 🚀

  1. Build and run the application.
  2. Verify the result. The result should look like this if the call is successfull:
Your Tenant ID is: {YOUR-TENANT-ID} and you are using url: https://sandbox.pingpayments.com/payments/
Ping... pong

Option 2, C# SDK Quickstart (cross platform)

Use the following steps to create a project if you are using a non-Windows computer and do not use Visual Studio. These steps use the .NET command-line interface. Make sure you have .NET installed on your computer. For information regarding .NET installation see: Download .NET.


Create a project 🔨

  1. Open a new terminal window. Find or create an operating system folder under which you can create a .NET project.
  2. In that folder, run the following command to create the .NET project:
dotnet new console --name TestPingPaymentsSDK
  1. Go to the newly created TestPingPaymentsSDK folder and add the following appsettings.json file:
{
  "PingPayments": {
    "ApiUrl": "https://sandbox.pingpayments.com/payments/",
    "TenantId": "{YOUR-TENANT-ID}"
  }
}
  1. Update the JSON with your Tenant ID.
  2. Run the following commands to install the NuGet packages from the NuGet package manager:
dotnet add package PingPayments.Payments
dotnet add package System.Net.Http  
dotnet add package Microsoft.Extensions.Configuration.Json
dotnet add package Microsoft.Extensions.Configuration

Write code 🧑‍💻

  1. In the TestPingPaymentsSDK folder, find and open Program.cs in your code editor.
  2. Replace the contents with the following code and save the file:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using PingPayments.PaymentsApi;

namespace TestPingPaymentsSDK
{
    public class Program
    {
        private static IPingPaymentsApiClient api;
        private static IConfigurationRoot config;

        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile($"appsettings.json", true, true);
            config = builder.Build();

            var tenantId = new Guid(config["PingPayments:TenantId"]);
            var apiUrl =  new Uri(config["PingPayments:ApiUrl"]);
          
            Console.WriteLine($"Your Tenant ID is: {tenantId} and you are using url: {apiUrl}");

            var httpClient = new HttpClient().ConfigurePingPaymentsClient(apiUrl, tenantId);
            api = new PingPaymentsApiClient(httpClient);

            PingTheApiAsync().Wait(); 
        }

        static async Task PingTheApiAsync()
        {
            var response = await api.Ping.V1.Ping();
          
            if (response.IsSuccessful)
            {
                Console.WriteLine($"Ping... {response.Body?.SuccesfulResponseBody?.Text}");
            }
            else 
            {
                Console.WriteLine($"Error: {response.Body?.ErrorResponseBody?.Errors}");
            }
        }
    }
}
  1. Update the preceding code to provide the full path of the appsettings.json file.
var builder = new ConfigurationBuilder()
  .AddJsonFile($"<file-path>/appsettings.json", true, true);

Run the application 🚀

  1. Run the following command:
dotnet run
  1. Verify the result. The result should look like this if the call is successfull:
Your Tenant ID is: {YOUR-TENANT-ID} and you are using url: https://sandbox.pingpayments.com/payments/
Ping... pong