Mastercard provides client libraries for integrating with its services, but the .NET packages don't support .NET Framework 4.5, whose support ended in 2016:
This tutorial explains how to consume the Mastercard MDES Customer Service from a .NET 4.5 app by generating an API client library with OpenAPI generator and signing HTTP requests using a .NET 4.5 version of the OAuth1 Signer lib.
A .NET 4.5 client library for MDES Customer Service can be generated using the following command:
java -jar openapi-generator-cli.jar generate -i mdes-customer-service-2.0.4.yaml -g csharp -c config.json -o mdes-customer-service-net45-tutorialconfig.json:
{
"targetFramework": "v4.5",
"packageName": "YourApp.MdesCustomerClient"
}See also: OpenAPI Generator (executable)
Condition="Exists('....packages')" in HintPath elements, example:<Reference Include="RestSharp">
<HintPath>....packagesRestSharp.105.1.0libnet45RestSharp.dll</HintPath>
</Reference>YourApp.Console project:
YourApp.MdesCustomerClientMastercard.Developer.OAuth1Signer.Core.dll (.NET 4.5 version)Mastercard.Developer.OAuth1Signer.RestSharp.dll (.NET 4.5 version)RestSharp.dllApiClient to Use JSON By default, the MDES Customer Service is accepting and returning XML payloads when our generated code expects JSON to be used.
To indicate the service we speak JSON, create a new class in the YourApp.MdesCustomerClient project defining the partial method InterceptRequest the following way:
using RestSharp;
namespace YourApp.MdesCustomerClient.Client
{
/// <summary>
/// Extends the generated ApiClient class.
/// </summary>
public partial class ApiClient
{
/// <summary>
/// Adds "Format=JSON" to the RestSharp request so that the service accepts and returns JSON instead of XML.
/// </summary>
partial void InterceptRequest(IRestRequest request) => request.AddQueryParameter("Format", "JSON");
}
}class Program
{
static void Main(string[] args)
{
// The request was aborted: Could not create SSL/TLS secure channel
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var consumerKey = "<insert consumer key>";
var signingKey = SecurityUtils.LoadPrivateKey("<insert PKCS#12 key file path>", "<insert key alias>", "<insert key password>"); // Pass X509KeyStoragFlags here
var config = Configuration.Default;
config.BasePath = "https://sandbox.api.mastercard.com/mdes/csapi/v2";
config.ApiClient.RestClient.Authenticator = new RestSharpOAuth1Authenticator(consumerKey, signingKey, new Uri(config.BasePath));
var searchApi = new SearchApi(config);
var auditInfo = new AuditInfo("A1435477", "John Smith", "Any Bank", "5555551234");
var tokenUniqueReference = "DWSPMC00000000010906a349d9ca4eb1a4d53e3c90a11d9c";
var searchRequest = new SearchRequest(null, tokenUniqueReference, null, null, null, null, null, null, auditInfo);
var response = searchApi.SearchPost(new SearchRequestSchema(searchRequest));
System.Console.WriteLine(response.SearchResponse.Accounts.Account[0].Tokens.Token[0]);
System.Console.ReadLine();
}
}consumerKey, pkcs12KeyFilePath, signingKeyAlias, signingKeyAlias and keyStorageFlags.