Esempio n. 1
0
        public void TestNotificationRequestItemHmac()
        {
            string key            = "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00";
            var    expectedSign   = "ipnxGCaUZ4l8TUW75a71/ghd2Fe5ffvX0pV4TLTntIc=";
            var    additionalData = new Dictionary <string, string>
            {
                { Constants.AdditionalData.HmacSignature, expectedSign }
            };
            var notificationRequestItem = new NotificationRequestItem
            {
                PspReference        = "pspReference",
                OriginalReference   = "originalReference",
                MerchantAccountCode = "merchantAccount",
                MerchantReference   = "reference",
                Amount         = new Model.Amount("EUR", 1000),
                EventCode      = "EVENT",
                Success        = true,
                AdditionalData = additionalData
            };
            var hmacValidator = new HmacValidator();
            var data          = hmacValidator.GetDataToSign(notificationRequestItem);

            Assert.AreEqual("pspReference:originalReference:merchantAccount:reference:1000:EUR:EVENT:true", data);
            var encrypted = hmacValidator.CalculateHmac(notificationRequestItem, key);

            Assert.AreEqual(expectedSign, encrypted);
            notificationRequestItem.AdditionalData[Constants.AdditionalData.HmacSignature] = expectedSign;
            Assert.IsTrue(hmacValidator.IsValidHmac(notificationRequestItem, key));
            notificationRequestItem.AdditionalData[Constants.AdditionalData.HmacSignature] = "notValidSign";
            Assert.IsFalse(hmacValidator.IsValidHmac(notificationRequestItem, key));
        }
        public void TestHmac()
        {
            var data          = "countryCode:currencyCode:merchantAccount:merchantReference:paymentAmount:sessionValidity:skinCode:NL:EUR:MagentoMerchantTest2:TEST-PAYMENT-2017-02-01-14\\:02\\:05:199:2017-02-02T14\\:02\\:05+01\\:00:PKz2KML1";
            var key           = "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00";
            var hmacValidator = new HmacValidator();
            var ecnrypted     = hmacValidator.CalculateHmac(data, key);

            Assert.IsTrue(string.Equals(ecnrypted, "34oR8T1whkQWTv9P+SzKyp8zhusf9n0dpqrm9nsqSJs="));
        }
        public Dictionary <string, string> GetPostParametersFromDlRequest(DirectoryLookupRequest request)
        {
            var config = this.Client.Config;

            var postParameters = new Dictionary <string, string>
            {
                { Fields.CurrencyCode, request.CurrencyCode },
                { Fields.MerchantReference, request.MerchantReference },
                { Fields.SessionValidity, request.SessionValidity },
                { Fields.CountryCode, request.CountryCode }
            };

            if (!string.IsNullOrEmpty(request.MerchantAccount))
            {
                postParameters.Add(Fields.MerchantAccount, request.MerchantAccount);
            }
            else
            {
                postParameters.Add(Fields.MerchantAccount, config.MerchantAccount);
            }
            postParameters.Add(Fields.PaymentAmount, request.PaymentAmount);

            if (!string.IsNullOrEmpty(request.SkinCode))
            {
                postParameters.Add(Fields.SkinCode, request.SkinCode);
            }
            else
            {
                postParameters.Add(Fields.SkinCode, config.SkinCode);
            }



            var hmacValidator = new HmacValidator();

            var dataToSign = hmacValidator.BuildSigningString(postParameters);

            string hmacKey;

            if (!string.IsNullOrEmpty(request.HmacKey))
            {
                hmacKey = request.HmacKey;
            }
            else
            {
                hmacKey = config.HmacKey;
            }

            var merchantSig = hmacValidator.CalculateHmac(dataToSign, hmacKey);

            postParameters.Add(Fields.MerchantSig, merchantSig);

            return(postParameters);
        }