public static LeagueBilling GetCurrentBillingStatus(Guid leagueId)
 {
     LeagueBilling bi = new LeagueBilling();
     try
     {
         var dc = new ManagementContext();
         PaymentGateway pg = new PaymentGateway();
         var invoice = pg.GetLatestInvoiceSubscriptionForLeagueId(leagueId);
         
         bi.LeagueId = leagueId;
         bi.LeagueName = dc.Leagues.Where(x => x.LeagueId == leagueId).Select(x => x.Name).FirstOrDefault();
         if (invoice != null)
         {
             bi.InvoiceId = invoice.InvoiceId;
             bi.Expires = invoice.Subscription.ValidUntil;
             bi.LastBilledOn = invoice.Subscription.Created;
             bi.MembershipStatus = CurrentStatusOfBillingEnum.League_Membership;
             bi.PaymentProviderCustomerId = invoice.PaymentProviderCustomerId;
         }
         else
         {
             bi.MembershipStatus = CurrentStatusOfBillingEnum.None;
         }
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
     return bi;
 }
        public ActionResult LeagueCancelSubscription(LeagueBilling add)
        {
            var memId = RDN.Library.Classes.Account.User.GetMemberId();

            try
            {
                var bi = RDN.Library.Classes.Billing.Classes.LeagueBilling.GetCurrentBillingStatus(add.LeagueId);

                PaymentGateway pg = new PaymentGateway();
                var f = pg.StartInvoiceWizard().Initalize(ServerConfig.RDNATION_STORE_ID, "USD", PaymentProvider.Stripe, PaymentMode.Live, ChargeTypeEnum.Cancel_Subscription)
                   .SetInvoiceId(bi.InvoiceId)
                        .FinalizeInvoice();

                //clears the member cache to update all cache repos of the league members.
                MemberCache.ClearLeagueMembersCache(add.LeagueId);
                MemberCache.ClearLeagueMembersApiCache(add.LeagueId);
                //succesfully charged.
                if (f.Status == InvoiceStatus.Cancelled)
                    return Redirect(Url.Content("~/billing/league/" + add.LeagueId.ToString().Replace("-", "") + "?u=" + SiteMessagesEnum.sc));
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return Redirect(Url.Content("~/?u=" + SiteMessagesEnum.sww));
        }