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:
- Make sure your organization is a Ping Payment registered customer.
- 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:
-
Microsoft .NET Standard 2.0 or later. This is typically included by default when installing a recent version of Visual Studio. For more information, see Install .NET on Windows.
Create a project 🔨
-
Open Visual Studio and create a new project using the C# Console Application template. Name the project TestPingPaymentsSDK.
-
After the project loads, right-click on the project and choose Manage NuGet packages...
-
Search the following NuGet packages and install them into the project:
PingPayments.Payments
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration
- 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 👩💻
- In the TestPingPaymentsSDK project, find and open Program.cs.
- 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 🚀
- Build and run the application.
- 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 🔨
- Open a new terminal window. Find or create an operating system folder under which you can create a .NET project.
- In that folder, run the following command to create the .NET project:
dotnet new console --name TestPingPaymentsSDK
- Go to the newly created TestPingPaymentsSDK folder and add the following appsettings.json file:
{
"PingPayments": {
"ApiUrl": "https://sandbox.pingpayments.com/payments/",
"TenantId": "{YOUR-TENNAT-ID}"
}
}
- Update the JSON with your tenant ID.
- 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 🧑💻
- In the TestPingPaymentsSDK folder, find and open Program.cs in your code editor.
- 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}");
}
}
}
}
- 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 🚀
- Run the following command:
dotnet run
- 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
Updated 3 months ago