示例#1
0
        /// <summary>
        ///     Implements <see cref="IStripeService.CreateCreditCard"/>
        /// </summary>
        public MemberCreditCard CreateCreditCard(Member member, string stripeCardToken)
        {
            var newCard = new StripeCardCreateOptions
            {
                Source = new StripeSourceOptions
                {
                    TokenId = stripeCardToken
                    // Note: Adding Object = "card" will result in a generic stripe error
                }
            };

            var cardService = new StripeCardService();

            StripeCard card;

            try
            {
                card = cardService.Create(member.StripeCustomerId, newCard);
            }
            catch (StripeException ex)
            {
                throw HandleStripeException(ex);
            }

            int expiryMonth = int.Parse(card.ExpirationMonth);
            int expiryYear = int.Parse(card.ExpirationYear);

            MemberCreditCard newMemberCard = new MemberCreditCard
            {
                CardholderName = card.Name,
                ExpiryMonth = expiryMonth,
                ExpiryYear = expiryYear,
                Last4Digits = card.Last4,
                StripeCardId = card.Id,
                Member = member,
                MemberId = member.UserId
            };

            return newMemberCard;
        }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CardProvider"/> class.
 /// </summary>
 /// <param name="apiKey">The API key.</param>
 /// <param name="cardDataService">The card data service.</param>
 public CardProvider(string apiKey, ICardDataService cardDataService)
 {
     this._cardDataService = cardDataService;
     this._cardService = new StripeCardService(apiKey);
 }
        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();
            });
        }
        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);
        }
 public StripeBillingCardService(string apiKey, IMapper mapper)
 {
     _service = new StripeCardService(apiKey);
     _mapper = mapper;
 }
        public ActionResult DeleteCard(string id)
        {
            User userObj = (User)Session["user"];
            var stripeCustomerID = accountRepo.Accounts.FirstOrDefault(aid => aid.ID == userObj.AccountID).StripeCustomerID;

            var card = new StripeCardService();
            card.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";
            card.Delete(stripeCustomerID, id);

            //return RedirectToAction("Subscription", "Admin", new { id = 1 });
            return Json("success", JsonRequestBehavior.AllowGet);
        }
        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);
        }
        public ActionResult AddNewCard(string stripeToken)
        {
            bool tokenUsed = false;

            User userObj = (User)Session["user"];
            if (userObj.PrimaryUser == false)
                return RedirectToAction("Index", "Admin");
            var stripeCustomerID = accountRepo.Accounts.FirstOrDefault(aid => aid.ID == userObj.AccountID).StripeCustomerID;

            if (stripeCustomerID == null)
            {
                // create subscription and get subscription id
                var customerService = new StripeCustomerService();
                customerService.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";

                var currentAccountDetails = accountRepo.Accounts.FirstOrDefault(aid => aid.ID == userObj.AccountID);

                var customer = new StripeCustomerCreateOptions();
                customer.PlanId = "Empty Plan";
                customer.Quantity = 1;
                customer.TokenId = stripeToken;
                customer.Email = userObj.Email;

                // Create subscription
                var subscriptionDetails = customerService.Create(customer);
                stripeCustomerID = subscriptionDetails.Id;

                // save StripeCustomerID
                currentAccountDetails.StripeCustomerID = stripeCustomerID;
                accountRepo.SaveAccount(currentAccountDetails);

                tokenUsed = true;
            }
            else
            {
                var card = new StripeCardService();
                card.ApiKey = "sk_test_4Xusc3Meo8gniONh6dDRZvlp";

                var cardOpt = new StripeCardCreateOptions();
                cardOpt.TokenId = stripeToken;

                var res = card.Create(stripeCustomerID, cardOpt);
            }

            return RedirectToAction("BillingOptions", "Admin", new { id = 1 });
        }