Exemplo n.º 1
0
        public virtual Subscription TryCreateSubscriptionFromOrder(CustomerOrder order)
        {
            Subscription retVal      = null;
            PaymentPlan  paymentPlan = null;

            if (!string.IsNullOrEmpty(order.ShoppingCartId))
            {
                //Retrieve payment plan with id as the same order original shopping cart id
                paymentPlan = _paymentPlanService.GetByIds(new[] { order.ShoppingCartId }).FirstOrDefault();
            }
            if (paymentPlan == null)
            {
                //Try to create subscription if order line item with have defined PaymentPlan
                //TODO: On the right must also be taken into account when the situation in the order contains items with several different plans
                paymentPlan = _paymentPlanService.GetByIds(order.Items.Select(x => x.ProductId).ToArray()).FirstOrDefault();
            }

            //Generate numbers for new subscriptions
            var store          = _storeService.GetById(order.StoreId);
            var numberTemplate = store.Settings.GetSettingValue("Subscription.SubscriptionNewNumberTemplate", "SU{0:yyMMdd}-{1:D5}");

            if (paymentPlan != null)
            {
                var now = DateTime.UtcNow;
                //There need to make "prototype" for future orders which will be created by subscription schedule information
                retVal = AbstractTypeFactory <Subscription> .TryCreateInstance <Subscription>();

                retVal.StoreId = order.StoreId;
                retVal.Number  = _uniqueNumberGenerator.GenerateNumber(numberTemplate);
                retVal.CustomerOrderPrototype = CloneCustomerOrder(order);
                //Need to prevent subscription creation for prototype order in CreateSubscriptionHandler
                retVal.CustomerOrderPrototype.Number      = retVal.Number;
                retVal.CustomerOrderPrototype.IsPrototype = true;
                retVal.CustomerId         = order.CustomerId;
                retVal.CustomerName       = order.CustomerName;
                retVal.Interval           = paymentPlan.Interval;
                retVal.IntervalCount      = paymentPlan.IntervalCount;
                retVal.StartDate          = now;
                retVal.CurrentPeriodStart = now;
                retVal.TrialPeriodDays    = paymentPlan.TrialPeriodDays;
                retVal.SubscriptionStatus = SubscriptionStatus.Active;
                retVal.CurrentPeriodEnd   = GetPeriodEnd(now, paymentPlan.Interval, paymentPlan.IntervalCount);
                if (retVal.TrialPeriodDays > 0)
                {
                    retVal.TrialSart = now;
                    retVal.TrialEnd  = GetPeriodEnd(now, PaymentInterval.Days, retVal.TrialPeriodDays);
                    //For trial need to shift start and end period
                    retVal.CurrentPeriodStart = retVal.TrialEnd;
                    retVal.CurrentPeriodEnd   = GetPeriodEnd(retVal.TrialEnd.Value, paymentPlan.Interval, paymentPlan.IntervalCount);
                }

                retVal.CustomerOrders = new List <CustomerOrder>
                {
                    order
                };
            }
            return(retVal);
        }
Exemplo n.º 2
0
        public IHttpActionResult GetPaymentPlanById(string id)
        {
            var retVal = _planService.GetByIds(new[] { id }).FirstOrDefault();

            return(Ok(retVal));
        }