/// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="refundPaymentRequest">Request</param>
        /// <returns>Result</returns>
        public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            var result = new RefundPaymentResult();
            PayfirmaTransaction payfirma = new PayfirmaTransaction();
            PayfirmaTransactionResponse payfirmaResponse =
                payfirma.ProcessRefund(this.PopulateMerchantCredentials(), refundPaymentRequest.Order.AuthorizationTransactionId,
                Convert.ToDouble(refundPaymentRequest.AmountToRefund), _payfirmaPaymentSettings.IsTest);

            if (!String.IsNullOrEmpty(payfirmaResponse.Error))
            {
                result.AddError(payfirmaResponse.Error);
            }
            else if (!payfirmaResponse.Result)
            {
                result.AddError(payfirmaResponse.ResultMessage);
            }
            else
            {
                var isOrderFullyRefunded = (refundPaymentRequest.AmountToRefund + refundPaymentRequest.Order.RefundedAmount == refundPaymentRequest.Order.OrderTotal);
                result.NewPaymentStatus = isOrderFullyRefunded ? PaymentStatus.Refunded : PaymentStatus.PartiallyRefunded;
            }

            return result;
        }
        public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            var result = new ProcessPaymentResult();

            var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);

            // Credit Card Info
            PayfirmaCreditCard cc = new PayfirmaCreditCard()
            {
                Number = processPaymentRequest.CreditCardNumber,
                ExpMonth = processPaymentRequest.CreditCardExpireMonth,
                ExpYear = processPaymentRequest.CreditCardExpireYear,
                CVV2 = processPaymentRequest.CreditCardCvv2
            };

            // Extra Meta Data
            PayfirmaMetaData payfirmaMeta = new PayfirmaMetaData();
            payfirmaMeta.Firstname = customer.BillingAddress.FirstName;
            payfirmaMeta.Lastname = customer.BillingAddress.LastName;
            if (!String.IsNullOrEmpty(customer.BillingAddress.Company)) { payfirmaMeta.Company = customer.BillingAddress.Company; }
            payfirmaMeta.Address1 = customer.BillingAddress.Address1;
            if (!String.IsNullOrEmpty(customer.BillingAddress.Address2)) { payfirmaMeta.Address2 = customer.BillingAddress.Address2; }
            payfirmaMeta.City = customer.BillingAddress.City;
            if (customer.BillingAddress.StateProvince != null)
            {
                payfirmaMeta.Province = customer.BillingAddress.StateProvince.Name;
            }
            payfirmaMeta.PostalCode = customer.BillingAddress.ZipPostalCode;
            if (customer.BillingAddress.Country != null)
            {
                payfirmaMeta.Country = customer.BillingAddress.Country.Name;
            }
            payfirmaMeta.Email = customer.BillingAddress.Email;

            String currencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode;
            payfirmaMeta.Currency = "CA$";
            if (currencyCode == "USD") { payfirmaMeta.Currency = "US$"; }
            payfirmaMeta.OrderId = processPaymentRequest.OrderGuid.ToString();
            if (!String.IsNullOrEmpty(customer.BillingAddress.PhoneNumber)) { payfirmaMeta.Telephone = customer.BillingAddress.PhoneNumber;  }
            payfirmaMeta.Description = "Payment via nopCommerce.";

            PayfirmaTransaction payfirma = new PayfirmaTransaction();
            PayfirmaTransactionResponse payfirmaResponse;
            if (_payfirmaPaymentSettings.TransactMode == TransactMode.Authorize)
            {
                payfirmaResponse = payfirma.ProcessAuthorize(this.PopulateMerchantCredentials(), cc, payfirmaMeta,
                    Convert.ToDouble(processPaymentRequest.OrderTotal), _payfirmaPaymentSettings.IsTest);
            }
            else
            {
                payfirmaResponse = payfirma.ProcessSale(this.PopulateMerchantCredentials(), cc, payfirmaMeta,
                   Convert.ToDouble(processPaymentRequest.OrderTotal), _payfirmaPaymentSettings.IsTest);
            }

            if (!String.IsNullOrEmpty(payfirmaResponse.Error))
            {
                result.AddError(payfirmaResponse.Error);
            }
            else if (!payfirmaResponse.Result)
            {
                result.AddError(payfirmaResponse.ResultMessage);
            } else {
                result.AvsResult = payfirmaResponse.AVS;
                result.AuthorizationTransactionCode = payfirmaResponse.AuthCode;
                result.AuthorizationTransactionId = payfirmaResponse.TransactionId;
                result.AuthorizationTransactionResult = payfirmaResponse.ResultMessage;

                if (_payfirmaPaymentSettings.TransactMode == TransactMode.Authorize)
                {
                    result.NewPaymentStatus = PaymentStatus.Authorized;
                }
                else
                {
                    result.NewPaymentStatus = PaymentStatus.Paid;
                }
            }

            return result;
        }
        /// <summary>
        /// Captures payment
        /// </summary>
        /// <param name="capturePaymentRequest">Capture payment request</param>
        /// <returns>Capture payment result</returns>
        public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
        {
            var result = new CapturePaymentResult();

            PayfirmaTransaction payfirma = new PayfirmaTransaction();
            PayfirmaTransactionResponse payfirmaResponse =
                payfirma.ProcessCapture(this.PopulateMerchantCredentials(), capturePaymentRequest.Order.AuthorizationTransactionId,
                Convert.ToDouble(capturePaymentRequest.Order.OrderTotal), _payfirmaPaymentSettings.IsTest);

            if (!String.IsNullOrEmpty(payfirmaResponse.Error))
            {
                result.AddError(payfirmaResponse.Error);
            }
            else if (!payfirmaResponse.Result)
            {
                result.AddError(payfirmaResponse.ResultMessage);
            }
            else
            {
                result.CaptureTransactionId = payfirmaResponse.TransactionId;
                result.CaptureTransactionResult = payfirmaResponse.ResultMessage;
                result.NewPaymentStatus = PaymentStatus.Paid;
            }

            return result;
        }