private bool VerifyMd5Hash(Payment payment)
        {
            bool useMd5 = payment.PaymentMethod.DynamicProperty <bool>().UseMd5;

            if (!useMd5)
            {
                return(true);
            }

            string key1     = payment.PaymentMethod.DynamicProperty <string>().Key1.ToString();
            string key2     = payment.PaymentMethod.DynamicProperty <string>().Key2.ToString();
            string transact = GetTransactionParameter();

            const string format = "When using md5 \"{0}\" cannot be null or empty";

            var authKeyParameter  = GetParameter("authkey", format);
            var currencyParameter = GetParameter("currency", format);
            var amountParameter   = GetParameter("amount", format);

            var currencyCodeTranslater = new CurrencyCodeTranslater();
            int isoCode = currencyCodeTranslater.FromIsoCode(currencyParameter);

            var hashComputer   = new DibsMd5Computer();
            var md5ResponseKey = hashComputer.GetPostMd5Key(transact, amountParameter, isoCode, key1, key2);


            var verifyMd5Hash = authKeyParameter.Equals(md5ResponseKey);

            if (!verifyMd5Hash)
            {
                _loggingService.Log <DibsPaymentMethodService>(string.Format("Comparing response authkey: '{0}' with calculated key: '{1}' returned false. Hash cannot be verified!", authKeyParameter, md5ResponseKey));
            }

            return(verifyMd5Hash);
        }
 public DibsPaymentMethodService(DibsPageBuilder pageBuilder, DibsMd5Computer md5Computer, IWebRuntimeInspector webRuntimeInspector, ILoggingService loggingService)
 {
     _webRuntimeInspector = webRuntimeInspector;
     _loggingService      = loggingService;
     DibsMd5Computer      = md5Computer;
     PageBuilder          = pageBuilder;
 }
 public DibsPageBuilder(IDomainService domainService, DibsMd5Computer dibsMd5Computer, IAbsoluteUrlService absoluteUrlService, ICallbackUrl callbackUrl)
 {
     _absoluteUrlService = absoluteUrlService;
     _callbackUrl        = callbackUrl;
     DomainService       = domainService;
     DibsMd5Computer     = dibsMd5Computer;
 }
        /// <summary>
        /// Refunds the payment from the payment provider. This is often used when you need to call external services to handle the refund process.
        /// </summary>
        /// <param name="payment">The payment.</param>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        protected override bool  RefundPaymentInternal(Payment payment, out string status)
        {
            string merchant = payment.PaymentMethod.DynamicProperty <string>().Merchant.ToString();
            string key1     = payment.PaymentMethod.DynamicProperty <string>().Key1.ToString();
            string key2     = payment.PaymentMethod.DynamicProperty <string>().Key2.ToString();
            string login    = payment.PaymentMethod.DynamicProperty <string>().Login.ToString();
            string password = payment.PaymentMethod.DynamicProperty <string>().Password.ToString();

            var amount = payment.Amount.ToCents().ToString();

            var transactionId = payment.TransactionId;
            var referenceId   = payment.ReferenceId;
            var key           = DibsMd5Computer.GetRefundKey(referenceId, transactionId, amount, key1, key2, merchant);

            var isoCode   = payment.PurchaseOrder.BillingCurrency.ISOCode;
            var refundUrl = GetRefundUrl(merchant, amount, isoCode, transactionId, referenceId, key);

            var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(refundUrl);

            httpWebRequest.Credentials = new NetworkCredential(login, password);

            WebResponse webResponse;

            try
            {
                webResponse = httpWebRequest.GetResponse();
            }
            catch (WebException exception)
            {
                var httpWebResponse = exception.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    if (httpWebResponse.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        status = exception.Message;
                        return(false);
                    }
                }

                throw;
            }

            var    streamReader = new StreamReader(webResponse.GetResponseStream());
            string errorStatus  = streamReader.ReadToEnd();

            var dibsResponseStatus = GetStatus(errorStatus);

            if (dibsResponseStatus)
            {
                status = PaymentMessages.RefundSuccess + " >> " + GetErrorMessage(errorStatus);
            }
            else
            {
                status = PaymentMessages.RefundFailed + " >> " + GetErrorMessage(errorStatus);
            }

            return(dibsResponseStatus);
        }
        /// <summary>
        /// Cancels the payment from the payment provider. This is often used when you need to call external services to handle the cancel process.
        /// </summary>
        /// <param name="payment">The payment.</param>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        protected override bool CancelPaymentInternal(Payment payment, out string status)
        {
            string merchant = payment.PaymentMethod.DynamicProperty <string>().Merchant.ToString();
            string key1     = payment.PaymentMethod.DynamicProperty <string>().Key1.ToString();
            string key2     = payment.PaymentMethod.DynamicProperty <string>().Key2.ToString();
            string login    = payment.PaymentMethod.DynamicProperty <string>().Login.ToString();
            string password = payment.PaymentMethod.DynamicProperty <string>().Password.ToString();

            var referenceId = payment.ReferenceId;
            var key         = DibsMd5Computer.GetCancelMd5Key(referenceId, payment.TransactionId, key1, key2, merchant);

            var refundUrl = GetCancelUrl(merchant, referenceId, payment.TransactionId, key);

            var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(refundUrl);

            httpWebRequest.Credentials = new NetworkCredential(login, password);

            WebResponse webResponse;

            try
            {
                webResponse = httpWebRequest.GetResponse();
            }
            catch (WebException exception)
            {
                var httpWebResponse = exception.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    if (httpWebResponse.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        status = exception.Message;
                        return(false);
                    }
                }

                throw;
            }

            var    streamReader = new StreamReader(webResponse.GetResponseStream());
            string errorStatus  = streamReader.ReadToEnd();

            bool dibsResponseStatus = GetStatus(errorStatus);

            status = GetErrorMessage(errorStatus);

            return(dibsResponseStatus);
        }