Visa card eligibility service using pay token getting unauthorized issue

Solved! Go to solution
shivang
Helper

Visa card eligibility service using pay token getting unauthorized issue

public static string visaUrl = "https://sandbox.api.visa.com/";
public static string apiKey = "apiKey ";
public static string sharedSecret = "sharedSecret ";
[FunctionName("XPayTokenGenerator")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

string name = req.Query["name"];

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
//var resourcepath = "https://sandbox.api.visa.com/visacardeligibilityservices/v1/cardeligibility/validate";
//var apikey = "";
//var requestBody = "";
// token = getXPayToken(resourcepath, apikey, requestBody);
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}";

string queryString = "apiKey=" + apiKey;
String status = DoXPayTokenCall("vdp/", "helloworld", queryString, "POST", null, null);

return new OkObjectResult(responseMessage);
}

private static string GetCorrelationId()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
return new string(Enumerable.Repeat(chars, 12).Select(s => s[random.Next(s.Length)]).ToArray()) + "_SC";

}


public static string DoXPayTokenCall(string baseUri, string resourcePath, string queryString, string method, string testInfo, string requestBodyString)
{
string requestURL = visaUrl + baseUri + resourcePath + "?" + "apikey=" + apiKey;
string apikey = apiKey;
//LogRequest(requestURL, requestBodyString);
// Create the POST request object
string statusCode = "";
HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
request.Method = method;
if (method.Equals("POST") || method.Equals("PUT"))
{
request.ContentType = "application/json";
request.Accept = "application/json";
// Load the body for the post request
var requestStringBytes = Encoding.UTF8.GetBytes(requestBodyString);
request.GetRequestStream().Write(requestStringBytes, 0, requestStringBytes.Length);
}

string xPayToken = getXPayToken(resourcePath, "apikey=" + apikey, requestBodyString);
request.Headers["x-pay-token"] = xPayToken;
request.Headers["ex-correlation-id"] = GetCorrelationId();

try
{
// Make the call
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//LogResponse(testInfo, response);
statusCode = response.StatusCode.ToString();
}
}
catch (WebException e)
{
if (e.Response is HttpWebResponse)
{
HttpWebResponse response = (HttpWebResponse)e.Response;
//LogResponse(testInfo, response);
statusCode = response.StatusCode.ToString();
}
}
return statusCode;
}

private static string getTimestamp()
{
long timeStamp = ((long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds) / 1000;
return timeStamp.ToString();
}

private static string getHash(string data)
{
var SHARED_SECRET = "s4z1q3VYqWAS7-{3mpCQ1rwDCD/7w5UYoI/JV/g-";
var hashString = new HMACSHA256(Encoding.ASCII.GetBytes(SHARED_SECRET));
var hashbytes = hashString.ComputeHash(Encoding.ASCII.GetBytes(data));
string digest = String.Empty;

foreach (byte b in hashbytes)
{
digest += b.ToString("x2");
}

return digest;
}

private static string getXPayToken(string resourcePath, string queryString, string requestBody)
{
string timestamp = getTimestamp();
string sourceString = timestamp + resourcePath + queryString + requestBody;
string hash = getHash(sourceString);
string token = "xv2:" + timestamp + ":" + hash;
return token;
}

 

I tried to use the X-Pay token for visa card eligibility service I am getting unauthorized issue,


"responseStatus": {
""status"": 401
""code"": ""9201""
""severity"": ""ERROR""
""message"": ""Token Validation Failed""
""info"": """"
}"

3 REPLIES 3
SyedSa
Community Moderator

Re: Visa card eligibility service using pay token getting unauthorized issue

Hi @shivang

Thank you for your question. One of our agents will get back to you as soon as possible. In the meantime, if any community members know a solution, please reply to this thread.

API_Managers
Visa Developer Support Specialist

Re: Visa card eligibility service using pay token getting unauthorized issue

Hi @shivang,

 

Please provide the information below for investigation of the error.

1. End Point
2. Request Header
3. Request Body
4. Response Header (include the x-correlation-id)
5. Response Body

You can find the x-correlation-id in the response header.




Thanks,

Tee



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

shivang
Helper

Re: Visa card eligibility service using pay token getting unauthorized issue

Working fine now, the issue is occurring because the request JSON is not the same one that I am passing for token generation.

Now is working fine.