MasterCard proporciona bibliotecas de clientes para integrarse con sus servicios, pero los paquetes .NET no admiten .NET Framework 4.5, cuyo soporte terminó en 2016:
Este tutorial explica cómo consumir el servicio al cliente de MasterCard MDES de una aplicación .NET 4.5 generando una biblioteca de clientes API con el generador OpenAPI y firmando solicitudes HTTP utilizando una versión .NET 4.5 del firmante OAuth1 Lib.
Se puede generar una biblioteca de clientes .NET 4.5 para el servicio al cliente de MDES utilizando el siguiente comando:
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 "
}Ver también: OpenApi Generator (ejecutable)
Condition="Exists('....packages')" en elementos HintPath , ejemplo: < Reference Include = " RestSharp " >
< HintPath >....packagesRestSharp.105.1.0libnet45RestSharp.dll</ HintPath >
</ Reference >YourApp.Console :YourApp.MdesCustomerClientMastercard.Developer.OAuth1Signer.Core.dll (versión .net 4.5)Mastercard.Developer.OAuth1Signer.RestSharp.dll (versión .net 4.5)RestSharp.dllApiClient para usar JSONPor defecto, el servicio al cliente de MDES está aceptando y devuelve las cargas de XML cuando nuestro código generado espera que se use JSON.
Para indicar el servicio que hablamos JSON, cree una nueva clase en el proyecto YourApp.MdesCustomerClient Definiendo el método parcial InterceptRequest de la siguiente manera:
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 y keyStorageFlags .