Skip to content

jaaufauvre/mastercard-mdes-customer-service-net45-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

.NET 4.5: How to Integrate with the Mastercard MDES Customer Service?

Table of Contents

Overview

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.

Prerequisites

  1. Read the MDES Customer Service documentation
  2. Download the MDES Customer Service OpenAPI specification
  3. Set up a project in the Mastercard Developers Portal, add the MDES Customer Service API to it:

Project

  1. Checkout and build the .NET 4.5 version of the OAuth1 Signer library

Step 1: Generate a .NET 4.5 API Client Library

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-tutorial

config.json:

{ 
    "targetFramework": "v4.5",
    "packageName": "YourApp.MdesCustomerClient" 
}

See also: OpenAPI Generator (executable)

Step 2: Open and Build the Generated Solution

  1. Navigate to the mdes-customer-service-net45-tutorial/src folder
  2. Fix the project files (.csproj) by deleting Condition="Exists('..\..\packages')" in HintPath elements, example:
<Reference Include="RestSharp">
    <HintPath>..\..\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>
</Reference>
  1. Open YourApp.MdesCustomerClient.sln
  2. Click Build > Rebuild Solution

Step 2

Step 3: Add a Console App Project to the Solution

  1. Click File > New > Project...
  2. Choose Console App (.NET Framework) and configure the project the following way:

Step 3a

  1. Right-click on References and add the following dependencies to the YourApp.Console project:
    • YourApp.MdesCustomerClient
    • Mastercard.Developer.OAuth1Signer.Core.dll (.NET 4.5 version)
    • Mastercard.Developer.OAuth1Signer.RestSharp.dll (.NET 4.5 version)
    • RestSharp.dll

Step 3b

Step 4: Extend ApiClient 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");
    }
}

Step 4

Step 5: Send Requests to the MDES Search API

  1. Update Program.cs with the following code:
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();
    }
}
  1. Use your own consumerKey, pkcs12KeyFilePath, signingKeyAlias, signingKeyAlias and keyStorageFlags.
  2. Click Debug
  3. You should get a result similar to this one:

Step 5

About

.NET 4.5: How to Integrate with the Mastercard MDES Customer Service?

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages