[英]MiGS Payment Gateway integration implementation in C#
I have a web application which is using payment gateway integration. 我有一个使用支付网关集成的Web应用程序。 Now i am facing some issues in creating Secure Hash Code using sha-256 HMAC algorithm. 现在我在使用sha-256 HMAC算法创建安全哈希代码时遇到一些问题。
I have all the details regarding connecting to migs gateway, but my problem is when i tried to connect to gateway i am getting some issues with the created Hash Code. 我有关于连接到migs网关的所有详细信息,但是我的问题是当我尝试连接到网关时,我在创建的哈希代码时遇到了一些问题。
Constructed URL to MIGS gateway 构造到MIGS网关的URL
https://migs.mastercard.com.au/vpcpay?vpc_AccessCode=XXXXXX&vpc_Amount=6000&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=12345678&vpc_Merchant=TESTXXXXXX&vpc_OrderInfo=54444&vpc_ReturnURL=http%3a%2f%2flocalhost%3a2231%2fTransaction%2fSecureTransaction%3fdataKey=33445566&vpc_Version=1&vpc_SecureHash=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&vpc_SecureHashType=SHA256
Once i fire this URL i am getting an error like below: 触发此网址后,出现如下错误:
HTTP Status - 400
E5000: Cannot form a matching secure hash based on the merchant's request using either of the two merchant's secrets
I have verified the SecretHash and its same as provided by the merchant. 我已经验证了SecretHash及其与商家提供的相同。
Existing implementation C#: 现有的实现C#:
string hashSecret = ConfigurationManager.AppSettings["MigsSecureHashSecret"];
var transactionData = paymentRequest.GetParameters().OrderBy(t => t.Key, new VPCStringComparer()).ToList();
var redirectUrl = VPC_URL + "?" + string.Join("&", transactionData.Select(item => HttpUtility.UrlEncode(item.Key) + "=" + HttpUtility.UrlEncode(item.Value)));
if (!string.IsNullOrEmpty(hashSecret))
{
var hashedData = hashSecret + string.Join("", transactionData.Select(item => item.Value));
redirectUrl += "&vpc_SecureHash=" + Crypto.CreateSHA256Signature(hashedData);
}
return Redirect(redirectUrl);
CreateSHA256Signature function CreateSHA256Signature函数
public static string CreateSHA256Signature (string RawData)
{
var hasher = System.Security.Cryptography.HMACSHA256.Create();
var HashValue = hasher.ComputeHash(Encoding.ASCII.GetBytes(RawData));
return string.Join("", HashValue.Select(b => b.ToString("x2"))).ToUpper();
}
I am not sure whether i have did the right method or not. 我不确定我是否使用了正确的方法。 Please help me in this issue. 请帮我解决这个问题。
Any help will be highly appreciated. 任何帮助将不胜感激。
I assume you've done it by now. 我想您已经完成了。 However, in your supplied code you're trying to create a hash from Secure Hash Secret and all values joined together. 但是,在您提供的代码中,您尝试从Secure Hash Secret创建一个哈希,并将所有值连接在一起。 That's incorrect. 不对 You need to use Secure Hash Secret as a key supplied to HMACSHA256 object and compute a hash from a string of key1=value1&key=value2&... Working code: 您需要使用Secure Hash Secret作为提供给HMACSHA256对象的密钥,并根据key1 = value1&key = value2&...的字符串计算哈希。工作代码:
var secureSecret = "123456789ABCDEF123456789ABCDEF12";
var args = new SortedDictionary<string, string>()
{
{"vpc_Version", "1"},
{"vpc_Command", "refund"},
{"vpc_MerchTxnRef", "TestRefund"},
{"vpc_AccessCode", "XXXXXXXX"},
{"vpc_Merchant", "XXXXXXXX"},
{"vpc_TransNo", "123"},
{"vpc_Amount", "1"}
};
var getPart = "";
foreach (var arg in args)
{
getPart += arg.Key + "=" + arg.Value + "&";
}
getPart = getPart.TrimEnd('&');
var keyBytes = new byte[secureSecret.Length / 2];
for(int i=0;i<keyBytes.Length;i++)
{
keyBytes[i] = byte.Parse(secureSecret.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
var hmac = new HMACSHA256(keyBytes);
var hash = hmac.ComputeHash(Encoding.ASCII.GetBytes(getPart));
var hashString = BitConverter.ToString(hash).Replace("-", "");
var requestUri = "https://migs.mastercard.com.au/vpcpay?"+getPart+"&vpc_SecureHash="+hashString+"&vpc_SecureHashType=SHA256";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.