如何将C#中的migs api与vcp_CardNum和vpc_CardSecurityCode以及SHA256安全哈希类型集成

时间:2017-07-11 21:20:46

标签: c# mastercard

与C#集成,包括使用SHA256哈希的卡详细信息

1 个答案:

答案 0 :(得分:1)

以下是取自proj solution

的工作代码
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";