Get() public method

public Get ( string customerId, Stripe.StripeRequestOptions requestOptions = null ) : Stripe.StripeCustomer
customerId string
requestOptions Stripe.StripeRequestOptions
return Stripe.StripeCustomer
        SubscriptionDetails ISubscriptionService.GetCustomerSubscription(string customerBillingId)
        {
            SubscriptionDetails subscription = null;

            try
            {
                var customerService = new StripeCustomerService();
                StripeCustomer stripeCustomer = customerService.Get(customerBillingId);
                Coupon coupon = null;
                Discount discount = null;
                if (stripeCustomer.StripeDiscount != null && stripeCustomer.StripeDiscount.StripeCoupon != null)
                {
                    coupon = new Coupon
                    {
                        AmountOff = stripeCustomer.StripeDiscount.StripeCoupon.AmountOff,
                        Duration = stripeCustomer.StripeDiscount.StripeCoupon.Duration,
                        DurationInMonths = stripeCustomer.StripeDiscount.StripeCoupon.DurationInMonths,
                        Id = stripeCustomer.StripeDiscount.StripeCoupon.Id,
                        PercentOff = stripeCustomer.StripeDiscount.StripeCoupon.PercentOff,
                        RedeemBy = stripeCustomer.StripeDiscount.StripeCoupon.RedeemBy
                    };
                    discount = new Discount
                    {
                        Coupon = coupon,
                        End = stripeCustomer.StripeDiscount.End,
                        Id = stripeCustomer.StripeDiscount.Id,
                        Start = stripeCustomer.StripeDiscount.Start
                    };
                }

                subscription = new SubscriptionDetails
                {
                    Created = stripeCustomer.Created,
                    Deleted = stripeCustomer.Deleted,
                    CanceledAt = stripeCustomer.StripeSubscription != null? stripeCustomer.StripeSubscription.CanceledAt : null,
                    CustomerId = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.CustomerId: null,
                    EndedAt = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.EndedAt: null,
                    PeriodEnd = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.PeriodEnd: null,
                    PeriodStart = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.PeriodStart: null,
                    Start = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.Start: null,
                    TrialEnd = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.TrialEnd: null,
                    TrialStart = stripeCustomer.StripeSubscription != null?stripeCustomer.StripeSubscription.TrialStart: null,
                    Status = stripeCustomer.StripeSubscription != null? stripeCustomer.StripeSubscription.Status: null,
                    Plan = stripeCustomer.StripeSubscription != null ? stripeCustomer.StripeSubscription.StripePlan.Name : null,
                    Discount = discount
                };
            }
            catch { }

            return subscription;
        }
        public async Task<ActionResult> CancelSubscription()
        {          
            await Task.Run(() =>
            {
                var hasSubscription = getCustomer().HasSubscription;
                if(hasSubscription == true)
                {
                    var uStripeCustomer = getCustomer();
                    var stripeCustomerID = uStripeCustomer.StripeCustomerID;
                    var subscriptionID = uStripeCustomer.StripeSubscriptionID;                    
                  
                    if (uStripeCustomer.SubscriptionType == "ultimate")
                    {
                        // cancel subscription in stripe.com
                        var subscriptionService = new StripeSubscriptionService();
                        subscriptionService.Cancel(stripeCustomerID, subscriptionID);

                        // delete stripe customer as well
                        var customerService = new StripeCustomerService();
                        StripeCustomer customerToDelete = customerService.Get(stripeCustomerID);
                        customerService.Delete(customerToDelete.Id);

                        db.StripeCustomers.Remove(uStripeCustomer); //db
                    }
                    else if (uStripeCustomer.SubscriptionType == "standard")
                    {
                        // cancel subscription in stripe.com
                        var subscriptionService = new StripeSubscriptionService();
                        subscriptionService.Cancel(stripeCustomerID, subscriptionID);

                        // delete stripe customer as well
                        var customerService = new StripeCustomerService();
                        StripeCustomer customerToDelete = customerService.Get(stripeCustomerID);
                        customerService.Delete(customerToDelete.Id);

                        uStripeCustomer.HasSubscription = false; //db
                    }
                    db.SaveChanges();                  
                }
            });
         
            return RedirectToAction("Index", "Home");
        }
        public async Task<ActionResult> DowngradePlan()
        {
            await Task.Run(() =>
            {
                var cust = getCustomer();

                if (cust != null) // downgrade
                {
                    // retrieve customer
                    var customerService = new StripeCustomerService();
                    StripeCustomer currentCustomer = customerService.Get(cust.StripeCustomerID);

                    // change plan
                    StripeSubscriptionService subService = new StripeSubscriptionService();
                    StripeSubscriptionUpdateOptions updateOptions = new StripeSubscriptionUpdateOptions();
                    updateOptions.PlanId = "standard";
                    updateOptions.TrialEnd = currentTime; // no free days allowed when downgrading

                    var myUpdateSuscription = subService.Update(currentCustomer.Id, cust.StripeSubscriptionID, updateOptions);

                    // record changes in db
                    StripeCustomers newRecord = new StripeCustomers();

                    newRecord.StripeCustomerID = myUpdateSuscription.CustomerId;
                    newRecord.CustomerName = User.Identity.Name;
                    newRecord.StripeSubscriptionID = cust.StripeSubscriptionID;
                    newRecord.SubscriptionType = "standard";
                    newRecord.TrialValidUntil = currentTime;
                    newRecord.HasSubscription = true;
                    newRecord.StartDate = currentTime;
                    newRecord.Interval = "Monthly";

                    db.StripeCustomers.Add(newRecord);
                    db.StripeCustomers.Remove(cust);
                    db.SaveChanges();
                }
            });
            return RedirectToAction("Index", "Home");
        }
        private async Task CreateCustomer(StripeModel model)
        {
            await Task.Run(() =>
            {
                // create customer based on validated token from stripe.js 
                var myCustomer = new StripeCustomerCreateOptions();

                // assign token to a credit card option for a user
                myCustomer.Card = new StripeCreditCardOptions()
                {
                    TokenId = model.Token
                };

                myCustomer.Email = User.Identity.Name;
                myCustomer.Description = User.Identity.Name;
                myCustomer.PlanId = model.SubscriptionType.ToString();
                myCustomer.Quantity = 1;
                
                // create customer in stripe service
                var customerService = new StripeCustomerService();
                StripeCustomer stripeCustomer = customerService.Create(myCustomer);

                // get subscription Id from created user
                var subscriptionID = stripeCustomer.StripeSubscriptionList.StripeSubscriptions.FirstOrDefault().Id;              
                       
             // save credit card optional details 
                StripeCustomerService customerServic = new StripeCustomerService();
                stripeCustomer = customerService.Get(stripeCustomer.Id);
                var cardId = stripeCustomer.StripeDefaultCardId; // get card id
                var myCard = new StripeCardUpdateOptions();

                myCard.Name = model.CardHolderName;
                myCard.AddressLine1 = model.AddressLine1;
                myCard.AddressLine2 = model.AddressLine2;
                myCard.AddressCity = model.AddressCity;
                myCard.AddressZip = model.AddressPostcode;
                myCard.AddressCountry = model.AddressCountry;

                var cardService = new StripeCardService();
                StripeCard stripeCard = cardService.Update(stripeCustomer.Id, cardId, myCard);
            //........................
                
                // record customer in database
                var cust = getCustomer();
                if(cust == null) // new users
                {
                    // get values to create a new record in StripeCustomer table
                    StripeCustomers customer = new StripeCustomers();
                
                    customer.CustomerName = User.Identity.Name;
                    customer.StripeCustomerID = stripeCustomer.Id;
                    customer.StripeSubscriptionID = subscriptionID;
                    customer.SubscriptionType = model.SubscriptionType.ToString();
                    customer.HasSubscription = true;
                    customer.Interval = "Monthly";
                    customer.StartDate = TimeConverter.ConvertToLocalTime(DateTime.Now, "GMT Standard Time");
                    if (model.SubscriptionType.ToString() == "standard")
                    {
                        customer.TrialValidUntil = currentTime.AddDays(30);
                    }
                    db.StripeCustomers.Add(customer);
                    }
                    else // user with db records
                    {
                        StripeCustomers newRecord = new StripeCustomers();

                        // take the data from current user
                        newRecord.StripeCustomerID = stripeCustomer.Id;
                        newRecord.CustomerName = User.Identity.Name;
                        newRecord.StripeSubscriptionID = subscriptionID;
                        newRecord.SubscriptionType = model.SubscriptionType.ToString();
                        newRecord.HasSubscription = true;
                        newRecord.Interval = "Monthly";
                        newRecord.StartDate = currentTime;
                        if (model.SubscriptionType.ToString() == "standard")
                        {
                            newRecord.TrialValidUntil = currentTime.AddDays(30);
                        }
                        db.StripeCustomers.Add(newRecord);

                        // delete customer's old record in database
                        db.StripeCustomers.Remove(cust);
                    }
                db.SaveChanges();
            });
        }
Example #5
0
 /// <summary>
 /// Retrieves the StripeCustomer associated with the given IStripeUser instance
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public static StripeCustomer RetrieveCustomer(IStripeUser user)
 {
     var customerService = new StripeCustomerService();
     StripeCustomer stripeCustomer = customerService.Get(user.PaymentSystemId);
     return stripeCustomer;
 }
        public ActionResult AccountOptions()
        {
            TempData["SelectedMenu"] = "Account";
            TempData["SelectedSubMenu"] = "Options";
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];

            if (userObj.PrimaryUser == false)
                return RedirectToAction("Index", "Admin");

            AccountInfomationViewModel accountinfor = new AccountInfomationViewModel();

            //get time zone
            System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> tz;
            tz = TimeZoneInfo.GetSystemTimeZones();
            List<TimeZoneViewModel> tzList = new List<TimeZoneViewModel>();

            foreach (var t in tz)
            {
                TimeZoneViewModel tzvmodel = new TimeZoneViewModel();
                tzvmodel.DisplayName = t.DisplayName;
                tzvmodel.ID = t.Id;
                tzList.Add(tzvmodel);
            }

            accountinfor.TimeZoneList = tzList;

            if (userObj != null)
            {
                var account = accountRepo.Accounts.Where(aid => aid.ID == userObj.AccountID).FirstOrDefault();
                accountinfor.AccountName = account.AccountName;
                accountinfor.ContactName = userObj.FullName;
                accountinfor.EmailAddress = userObj.Email;
                accountinfor.TimeZone = account.TimeZone;
                accountinfor.BillingAddress = account.BusinessAddress;

                //get numberof connection
                int planid = account.PlanID;
                int planLevel = planid * 10;

                //get billing plan
                var billingPlan = planRepository.Plans.Where(pid => pid.ID == planid).FirstOrDefault();
                accountinfor.BillingPlan = billingPlan.Name;

                // get saved quantity
                var accDetails = accountRepo.Accounts.Where(aguid => aguid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                var planLeval = planRepository.Plans.Where(pid => pid.ID == accDetails.PlanID).FirstOrDefault().PlanLevel;

                var featureQuality = featureRepository.Features.Where(pid => pid.PlanLevel == planLeval & pid.Type == "Max Items per Folder").FirstOrDefault();
                    var savedQuality = purchRepository.Purchases.Where(fid => fid.FeatureID == featureQuality.ID && fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();

                    if (savedQuality != null)
                    {
                        var quantitySaved = (savedQuality.Quantity) / (featureQuality.Quantity);
                        accountinfor.NoOfConnection = quantitySaved * 5;

                    }
                    else
                    {
                        accountinfor.NoOfConnection = 0;
                    }

                accountinfor.ListofUsers = new List<Domain.Entities.User>();
                accountinfor.ListofUsers = userRepository.Users.Where(u => u.AccountID == userObj.AccountID).ToList();

                //var errorLogsObj = CCErrorLogRepository.CCErrorLogs.Where(guid => guid.AccountGUID == accountObj.AccountGUID & (guid.ErrorType == "Exchange Connection" | guid.ErrorType == "Sync")).Take(100).ToList();
                accountinfor.ErrorLogList = new List<CCErrorLog>();
                //accountinfor.ErrorLogList = errorLogsObj;

                var plans = planRepository.Plans.ToList();
                // plans.RemoveAll(pname => pname.Name == "Free");
                accountinfor.Plans = plans;

                // get selected plan
                var planID = accountRepo.Accounts.FirstOrDefault(aguid => aguid.AccountGUID == accountObj.AccountGUID).PlanID;
                accountinfor.PlanID = planID;

                // get selected plan details
                var selectedPlanDetails = plans.FirstOrDefault(pid => pid.ID == planID);
                selectedPlanDetails.Price = selectedPlanDetails.Price;
                //packageViewModel.SelectedPlanDetails = selectedPlanDetails;

                // get card details from stripe
                var stripeCustomerID = accountRepo.Accounts.FirstOrDefault(aguid => aguid.AccountGUID == accountObj.AccountGUID).StripeCustomerID;

                if (stripeCustomerID != null)
                {
                    // default card details
                    var customer = new StripeCustomerService();
               //         customer.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";
                    var cusRes = customer.Get(stripeCustomerID);
                    var defaultCardID = cusRes.StripeDefaultCardId;
                    //packageViewModel.DefaultCardID = defaultCardID;

                    // get saved quantity
                    featureQuality = featureRepository.Features.FirstOrDefault(pid => pid.PlanLevel == selectedPlanDetails.PlanLevel & pid.Type == "Max Items per Folder");
                    savedQuality = purchRepository.Purchases.FirstOrDefault(fid => fid.FeatureID == featureQuality.ID && fid.AccountGUID == accountObj.AccountGUID);

                    if (savedQuality != null)
                    {
                        var quantitySaved = (savedQuality.Quantity) / (featureQuality.Quantity);
                        accountinfor.QuantitySaved = quantitySaved;

                    }
                    else
                    {
                        accountinfor.QuantitySaved = 1;
                    }

                    var cards = new StripeCardService();
                //    cards.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";
                    var objCard = cards.List(stripeCustomerID, null);
                    List<CardViewModel> allCardDetails = new List<CardViewModel>();

                    foreach (var card in objCard)
                    {
                        CardViewModel objcard = new CardViewModel();
                        objcard.CardID = card.Id;
                        objcard.Name = card.Name;
                        objcard.Number = "**** " + card.Last4;
                        objcard.Type = card.Brand;
                        objcard.ExpireDate = card.ExpirationYear + "/" + card.ExpirationMonth;

                        allCardDetails.Add(objcard);
                    }
                    //packageViewModel.CardDetails = allCardDetails;
                }
                else
                {
                    //packageViewModel.CardDetails = null;
                }

            }

            return View(accountinfor);
        }
Example #7
0
        public GetCustomerResponse GetCustomerInformation(string customerId)
        {
            var response = new GetCustomerResponse();

            var customerService = new StripeCustomerService();
            try
            {
                StripeCustomer stripeCustomer = customerService.Get(customerId);
                MapStripeCustomerTo(stripeCustomer, response);
                response.Success = true;
            }
            catch (StripeException ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return response;
        }
        public void ChargeFromExistingDetails(int existingPlanID, int quantities, string couponID)
        {
            // get selected plan
            User userObj = (User)Session["user"];
            var planID = accountRepo.Accounts.FirstOrDefault(aid => aid.ID == userObj.AccountID).PlanID;

            // get selected plan details
            Plan selectedPlanName = new Plan();
            selectedPlanName = planRepository.Plans.FirstOrDefault(pid => pid.ID == planID);

            // get Existing plan name
            var existingPlan = planRepository.Plans.FirstOrDefault(pid => pid.ID == existingPlanID);

            var customerService = new StripeCustomerService();
            customerService.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";

            var currentAccountDetails = accountRepo.Accounts.FirstOrDefault(aid => aid.ID == userObj.AccountID);
            if (currentAccountDetails.StripeCustomerID != null)
            {
                //update subscription
                var subscribeDetails = customerService.Get(currentAccountDetails.StripeCustomerID);
                var allCurrentSubscriptions = subscribeDetails.StripeSubscriptionList.StripeSubscriptions.Where(sid => sid.CustomerId == currentAccountDetails.StripeCustomerID).ToList();
                var subID = "";
                try
                {
                    subID = subscribeDetails.StripeSubscriptionList.StripeSubscriptions.FirstOrDefault(sid => sid.CustomerId == currentAccountDetails.StripeCustomerID && sid.StripePlan.Name == existingPlan.Name).Id;
                }
                catch(Exception ex)
                {
                    subID = subscribeDetails.StripeSubscriptionList.StripeSubscriptions.FirstOrDefault(sid => sid.CustomerId == currentAccountDetails.StripeCustomerID && sid.StripePlan.Name == "Empty Plan").Id;
                }

                var subs = new StripeSubscriptionService();
                subs.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";
                if (couponID != "")
                {
                    var updateRes = subs.Update(currentAccountDetails.StripeCustomerID, subID, new StripeSubscriptionUpdateOptions() { PlanId = selectedPlanName.Name, Quantity = quantities, CouponId = couponID });
                }
                else
                {
                    var updateRes = subs.Update(currentAccountDetails.StripeCustomerID, subID, new StripeSubscriptionUpdateOptions() { PlanId = selectedPlanName.Name, Quantity = quantities});
                }

            }
        }
        public ActionResult BillingOptions(int id)
        {
            TempData["SelectedMenu"] = "Account";
            TempData["SelectedSubMenu"] = "Billing";
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];

            if (userObj.PrimaryUser == false)
                return RedirectToAction("Index", "Admin");

            decimal additionalPrice = 0;
            //TempData["SelectedMenu"] = "Subscription";
            List<List<string>> typeListObj = new List<List<string>>();

            PackageViewModel packageViewModel = new PackageViewModel();

            //trial Data
            if (Session["trialData"] != null)
            {
                TrialDataModel trialObj = (TrialDataModel)Session["trialData"];
                packageViewModel.trialObj = trialObj;
            }

            //get all plans
            var plans = planRepository.Plans.ToList();
            // plans.RemoveAll(pname => pname.Name == "Free");
            packageViewModel.Plans = plans;

            // get all types
            //var types = featureRepository.Features.Where(p => p.Price == 0).Select(f => f.Type).Distinct().ToList();
            List<string> types = new List<string>();
            types.Add("Sync Contacts");
            types.Add("Sync Calendar");
            types.Add("Sync Period");
            //types.Add("Contacts Sync");
            //types.Add("Calendar Sync");
            //types.Add("Connections");
            types.Add("Max Items per Folder");
            types.Add("Fields Available");
            types.Add("History Tracking");
            types.Add("Third Party Connections");
            packageViewModel.Types = types;

            // get selected plan
            var planID = accountRepo.Accounts.FirstOrDefault(aguid => aguid.AccountGUID == accountObj.AccountGUID).PlanID;
            packageViewModel.PlanID = planID;

            // get selected plan details
            var selectedPlanDetails = plans.FirstOrDefault(pid => pid.ID == planID);
            selectedPlanDetails.Price = selectedPlanDetails.Price;
            packageViewModel.SelectedPlanDetails = selectedPlanDetails;

            // run all plans
            foreach (var pln in plans)
            {

                var allFeatures = featureRepository.Features.Where(pl => pl.PlanLevel == pln.PlanLevel).ToList();

                List<string> typesList = new List<string>();

                foreach (var typ in types)
                {

                    var res = allFeatures.FirstOrDefault(t => t.Type == typ);

                    if (res != null)
                    {
                        typesList.Add(res.Name);
                    }
                    else
                    {
                        typesList.Add("-");
                    }

                }

                typeListObj.Add(typesList);

            }

            packageViewModel.Names = typeListObj;

            // get card details from stripe
            var stripeCustomerID = accountRepo.Accounts.FirstOrDefault(aguid => aguid.AccountGUID == accountObj.AccountGUID).StripeCustomerID;

            if (stripeCustomerID != null)
            {
                // default card details
                var customer = new StripeCustomerService();
                customer.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";
                var cusRes = customer.Get(stripeCustomerID);
                var defaultCardID = cusRes.StripeDefaultCardId;
                packageViewModel.DefaultCardID = defaultCardID;

                // get saved quantity
                var featureQuality = featureRepository.Features.FirstOrDefault(pid => pid.PlanLevel == selectedPlanDetails.PlanLevel & pid.Type == "Max Items per Folder");
                var savedQuality = purchRepository.Purchases.FirstOrDefault(fid => fid.FeatureID == featureQuality.ID && fid.AccountGUID == accountObj.AccountGUID);

                if (savedQuality != null)
                {
                    var quantitySaved = (savedQuality.Quantity) / (featureQuality.Quantity);
                    packageViewModel.QuantitySaved = quantitySaved;

                }
                else
                {
                    packageViewModel.QuantitySaved = 1;
                }

                var cards = new StripeCardService();
                cards.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";
                var objCard = cards.List(stripeCustomerID, null);
                List<CardViewModel> allCardDetails = new List<CardViewModel>();

                foreach (var card in objCard)
                {
                    CardViewModel objcard = new CardViewModel();
                    objcard.CardID = card.Id;
                    objcard.Name = card.Name;
                    objcard.Number = "**** " + card.Last4;
                    objcard.Type = card.Brand;
                    objcard.ExpireDate = card.ExpirationYear + "/" + card.ExpirationMonth;

                    allCardDetails.Add(objcard);
                }
                packageViewModel.CardDetails = allCardDetails;
            }

            else
            {
                packageViewModel.CardDetails = null;
            }

            // End get card details

            if (id == 2)
            {
                ViewBag.MessagePass = "******";
            }
            else
            {
                ViewBag.MessagePass = "";
            }

            return View(packageViewModel);
        }