Exemple #1
0
        public IHashing CreateHashing(EHashAlgorithm hash)
        {
            switch (hash)
            {
            case EHashAlgorithm.SHA1:
                return(new SHA1Adapter());

                break;

            case EHashAlgorithm.SHA224:
                return(new SHA224Adapter());

                break;

            case EHashAlgorithm.SHA256:
                return(new SHA256Adapter());

                break;

            case EHashAlgorithm.SHA512:
                return(new SHA512Adapter());

                break;

            case EHashAlgorithm.SHA384:
                return(new SHA384Adapter());

            default:
                throw new ArgumentOutOfRangeException(nameof(hash), hash, null);
            }
        }
Exemple #2
0
        public string GetValue(IPayment payment, EHashAlgorithm encryptType = EHashAlgorithm.SHA256)
        {
            if (payment is null)
            {
                throw new ArgumentNullException(nameof(payment));
            }
            var properties = typeof(IPayment).GetProperties();
            var parameters = properties
                             .Where(property => property.Name != "URL")
                             .Where(property => property.GetValue(payment) != null)
                             .ToDictionary(property => property.Name, property => property.GetValue(payment).ToString());

            return(GetValue(parameters, _hashKey, _hashIV, encryptType));
        }
Exemple #3
0
 public PaymentConfiguration()
 {
     Send = new PaymentSendConfiguration(
         this,
         url => _url               = url,
         id => _merchantId         = id,
         checkMac => _checkMac     = checkMac,
         type => _encryptType      = type,
         id => _storeId            = id,
         isPlatform => _isPlatform = isPlatform
         );
     Return = new PaymentReturnConfiguration(
         this,
         url => _serverUrl = url,
         url => _clientUrl = url,
         url => _clientUrlWithExtraPaidInfo = url
         );
     Transaction = new PaymentTransactionConfiguration(
         this,
         payment => _payment = payment
         );
 }
Exemple #4
0
        public static string GetValue(IDictionary <string, string> parameters, string key, string iv, EHashAlgorithm encryptType = EHashAlgorithm.SHA256)
        {
            string checkMacValue    = string.Empty;
            var    sortedParameters = parameters
                                      .OrderBy(o => o.Key)
                                      .Select(param => $"{param.Key}={param.Value}");
            var parameterString = string.Join("&", sortedParameters);

            checkMacValue = $"HashKey={key}&{parameterString}&HashIV={iv}";
            checkMacValue = HttpUtility.UrlEncode(checkMacValue).ToLower();
            switch (encryptType)
            {
            case EHashAlgorithm.SHA256:
                checkMacValue = SHA256ComputeHash(checkMacValue);
                break;

            default:
                checkMacValue = MD5ComputeHash(checkMacValue);
                break;
            }
            return(checkMacValue.ToUpper());
        }
Exemple #5
0
        public static bool PaymentResultIsValid(PaymentResult result, string hashKey, string hashIV, EHashAlgorithm encryptType = EHashAlgorithm.SHA256)
        {
            if (result is null)
            {
                throw new ArgumentNullException(nameof(result));
            }
            if (result.CheckMacValue is null)
            {
                throw new ArgumentNullException(nameof(result.CheckMacValue));
            }
            if (string.IsNullOrEmpty(hashKey))
            {
                throw new ArgumentNullException(nameof(result));
            }
            if (string.IsNullOrEmpty(hashIV))
            {
                throw new ArgumentNullException(nameof(result));
            }

            var properties = typeof(IPaymentResult).GetProperties();
            var parameters = properties
                             .Where(property => property.Name != nameof(PaymentResult.CheckMacValue))
                             .ToDictionary(property => property.Name, property =>
            {
                var value = property.GetValue(result);
                return(value is null ? string.Empty : value.ToString());
            });
            var toCheck = CheckMac.GetValue(parameters, hashKey, hashIV, encryptType);

            return(toCheck == result.CheckMacValue);
        }