Error when try to execute a transaction details

Solved! Go to solution
mpiedray
Newbie

Error when try to execute a transaction details

Hi everyone I try to implement a transaction details  using the API, for that I create my credential and my payment in the test server, this is my code

 

string SecretKey = "my_SecretKey ";
string KeyId = "my_KeyId ";
string MerchantId = "my_MerchantId ";
string current_date = DateTime.Now.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'");

var SignatureParm = "host: apitest.cybersource.com\n date: " + current_date + "\n(request-target): get /tss/v2/transactions/6135049847566524904005\nv -c-merchant-id: " + MerchantId;
var SignatureHash = GenerateSignatureFromParams(SignatureParm, SecretKey);


var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apitest.cybersource.com/tss/v2/transactions/6135049847566524904005");
httpWebRequest.Method = "GET";

httpWebRequest.Headers.Add("v-c-merchant-id", MerchantId);
httpWebRequest.Headers.Add("Date", DateTime.Now.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'"));
httpWebRequest.Headers.Add("Host", "apitest.cybersource.com");
httpWebRequest.Headers.Add("Signature", "keyid=\"" + KeyId + "\", algorithm=\"HmacSHA256\", headers=\"host date (request-target) v-c-merchant-id\", signature=\"" + SignatureHash + "\"");
httpWebRequest.ContentType = "application/json";


var httpResponse = new HttpWebResponse();
string res = "";

try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
res = streamReader.ReadToEnd();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.WriteLine(httpResponse.ToString());
}

Console.Write(res);

Console.ReadKey();

 

this is my function to generate the Signature Params

 

public static string GenerateSignatureFromParams(string signatureParams, string secretKey)
{
var sigBytes = Encoding.UTF8.GetBytes(signatureParams);
var decodedSecret = Convert.FromBase64String(secretKey);
var hmacSha256 = new HMACSHA256(decodedSecret);
var messageHash = hmacSha256.ComputeHash(sigBytes);
return Convert.ToBase64String(messageHash);
}

 

When I execute this code I obtain  this error

 

System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()

 

Any idea what I am  doing wrong?

 

Thanks in advances!

1 REPLY 1
DianaTran-Yee
Visa Developer Support Specialist

Re: Error when try to execute a transaction details

Hey @mpiedray,

 

At this time, the only way to correctly query the CyberSource Payments sandbox is creating a merchant account directly with CyberSource using this link here: https://developer.cybersource.com 

 

The CyberSource site provides an easy to use method to test their account credentials: https://developer.cybersource.com/api/reference/api-reference.html 
 
Here's the sample code:  https://github.com/CyberSource/cybersource-rest-samples-java/blob/master/src/main/java/samples/payme... 

 




Thanks,

Diana



Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.