Beispiel #1
0
        public static void RenewPlan()
        {
            int UserId = 0;

            try
            {
                var subs = ServicesData.GetSubscribers();
                int plan;
                if (subs.Rows.Count > 0)
                {
                    foreach (DataRow row in subs.Rows)
                    {
                        String msg        = "";
                        String CustomerId = row["stp_custId"].ToString();
                        String CardId     = row["stp_cardId"].ToString();
                        UserId = Convert.ToInt32(row["MppUserID"]);

                        if (!String.IsNullOrWhiteSpace(CustomerId) && !String.IsNullOrWhiteSpace(CardId))
                        {
                            var profile = APIData.GetUserProfileForSkuData(UserId);

                            if (profile.Rows.Count > 0)
                            {
                                DataRow  dr            = profile.Rows[0];
                                String   ProfileId     = Convert.ToString(dr["ProfileId"]);
                                DateTime LastUpdatedOn = Convert.ToDateTime(dr["AccessTokenUpdatedOn"]);

                                var Auth = new AuthorizationModel()
                                {
                                    access_token  = Convert.ToString(dr["AccessToken"]),
                                    refresh_token = Convert.ToString(dr["RefreshToken"]),
                                    token_type    = Convert.ToString(dr["TokenType"]),
                                    expires_in    = Convert.ToInt32(dr["TokenExpiresIn"])
                                };
                                var reportApi = new ReportsAPI();
                                var skuCount  = AccountData.UserSkuCount(UserId);
                                if (String.IsNullOrWhiteSpace(msg))
                                {
                                    if (skuCount > 250)
                                    {
                                        plan = GetCustomPlan(skuCount);
                                    }
                                    else
                                    {
                                        plan = GetPlanBySku(skuCount);
                                    }
                                    var subsPlan   = new StripeSubscription();
                                    var subService = StripeHelper.GetSubscription(CustomerId);
                                    if (subService != null)
                                    {
                                        subsPlan = StripeHelper.UpdateSubscription(CustomerId, subService.Id, new Stripe.StripeSubscriptionUpdateOptions {
                                            PlanId = plan.ToString(), Prorate = false
                                        }, out msg);
                                    }
                                    else
                                    {
                                        subsPlan = StripeHelper.Subscription(CustomerId, new Stripe.StripeSubscriptionCreateOptions {
                                            PlanId = plan.ToString()
                                        }, out msg);
                                    }
                                    if (String.IsNullOrWhiteSpace(msg))
                                    {
                                        UpdateUserInfo userInfo = new UpdateUserInfo
                                        {
                                            Amount        = ((double)(subsPlan.StripePlan.Amount) / 100).ToString(),
                                            CardId        = CardId,
                                            CustId        = CustomerId,
                                            UserId        = UserId,
                                            PlanId        = plan,
                                            EndedAt       = subsPlan.CurrentPeriodEnd.ToString(),
                                            PlanStartDate = subsPlan.CurrentPeriodStart.ToString(),
                                            PlanEndDate   = subsPlan.CurrentPeriodEnd.ToString(),
                                            TrialEndDate  = row["TrailEndDate"].ToString()
                                        };
                                        ServicesData.UpdateStripeData(userInfo);
                                    }
                                    else
                                    {
                                        LogAPI.WriteLog("RenewPlan - " + msg, UserId.ToString());
                                    }
                                }
                                else
                                {
                                    LogAPI.WriteLog("RenewPlan - " + msg, UserId.ToString());
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.WriteLog("RenewPlan - " + ex.Message, UserId.ToString());
            }
        }
Beispiel #2
0
        public static string SubscribePlan(String CustomerId, String CardId)
        {
            int    plan;
            String msg = "";

            try
            {
                Int32    UserId    = SessionData.UserID;
                DateTime Trialdate = SessionData.TrialEndOn;

                if (!String.IsNullOrWhiteSpace(CardId))
                {
                    var profile = APIData.GetUserProfileForSkuData(UserId);
                    if (profile.Rows.Count > 0)
                    {
                        DataRow  dr            = profile.Rows[0];
                        String   ProfileId     = Convert.ToString(dr["ProfileId"]);
                        DateTime LastUpdatedOn = Convert.ToDateTime(dr["AccessTokenUpdatedOn"]);

                        var Auth = new AuthorizationModel()
                        {
                            access_token  = Convert.ToString(dr["AccessToken"]),
                            refresh_token = Convert.ToString(dr["RefreshToken"]),
                            token_type    = Convert.ToString(dr["TokenType"]),
                            expires_in    = Convert.ToInt32(dr["TokenExpiresIn"])
                        };
                        var reportApi = new ReportsAPI();
                        var skuCount  = AccountData.UserSkuCount(UserId);

                        if (String.IsNullOrWhiteSpace(msg))
                        {
                            var subsPlan = new StripeSubscription();
                            if (skuCount > 250)
                            {
                                plan = GetCustomPlan(skuCount);
                            }
                            else
                            {
                                plan = GetPlanBySku(skuCount);
                            }
                            var subService = StripeHelper.GetSubscription(CustomerId);
                            if (subService != null)
                            {
                                subsPlan = StripeHelper.UpdateSubscription(CustomerId, subService.Id, new Stripe.StripeSubscriptionUpdateOptions {
                                    PlanId = plan.ToString(), Prorate = false
                                }, out msg);
                            }
                            else
                            {
                                subsPlan = StripeHelper.Subscription(CustomerId, new Stripe.StripeSubscriptionCreateOptions {
                                    PlanId = plan.ToString()
                                }, out msg);
                            }

                            if (String.IsNullOrWhiteSpace(msg))
                            {
                                UpdateUserInfo userInfo = new UpdateUserInfo
                                {
                                    Amount        = ((double)(subsPlan.StripePlan.Amount) / 100).ToString(),
                                    CardId        = CardId,
                                    CustId        = CustomerId,
                                    UserId        = UserId,
                                    PlanId        = plan,
                                    EndedAt       = subsPlan.CurrentPeriodEnd.ToString(),
                                    PlanStartDate = subsPlan.CurrentPeriodStart.ToString(),
                                    PlanEndDate   = subsPlan.CurrentPeriodEnd.ToString(),
                                    TrialEndDate  = Trialdate.ToString()
                                };
                                msg = AccountData.UpdateStripeData(userInfo);
                                if (msg == "")
                                {
                                    SessionData.PlanID = plan;
                                }
                            }
                            else
                            {
                                LogAPI.WriteLog("SubscribePlan - " + msg, SessionData.SellerName);
                            }
                        }
                    }
                    else
                    {
                        msg = Constant.PROFILE_NOTFOUND;
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogFile.WriteLog("SubscribePlan - " + msg, SessionData.SellerName);
            }
            return(msg);
        }