Esempio n. 1
0
        //Need to create an order record without going through any gateway/payment or other recurring steps - the order has already been changed by PayPal.
        public void ProcessPPECRecurringOrder(int OriginalOrderNumber)
        {
            var originalOrder     = new Order(OriginalOrderNumber);
            var recurringCustomer = new Customer(originalOrder.CustomerID);
            var recurringCart     = new ShoppingCart(recurringCustomer.SkinID, recurringCustomer, CartTypeEnum.RecurringCart, OriginalOrderNumber, false);

            var billingAddress = new Address();

            billingAddress.LoadByCustomer(recurringCustomer.CustomerID, recurringCustomer.PrimaryBillingAddressID, AddressTypes.Billing);
            billingAddress.PaymentMethodLastUsed = AppLogic.ro_PMPayPalExpress;

            var newOrderNumber = Gateway.CreateOrderRecord(recurringCart, 0, billingAddress);

            UpdateOrderFromOriginal(newOrderNumber, originalOrder);

            //Update the new order with some recurring information
            var orderUpdateSql = "UPDATE Orders SET TransactionState = @TransactionState, IsNew = @IsNew, ParentOrderNumber = @ParentOrderNumber, Notes = @Notes WHERE OrderNumber = @OrderNumber";

            var orderUpdateParams = new[] {
                new SqlParameter("@OrderNumber", newOrderNumber),
                new SqlParameter("@TransactionState", AppLogic.ro_TXStateCaptured),
                new SqlParameter("@IsNew", true),
                new SqlParameter("@ParentOrderNumber", OriginalOrderNumber),
                new SqlParameter("@Notes", "This order was created automatically based on a PayPal Express Checkout notification that the recurring subscription had been charged.")
            };

            DB.ExecuteSQL(orderUpdateSql, orderUpdateParams);

            Gateway.CreateOrderShoppingCartRecords(newOrderNumber, recurringCart, recurringCustomer);

            //Then update the ShoppingCart record, which is left behind for next time
            var firstRecurringCartItem = recurringCart.CartItems[0];               //Safe to do as we currently only support one recurring schedule per order
            var nextRecurringDate      = firstRecurringCartItem.NextRecurringShipDate;

            if (nextRecurringDate.Equals(System.DateTime.MinValue))
            {
                // safety check:
                nextRecurringDate = System.DateTime.Now;
            }

            switch (firstRecurringCartItem.RecurringIntervalType)
            {
            case DateIntervalTypeEnum.Day:
                nextRecurringDate = nextRecurringDate.AddDays(firstRecurringCartItem.RecurringInterval);
                break;

            case DateIntervalTypeEnum.Week:
                nextRecurringDate = nextRecurringDate.AddDays(7 * firstRecurringCartItem.RecurringInterval);
                break;

            case DateIntervalTypeEnum.Month:
                nextRecurringDate = nextRecurringDate.AddMonths(firstRecurringCartItem.RecurringInterval);
                break;

            case DateIntervalTypeEnum.Year:
                nextRecurringDate = nextRecurringDate.AddYears(firstRecurringCartItem.RecurringInterval);
                break;

            case DateIntervalTypeEnum.NumberOfDays:
                nextRecurringDate = nextRecurringDate.AddDays(firstRecurringCartItem.RecurringInterval);
                break;

            case DateIntervalTypeEnum.Weekly:
                nextRecurringDate = nextRecurringDate.AddDays(7);
                break;

            case DateIntervalTypeEnum.BiWeekly:
                nextRecurringDate = nextRecurringDate.AddDays(14);
                break;

            case DateIntervalTypeEnum.EveryFourWeeks:
                nextRecurringDate = nextRecurringDate.AddDays(28);
                break;

            case DateIntervalTypeEnum.Monthly:
                nextRecurringDate = nextRecurringDate.AddMonths(1);
                break;

            case DateIntervalTypeEnum.Quarterly:
                nextRecurringDate = nextRecurringDate.AddMonths(3);
                break;

            case DateIntervalTypeEnum.SemiYearly:
                nextRecurringDate = nextRecurringDate.AddMonths(6);
                break;

            case DateIntervalTypeEnum.Yearly:
                nextRecurringDate = nextRecurringDate.AddYears(1);
                break;

            default:                        //Default to monthly like we do elsewhere
                nextRecurringDate = nextRecurringDate.AddMonths(1);
                break;
            }

            var cartUpdateSql = "UPDATE ShoppingCart SET NextRecurringShipDate = @NextRecurringShipDate, RecurringIndex=RecurringIndex+1 WHERE OriginalRecurringOrderNumber = @OriginalRecurringOrderNumber AND CartType = @CartType";

            var cartUpdateParams = new[] {
                new SqlParameter("@OriginalRecurringOrderNumber", OriginalOrderNumber),
                new SqlParameter("@NextRecurringShipDate", nextRecurringDate),
                new SqlParameter("@CartType", ((int)CartTypeEnum.RecurringCart).ToString())
            };

            DB.ExecuteSQL(cartUpdateSql, cartUpdateParams);
        }