1

I am getting an error while implementing visa cyber source using c# code and I have downloaded source code for that after creating a project but getting error may be x-pay-token was not generating correctly. Is there any way so I can verify my token or generate it with any API by passing parameter. Below code, I am using

string baseUri = "cybersource/";
string resourcePath = "v2/payments";

string xPayToken = GetXPayToken(resourcePath, "apikey=" + apikey, requestBodyString);static string GetXPayToken(string apiNameURI, string queryString, string requestBody)
{
    string timestamp = GetTimestamp();
    string sourceString = timestamp + apiNameURI + queryString + requestBody;
    string hash = GetHash(sourceString);
    string token = "xv2:" + timestamp + ":" + hash;
    return token;
} 

private static string GetHash(string data)
{
    string sharedSecret = ConfigurationManager.AppSettings["VisaPaySharedSecret"];
    var hashString = new HMACSHA256(Encoding.ASCII.GetBytes(sharedSecret));
    var hashbytes = hashString.ComputeHash(Encoding.ASCII.GetBytes(data));
    string digest = String.Empty;

    foreach (byte b in hashbytes)
    {

    }

    return digest;
}
Braiam
  • 1
  • 11
  • 47
  • 78

1 Answers1

0

This Iis the official Visa way to do it: https://developer.visa.com/pages/working-with-visa-apis/x-pay-token#sample_code_for_api_key__shared_secret_xpaytoken

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 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; 
}
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Thank you for considering but I have the same code for getting Timestamp and x-pay-token is also generated but while I am passing x-pay-token I am getting an unauthorized error. So I am not sure about generated x-pay-token is correct or not – Shivang Gupta Feb 07 '20 at 06:43
  • I am also confused about passing resourcePath for https://sandbox.api.visa.com/cybersource/v2/payments?apikey={apikey} api – Shivang Gupta Feb 07 '20 at 06:45
  • Then your code is fine. Check this: https://community.developer.visa.com/t5/Getting-Started-with-Visa/x-pay-token-validation-problem/td-p/6477 for other reasons. Bad key, wrong http verb Post is needed), body is not hashed with the exact same spaces, e.t.c. – Athanasios Kataras Feb 07 '20 at 06:52