public static void AddToPaypalRecurringBilling(IPTV2Entities context, Product product, Offering offering, User user, DateTime registDt, string subscr_id, bool IsRecurringSignUp = false, int TrialProductId = 0)
        {
            try
            {
                //Check if product is subscription product
                if (product is SubscriptionProduct)
                {
                    //check if there are any recurring products that have the same productgroup
                    SubscriptionProduct subscriptionProduct = (SubscriptionProduct)product;

                    //Get user's recurring productGroups
                    var recurringProductGroups = user.GetRecurringProductGroups(offering);
                    if (!recurringProductGroups.Contains(subscriptionProduct.ProductGroup))
                    {
                        var productPackage = context.ProductPackages.FirstOrDefault(p => p.ProductId == product.ProductId);
                        if (productPackage != null)
                        {
                            var entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == productPackage.PackageId);
                            if (entitlement != null)
                            {
                                var paypalRecurringBilling = user.RecurringBillings.FirstOrDefault(r => r is PaypalRecurringBilling && r.StatusId == GlobalConfig.Visible && String.Compare(((PaypalRecurringBilling)r).SubscriberId, subscr_id, true) == 0);
                                if (paypalRecurringBilling == null)
                                {
                                    var billing = new PaypalRecurringBilling()
                                    {
                                        CreatedOn = registDt,
                                        Product = product,
                                        User = user,
                                        UpdatedOn = registDt,
                                        EndDate = entitlement.EndDate,
                                        //NextRun = entitlement.EndDate.AddDays(-3).Date, // Run day before expiry
                                        NextRun = entitlement.EndDate.Date,
                                        StatusId = GlobalConfig.Visible,
                                        Offering = offering,
                                        Package = (Package)productPackage.Package,
                                        SubscriberId = subscr_id,
                                        NumberOfAttempts = 0
                                    };
                                    context.RecurringBillings.Add(billing);
                                }
                                else
                                {
                                    if (paypalRecurringBilling.Product != null)
                                    {
                                        var recurringProduct = paypalRecurringBilling.Product;
                                        if (recurringProduct is SubscriptionProduct)
                                        {
                                            var recurringSubscriptionProduct = (SubscriptionProduct)recurringProduct;
                                            paypalRecurringBilling.NextRun = MyUtility.getEntitlementEndDate(recurringSubscriptionProduct.Duration, recurringSubscriptionProduct.DurationType, paypalRecurringBilling.NextRun != null ? (DateTime)paypalRecurringBilling.NextRun : registDt.Date);
                                            paypalRecurringBilling.UpdatedOn = DateTime.Now;
                                        }
                                    }
                                }
                                context.SaveChanges();
                            }
                            else
                            {
                                if (IsRecurringSignUp)
                                {
                                    // get  trial product
                                    var trialProduct = context.Products.FirstOrDefault(p => p.ProductId == TrialProductId);
                                    SubscriptionProduct trialSubscriptionProduct = (SubscriptionProduct)trialProduct;
                                    var billing = new PaypalRecurringBilling()
                                    {
                                        CreatedOn = registDt,
                                        Product = product,
                                        User = user,
                                        UpdatedOn = registDt,
                                        //NextRun = entitlement.EndDate.AddDays(-3).Date, // Run day before expiry                                    
                                        EndDate = MyUtility.getEntitlementEndDate(trialSubscriptionProduct.Duration, trialSubscriptionProduct.DurationType, registDt),
                                        NextRun = MyUtility.getEntitlementEndDate(trialSubscriptionProduct.Duration, trialSubscriptionProduct.DurationType, registDt),
                                        StatusId = GlobalConfig.Visible,
                                        Offering = offering,
                                        Package = (Package)productPackage.Package,
                                        SubscriberId = subscr_id,
                                        NumberOfAttempts = 0
                                    };
                                    context.RecurringBillings.Add(billing);
                                    context.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) { MyUtility.LogException(e); }
        }
        private RecurringBillingReturnValue CheckIfUserIsEnrolledToSameRecurringProductGroup(IPTV2Entities context, Offering offering, User user, Product product)
        {
            RecurringBillingReturnValue returnValue = new RecurringBillingReturnValue()
            {
                container = null,
                value = false
            };
            var profiler = MiniProfiler.Current;
            using (profiler.Step("Check Recurring Enrolment"))
            {
                try
                {

                    if (product is SubscriptionProduct)
                    {
                        // check if user is part of recurring
                        var subscriptionProduct = (SubscriptionProduct)product;
                        //Get user's recurring productGroups
                        var recurringProductGroups = user.GetRecurringProductGroups(offering);
                        if (recurringProductGroups.Contains(subscriptionProduct.ProductGroup))
                        {
                            var productPackage = context.ProductPackages.FirstOrDefault(p => p.ProductId == product.ProductId);
                            if (productPackage != null)
                            {
                                var entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == productPackage.PackageId);
                                if (entitlement != null)
                                {
                                    var container = new RecurringBillingContainer()
                                    {
                                        user = user,
                                        product = product,
                                        entitlement = entitlement,
                                        package = (Package)productPackage.Package
                                    };
                                    returnValue.value = true;
                                    returnValue.container = container;
                                }
                            }
                        }
                    }
                }
                catch (Exception e) { MyUtility.LogException(e); }
            }
            return returnValue;
        }
        public static void AddToRecurringBilling(IPTV2Entities context, Product product, Offering offering, User user, DateTime registDt, CreditCardInfo info)
        {
            //Check if product is subscription product
            if (product is SubscriptionProduct)
            {
                //check if there are any recurring products that have the same productgroup
                SubscriptionProduct subscriptionProduct = (SubscriptionProduct)product;

                //Get user's recurring productGroups
                var recurringProductGroups = user.GetRecurringProductGroups(offering);
                if (!recurringProductGroups.Contains(subscriptionProduct.ProductGroup))
                {
                    var productPackage = context.ProductPackages.FirstOrDefault(p => p.ProductId == product.ProductId);
                    if (productPackage != null)
                    {
                        var entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == productPackage.PackageId);
                        if (entitlement != null)
                        {
                            var billing = new CreditCardRecurringBilling()
                            {
                                CreatedOn = registDt,
                                Product = product,
                                User = user,
                                UpdatedOn = registDt,
                                EndDate = entitlement.EndDate,
                                NextRun = entitlement.EndDate.AddDays(-3).Date, // Run day before expiry
                                StatusId = GlobalConfig.Visible,
                                Offering = offering,
                                Package = (Package)productPackage.Package,
                                CreditCardHash = MyUtility.GetSHA1(info.Number),
                                NumberOfAttempts = 0
                            };
                            context.RecurringBillings.Add(billing);
                            context.SaveChanges();
                        }
                    }
                }
            }
        }