Example #1
0
        public virtual async Task <Subscription> TryCreateSubscriptionFromOrderAsync(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 = (await _paymentPlanService.GetByIdsAsync(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 = (await _paymentPlanService.GetByIdsAsync(order.Items.Select(x => x.ProductId).ToArray())).FirstOrDefault();
            }

            //Generate numbers for new subscriptions
            var store = await _storeService.GetByIdAsync(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);
        }
Example #2
0
        public virtual async Task <PaymentPlanSearchResult> SearchPlansAsync(PaymentPlanSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPlansAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(PaymentPlanSearchCacheRegion.CreateChangeToken());

                var retVal = AbstractTypeFactory <PaymentPlanSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = BuildQuery(repository, criteria);
                    var sortInfos = BuildSortExpression(criteria);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var paymentPlanIds = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                             .Select(x => x.Id)
                                             .Skip(criteria.Skip).Take(criteria.Take)
                                             .ToArrayAsync();

                        //Load plans with preserving sorting order
                        var unorderedResults = await _paymentPlanService.GetByIdsAsync(paymentPlanIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.OrderBy(x => Array.IndexOf(paymentPlanIds, x.Id)).ToArray();
                    }

                    return retVal;
                }
            }));
        }
Example #3
0
        public virtual async Task <PaymentPlanSearchResult> SearchPlansAsync(PaymentPlanSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPlansAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(PaymentPlanSearchCacheRegion.CreateChangeToken());

                var retVal = AbstractTypeFactory <PaymentPlanSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = repository.PaymentPlans;

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <PaymentPlan>(x => x.CreatedDate), SortDirection = SortDirection.Descending
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var paymentPlanEntities = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        var paymentPlanIds = paymentPlanEntities.Select(x => x.Id).ToArray();

                        //Load subscriptions with preserving sorting order
                        var unorderedResults = await _paymentPlanService.GetByIdsAsync(paymentPlanIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.AsQueryable().OrderBySortInfos(sortInfos).ToArray();
                    }

                    return retVal;
                }
            }));
        }
Example #4
0
        public async Task <IActionResult> GetPaymentPlanById(string id)
        {
            var retVal = (await _planService.GetByIdsAsync(new[] { id })).FirstOrDefault();

            return(Ok(retVal));
        }