1

我在使用 c# 代码实现签证网络源时遇到错误,我在创建项目后下载了源代码,但出现错误可能是 x-pay-token 未正确生成。有什么方法可以验证我的令牌或通过传递参数使用任何 API 生成它。下面的代码,我正在使用

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;
}
4

1 回答 1

0

这是官方的 Visa 方式: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; 
}
于 2020-02-07T06:30:43.643 回答