public PaymentWrite BuildPaymentForCreate(ShopifyTransaction transactionRecord)
        {
            var transaction = _shopifyJsonService.RetrieveTransaction(transactionRecord.ShopifyTransactionId);
            var gateway     = _settingsRepository.RetrievePaymentGatewayByShopifyId(transaction.gateway);

            // Locate the Acumatica Customer
            //
            var shopifyCustomerId = transactionRecord.CustomerId();
            var customer          = _syncOrderRepository.RetrieveCustomer(shopifyCustomerId);
            var acumaticaCustId   = customer.AcumaticaCustId();

            // Build the Payment Ref and Description
            //
            var orderRecord = _syncOrderRepository.RetrieveShopifyOrder(transactionRecord.OrderId());
            var order       = _shopifyJsonService.RetrieveOrder(orderRecord.ShopifyOrderId);

            // Create the payload for Acumatica
            //
            var payment = new PaymentWrite();

            payment.CustomerID = acumaticaCustId.ToValue();
            payment.Hold       = false.ToValue();
            payment.Type       = PaymentType.Payment.ToValue();
            payment.PaymentRef = $"{transaction.id}".ToValue();

            var createdAtUtc  = (transaction.created_at ?? order.created_at).UtcDateTime;
            var acumaticaDate = _acumaticaTimeZoneService.ToAcumaticaTimeZone(createdAtUtc);

            payment.ApplicationDate = acumaticaDate.Date.ToValue();

            // Amount computations
            //
            payment.PaymentAmount = ((double)transaction.amount).ToValue();
            var appliedToOrder = orderRecord.TheoreticalPaymentRemaining();


            // Applied to Documents
            //
            var acumaticaOrderRef = orderRecord.AcumaticaSalesOrderId();

            if (acumaticaOrderRef.HasValue() && orderRecord.IsNotCancelledOrAllRefunded())
            {
                payment.OrdersToApply =
                    PaymentOrdersRef.ForOrder(acumaticaOrderRef, SalesOrderType.SO, (double)appliedToOrder);
            }

            payment.PaymentMethod = gateway.AcumaticaPaymentMethod.ToValue();
            payment.CashAccount   = gateway.AcumaticaCashAccount.ToValue();
            payment.Description   = $"Payment for Shopify Order #{orderRecord.ShopifyOrderNumber}".ToValue();

            return(payment);
        }
        private PaymentWrite BuildCustomerRefund(ShopifyTransaction transactionRecord)
        {
            var transaction    = _shopifyJsonService.RetrieveTransaction(transactionRecord.ShopifyTransactionId);
            var paymentGateway = _settingsRepository.RetrievePaymentGatewayByShopifyId(transaction.gateway);

            // Locate the Acumatica Customer
            //
            var shopifyCustomerId = transactionRecord.CustomerId();
            var customer          = _syncOrderRepository.RetrieveCustomer(shopifyCustomerId);
            var acumaticaCustId   = customer.AcumaticaCustId();

            // Build the Payment Ref and Description
            //
            var order = _syncOrderRepository.RetrieveShopifyOrder(transactionRecord.OrderId());

            // Create the payload for Acumatica
            //
            var refundPayment = new PaymentWrite();

            refundPayment.CustomerID = acumaticaCustId.ToValue();
            refundPayment.Type       = PaymentType.CustomerRefund.ToValue();
            refundPayment.PaymentRef = $"{transaction.id}".ToValue();

            // Reference to the original Payment
            //
            if (transactionRecord.NeedManualApply())
            {
                refundPayment.Hold          = true.ToValue();
                refundPayment.PaymentAmount = ((double)transaction.amount).ToValue();
            }
            else
            {
                var acumaticaPayment = order.PaymentTransaction().AcumaticaPayment;
                var balance          = _paymentClient.RetrievePaymentBalance(acumaticaPayment.AcumaticaRefNbr, PaymentType.Payment);

                var amountToApply = transaction.amount > balance ? balance : transaction.amount;

                refundPayment.Hold          = false.ToValue();
                refundPayment.PaymentAmount = ((double)amountToApply).ToValue();
                refundPayment.DocumentsToApply
                    = PaymentDocumentsToApply.ForDocument(
                          acumaticaPayment.AcumaticaRefNbr, acumaticaPayment.AcumaticaDocType, (double)amountToApply);
            }

            // Amounts
            //
            refundPayment.PaymentMethod = paymentGateway.AcumaticaPaymentMethod.ToValue();
            refundPayment.CashAccount   = paymentGateway.AcumaticaCashAccount.ToValue();
            refundPayment.Description   = $"Refund for Order #{order.ShopifyOrderNumber} (TransId #{transaction.id})".ToValue();
            return(refundPayment);
        }
        private PaymentWrite BuildPaymentForUpdate(ShopifyTransaction transactionRecord)
        {
            // Build the Payment Ref and Description
            //
            var order             = _syncOrderRepository.RetrieveShopifyOrder(transactionRecord.OrderId());
            var acumaticaOrderRef = order.AcumaticaSalesOrderId();
            var paymentNbr        = transactionRecord.AcumaticaPayment.AcumaticaRefNbr;

            // Get the balance from Acumatica in case other Invoices have grabbed some of the monies!!
            //
            var balance = _paymentClient.RetrievePaymentBalance(paymentNbr, PaymentType.Payment);
            var amountToApply
                = balance < order.TheoreticalPaymentRemaining()
                    ? balance : order.TheoreticalPaymentRemaining();

            var payment = new PaymentWrite();

            payment.ReferenceNbr = paymentNbr.ToValue();
            payment.Type         = PaymentType.Payment.ToValue();
            payment.OrdersToApply
                = PaymentOrdersRef.ForOrder(acumaticaOrderRef, SalesOrderType.SO, (double)amountToApply);

            return(payment);
        }