/// <summary>
    /// Returns a PayExPayment for the given cart, if one exists
    /// </summary>
    /// <param name="cart">Cart</param>
    /// <returns>The PayExPayment gor the given cart</returns>
    private PayExPayment GetPayExPayment(Cart cart)
    {
        if (cart.OrderForms == null || cart.OrderForms.Count == 0 || cart.OrderForms[0].Payments == null || cart.OrderForms[0].Payments.Count == 0)
        {
            return(null);
        }

        List <Payment> payments = cart.OrderForms[0].Payments.Where(p => p is PayExPayment).ToList();

        payments = PaymentTransactionTypeManager.GetResultingPaymentsByTransactionType(payments,
                                                                                       TransactionType.Authorization).ToList();

        if (payments.Any())
        {
            return(payments.First() as PayExPayment);
        }
        return(null);
    }
Exemple #2
0
        /// <summary>
        /// Calculates the totals order forms.
        /// </summary>
        /// <param name="form">The form.</param>
        private void CalculateTotalsOrderForms(OrderForm form)
        {
            decimal subTotal              = 0m;
            decimal discountTotal         = 0m;
            decimal shippingDiscountTotal = 0m;
            decimal shippingTotal         = 0m;

            foreach (LineItem item in form.LineItems)
            {
                decimal lineItemDiscount = item.LineItemDiscountAmount + item.OrderLevelDiscountAmount;
                item.ExtendedPrice = item.PlacedPrice * item.Quantity - lineItemDiscount;
                subTotal          += item.ExtendedPrice;
                discountTotal     += lineItemDiscount;
            }

            foreach (Shipment shipment in form.Shipments)
            {
                shipment.SubTotal      = CalculateShipmentSubtotal(shipment);
                shippingTotal         += shipment.ShipmentTotal;
                shippingTotal         -= shipment.ShippingDiscountAmount;
                shippingDiscountTotal += shipment.ShippingDiscountAmount;
            }

            form.ShippingTotal  = shippingTotal;
            form.DiscountAmount = discountTotal + shippingDiscountTotal;
            form.SubTotal       = subTotal;

            form.Total = subTotal + shippingTotal;

            //Calculate payment total
            var formPayments = form.Payments.ToArray();
            var resultingAuthorizedPayments = PaymentTransactionTypeManager.GetResultingPaymentsByTransactionType(formPayments, TransactionType.Authorization);
            var resultingCapturedPayments   = PaymentTransactionTypeManager.GetResultingPaymentsByTransactionType(formPayments, TransactionType.Capture);
            var resultingSalsePayments      = PaymentTransactionTypeManager.GetResultingPaymentsByTransactionType(formPayments, TransactionType.Sale);
            var resultingCreditPayments     = PaymentTransactionTypeManager.GetResultingPaymentsByTransactionType(formPayments, TransactionType.Credit);

            form.AuthorizedPaymentTotal = resultingAuthorizedPayments.Where(x => PaymentStatusManager.GetPaymentStatus(x) == PaymentStatus.Processed).Sum(y => y.Amount);
            form.CapturedPaymentTotal   = resultingSalsePayments.Where(x => PaymentStatusManager.GetPaymentStatus(x) == PaymentStatus.Processed).Sum(y => y.Amount);
            form.CapturedPaymentTotal  += resultingCapturedPayments.Where(x => PaymentStatusManager.GetPaymentStatus(x) == PaymentStatus.Processed).Sum(y => y.Amount);
            form.CapturedPaymentTotal  -= resultingCreditPayments.Where(x => PaymentStatusManager.GetPaymentStatus(x) == PaymentStatus.Processed).Sum(y => y.Amount);
        }
        /// <summary>
        /// Processes the payment.
        /// </summary>
        private void ProcessPayment()
        {
            OrderGroup order = OrderGroup;

            decimal shipmentTotal = CalculateShipmentTotal();

            //Calculate payment total
            var formPayments = order.OrderForms[0].Payments.ToArray();
            var resultingAuthorizedPayments = PaymentTransactionTypeManager.GetResultingPaymentsByTransactionType(formPayments, TransactionType.Authorization);
            var authorizedPayments          = resultingAuthorizedPayments.Where(x => PaymentStatusManager.GetPaymentStatus(x) == PaymentStatus.Processed);

            //find intire authorization
            var intirePayment = authorizedPayments.OrderBy(x => x.Amount).FirstOrDefault(x => x.Amount >= shipmentTotal);

            if (intirePayment == null)
            {
                var payments = authorizedPayments.OrderByDescending(x => x.Amount);
                foreach (Payment partialPayment in payments)
                {
                    if (partialPayment.Amount < shipmentTotal)
                    {
                        DoCapture(partialPayment, partialPayment.Amount);
                        shipmentTotal -= partialPayment.Amount;
                    }
                    else
                    {
                        DoCapture(partialPayment, shipmentTotal);
                        break;
                    }
                }
            }
            else
            {
                DoCapture(intirePayment, shipmentTotal);
            }
        }