private PaymentProcessingResult SendRefundRequest(IPurchaseOrder po, IPayment payment)
        {
            try
            {
                var requestDoc  = _requestDocumentCreation.CreateDocumentForRefundRequest(payment);
                var responseDoc = DocumentHelpers.SendTransaction(requestDoc, _dataCashConfiguration.Config);
                if (DocumentHelpers.IsSuccessful(responseDoc))
                {
                    payment.ProviderTransactionID = DocumentHelpers.GetResponseInfo(responseDoc, "Response.datacash_reference");

                    var message = string.Format("[{0}] [RefundTransaction-{1}] [Status: {2}] Response: {3} at Time stamp={4}.",
                                                payment.PaymentMethodName,
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.datacash_reference"),
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.status"),
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.reason"),
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.time")
                                                );

                    // add a new order note about this refund
                    AddNoteToPurchaseOrder(po, po.CustomerId, "REFUND", message);
                    _orderRepository.Save(po);

                    return(PaymentProcessingResult.CreateSuccessfulResult(message));
                }

                return(PaymentProcessingResult.CreateUnsuccessfulResult(DocumentHelpers.GetErrorMessage(responseDoc)));
            }
            catch (System.Exception e)
            {
                return(PaymentProcessingResult.CreateUnsuccessfulResult(e.Message));
            }
        }
        private PaymentProcessingResult SendFulfillRequest(IPurchaseOrder po, IOrderForm orderForm, IPayment payment)
        {
            try
            {
                var requestDoc  = _requestDocumentCreation.CreateDocumentForFulfillRequest(payment, orderForm);
                var responseDoc = DocumentHelpers.SendTransaction(requestDoc, _dataCashConfiguration.Config);
                if (DocumentHelpers.IsSuccessful(responseDoc))
                {
                    // Extract the response details.
                    // When doing capture, refund, etc... transactions, DataCase will return a new Reference Id. We need to store this to ProviderTransactionID
                    // instead of TransactionID, because TransactionID should be the Authorization reference Id, and ProviderTransactionID will be used when we want to refund.
                    payment.ProviderTransactionID = DocumentHelpers.GetResponseInfo(responseDoc, "Response.datacash_reference");

                    var message = string.Format("[{0}] [Capture payment-{1}] [Status: {2}] .Response: {3} at Time stamp={4}",
                                                payment.PaymentMethodName,
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.merchantreference"),
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.status"),
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.reason"),
                                                DocumentHelpers.GetResponseInfo(responseDoc, "Response.time")
                                                );

                    // add a new order note about this capture
                    AddNoteToPurchaseOrder(po, po.CustomerId, "CAPTURE", message);
                    _orderRepository.Save(po);

                    return(PaymentProcessingResult.CreateSuccessfulResult(message));
                }

                return(PaymentProcessingResult.CreateUnsuccessfulResult(DocumentHelpers.GetErrorMessage(responseDoc)));
            }
            catch (System.Exception e)
            {
                return(PaymentProcessingResult.CreateUnsuccessfulResult(e.Message));
            }
        }
        private PaymentProcessingResult ProcessPaymentCheckout(IPayment payment, ICart cart)
        {
            var merchRef = DateTime.Now.Ticks.ToString();

            payment.Properties[DataCashMerchantReferencePropertyName] = merchRef; // A unique reference number for each transaction (Min 6, max 30 alphanumeric character)

            var notifyUrl = UriSupport.AbsoluteUrlBySettings(Utilities.GetUrlFromStartPageReferenceProperty("DataCashPaymentPage"));

            notifyUrl = UriUtil.AddQueryString(notifyUrl, "accept", "true");
            notifyUrl = UriUtil.AddQueryString(notifyUrl, "hash", Utilities.GetSHA256Key(merchRef + "accepted"));

            var requestDoc = _requestDocumentCreation.CreateDocumentForPaymentCheckout(cart, payment, notifyUrl);

            var    responseDoc = DocumentHelpers.SendTransaction(requestDoc, _dataCashConfiguration.Config);
            string redirectUrl;

            if (DocumentHelpers.IsSuccessful(responseDoc))
            {
                redirectUrl = $"{responseDoc.get("Response.HpsTxn.hps_url")}?HPS_SessionID={responseDoc.get("Response.HpsTxn.session_id")}";
                payment.Properties[DataCashReferencePropertyName] = responseDoc.get("Response.datacash_reference");
            }
            else
            {
                return(PaymentProcessingResult.CreateUnsuccessfulResult(DocumentHelpers.GetErrorMessage(responseDoc)));
            }

            _orderRepository.Save(cart);

            var message = $"---DataCash--. Redirect end user to {redirectUrl}";

            _logger.Information(message);

            return(PaymentProcessingResult.CreateSuccessfulResult(message, redirectUrl));
        }
        private PaymentProcessingResult PreAuthenticateRequest(IOrderGroup orderGroup, IOrderForm orderForm, IPayment payment, out string authenticateCode)
        {
            authenticateCode = string.Empty;
            try
            {
                var requestDoc  = _requestDocumentCreation.CreateDocumentForPreAuthenticateRequest(payment, orderForm, orderGroup.Currency);
                var responseDoc = DocumentHelpers.SendTransaction(requestDoc, _dataCashConfiguration.Config);
                if (DocumentHelpers.IsSuccessful(responseDoc))
                {
                    authenticateCode = responseDoc.get("Response.CardTxn.authcode");
                    return(string.IsNullOrEmpty(authenticateCode) ?
                           PaymentProcessingResult.CreateUnsuccessfulResult(string.Empty) :
                           PaymentProcessingResult.CreateSuccessfulResult(string.Empty));
                }

                return(PaymentProcessingResult.CreateUnsuccessfulResult(DocumentHelpers.GetErrorMessage(responseDoc)));
            }
            catch (System.Exception e)
            {
                return(PaymentProcessingResult.CreateUnsuccessfulResult(e.Message));
            }
        }