Exemplo n.º 1
0
        //CHARGE EXISTING CUSTOMERS
        public List <ChargeExistingUser> GetUsersToCharge()
        {
            List <ChargeExistingUser> existingUserList = null;

            DataProvider.ExecuteCmd("dbo.UserSubscriptions_SelectCustomersToBill",
                                    inputParamMapper: null,
                                    singleRecordMapper: (IDataReader reader, short resultSet) =>
            {
                if (existingUserList == null)
                {
                    existingUserList = new List <ChargeExistingUser>();
                }

                existingUserList.Add(DataMapper <ChargeExistingUser> .Instance.MapToObject(reader));
            });

            if (existingUserList != null)
            {
                ChargeExistingUser model = null;

                foreach (ChargeExistingUser existingUser in existingUserList.ToList())
                {
                    try
                    {
                        model = existingUser;
                        TransactionsAddRequest transaction = ChargeExistingCustomer(existingUser);
                        InsertTransaction(transaction);
                        DateTime _newBillDate = existingUser.NextBillingDate.AddMonths(existingUser.DurationMonths);
                        existingUser.NextBillingDate = _newBillDate;
                    }
                    catch (Exception ex)
                    {
                        TransactionFailAddRequest failedTransaction = new TransactionFailAddRequest
                        {
                            UserBaseId      = model.UserBaseId,
                            CustomerId      = model.CustomerId,
                            Amount          = model.Price,
                            Currency        = model.Currency,
                            Name            = model.CustomerName,
                            Email           = model.CustomerEmail,
                            PlanId          = model.PlanId,
                            DurationTypeId  = model.DurationTypeId,
                            DiscountPercent = model.DiscountPercent,
                            ErrorMessage    = ex.Message
                        };

                        //will lock UserBaseId when transaction failure is inserted
                        this.InsertTransactionFail(failedTransaction);
                        //will deactivate user once transaction fails
                        //this.UpdateUserIsActive(failedTransaction);

                        //create Token/GUID on AppToken table with the UserBaseId
                        ItemResponse <string> Token = new ItemResponse <string> {
                            Item = this.CreateTokenForFailedTransactions(failedTransaction)
                        };

                        Email eml = new Email();

                        MessageAddress msgAdd = new MessageAddress
                        {
                            Email = "*****@*****.**",
                            Name  = model.CustomerName
                        };

                        List <MessageAddress> list = new List <MessageAddress>
                        {
                            msgAdd
                        };

                        eml.To          = list;
                        eml.FromAddress = "*****@*****.**";
                        eml.FromName    = "Eleveight";
                        eml.Subject     = "Subscription Charge Failure";
                        eml.HtmlBody    = _emailTemplateService.CreateFailedPayments(new EmailPaymentFailTemplateInput
                        {
                            CustomerName = msgAdd.Name,
                            Token        = Token.Item,
                            ExtraInfo    = "Your subscription has been deactivated due to failed recurring charges. " +
                                           "Please call Eleveight customer service to reactivate your subscription"
                        });

                        _emailMessenger.SendMail(eml);

                        //remove from list if transaction failed
                        existingUserList.Remove(existingUser);
                    }
                }
                if (existingUserList.Count != 0)
                {
                    UpdateMultipleCustomerSubscription(existingUserList);
                }
            }

            return(existingUserList);
        }