【问题标题】:MiGS Payment Gateway integration implementation in C#C# 中的 MiGS 支付网关集成实现
【发布时间】:2017-01-20 10:14:03
【问题描述】:

我有一个使用支付网关集成的 Web 应用程序。现在我在使用 sha-256 HMAC 算法创建安全哈希码时遇到了一些问题。

我有关于连接到 Migs 网关的所有详细信息,但我的问题是当我尝试连接到网关时,我遇到了创建的哈希码的一些问题。

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

一旦我触发此 URL,我就会收到如下错误:

HTTP Status - 400

E5000: Cannot form a matching secure hash based on the merchant's request using either of the two merchant's secrets

我已验证 SecretHash 与商家提供的相同。

现有的 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);

创建SHA256签名函数

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();
        }

我不确定我是否采用了正确的方法。请帮助我解决这个问题。

我们将不胜感激。

【问题讨论】:

    标签: c# asp.net asp.net-web-api payment-gateway mastercard


    【解决方案1】:

    我假设你现在已经完成了。但是,在您提供的代码中,您尝试从 Secure Hash Secret 创建一个哈希,并将所有值连接在一起。这是不正确的。您需要使用 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";
    

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 2023-04-08
      • 2011-05-05
      • 2012-02-19
      • 2018-12-05
      • 2012-08-27
      • 2012-07-17
      • 2016-02-16
      • 2016-02-02
      相关资源
      最近更新 更多