public CheckoutSubmitModel AddCartToMyWallet(string providerUserKey)
        {
            CheckoutSubmitModel model = new CheckoutSubmitModel();

            using (var context = new greenMoneyEntities())
            {
                var user = context.Users1.Find(new Guid(providerUserKey));

                var items = user.CartItems.ToList();
                long total = items.Sum(i => i.Cost);
                decimal dollartotal = items.Sum(i => i.DollarCost);

                // User don't have any items in cart
                if (!items.Any())
                {
                    model.CheckoutSubmitModelState = CheckoutSubmitModelState.NoItemsFound;
                    return model;
                }

                // User has enough points to purchase
                if (user.PointsTotal >= total)
                {
                    var time = DateTime.Now;

                    string description;
                    int? transactionType = null;

                    if (user.CartItems.Count == 1)
                    {
                        description = "Redeem " + user.CartItems.Single().Rewards.PartnerName
                            + " " + user.CartItems.Single().Rewards.Name;
                        transactionType = 4;
                    }
                    else
                    {
                        description = "Voucher redemption x" + user.CartItems.Sum(i => i.Quantity);
                        transactionType = 1;
                    }

                    var purchases = new List<OrderSummaryItemModel>();

                    int costIndex = 1;

                    foreach (var item in items)
                    {
                        int itemidx;
                        if (item.DollarCost == 0)
                        {
                            for (int i = 0; i < item.Quantity; i++)
                            {
                                var voucher = new Vouchers
                                {
                                    Id = Guid.NewGuid(),
                                    Issued = time,
                                    Rewards = item.Rewards,
                                    Users1 = user
                                };

                                context.Vouchers.Add(voucher);

                                voucher.Rewards.Popularity += 1;

                                var ownerUser = context.Users.FirstOrDefault(x => x.UserId == item.Rewards.Owner_Id);

                                purchases.Add(new OrderSummaryItemModel
                                {
                                    VoucherId = voucher.Id,
                                    Name = item.Rewards.Name,
                                    Points = item.Cost,
                                    Quantity = item.Quantity,
                                    DollarCost = item.DollarCost,
                                    PayPalIndex = 0,
                                    AttachVoucherUrl = true,
                                    NotifyOnRedeem = item.Rewards.NotifyOnRedeem,
                                    UserFirstName = user.FirstName,
                                    UserLastName = user.LastName,
                                    PartnerName = item.Rewards.PartnerName,
                                    PartnerOwnerEmail = ownerUser != null ? ownerUser.UserName : null,
                                    Category = item.Rewards.RewardCategories.Name
                                });
                            }
                        }
                        else
                        {
                            itemidx = costIndex++;
                            var ownerUser = context.Users.FirstOrDefault(x => x.UserId == item.Rewards.Owner_Id);

                            purchases.Add(new OrderSummaryItemModel
                            {
                                Name = item.Rewards.Name,
                                Points = item.Cost,
                                Quantity = item.Quantity,
                                DollarCost = item.DollarCost,
                                PayPalIndex = itemidx,
                                AttachVoucherUrl = false,
                                VoucherUrl = string.Empty,
                                NotifyOnRedeem = item.Rewards.NotifyOnRedeem,
                                UserFirstName = user.FirstName,
                                UserLastName = user.LastName,
                                PartnerName = item.Rewards.PartnerName,
                                PartnerOwnerEmail = ownerUser != null ? ownerUser.UserName : null,
                                Category = item.Rewards.RewardCategories.Name
                            });
                        }

                       // Remove from cart items after added to purchases
                       context.CartItems.Remove(item);
                    }

                    var transaction = new Transactions
                    {
                        Addresses = user.Addresses,
                        Users1 = user,
                        Time = time,
                        Description = description,
                        Points = -total,
                        TransactionTypeID = transactionType
                    };
                    context.Transactions.Add(transaction);
                    context.SaveChanges();

                    if (dollartotal > 0)
                    {
                        model.CheckoutSubmitModelState = CheckoutSubmitModelState.SuccessWithProductOrderConfirmation;
                    }
                    else
                    {
                        model.CheckoutSubmitModelState = CheckoutSubmitModelState.SuccessWithOrderConfirmation;
                    }

                    model.Purchases = purchases;

                    return model;

                }
                else // User does NOT have enough points to purchase
                {
                    model.CheckoutSubmitModelState = CheckoutSubmitModelState.NotEnoughPoints;
                    return model;
                }
            }
        }
Esempio n. 2
0
        public static void SendEmailsOnRewardsRedeem(
            string emailContentPath, string emailPathProductOrderConfirmation, string emailPathOrderConfirmation,
            string email, string firstname, string lastname, string url, string voucherUrlBase, CheckoutSubmitModel addToMyWallet)
        {
            foreach (var purchase in addToMyWallet.Purchases)
            {

                if (purchase.NotifyOnRedeem)
                {
                    var recipientEmail = SetRecipientEmail(purchase.PartnerOwnerEmail);
                    string subjectRewardRedeemed = "GreenMoney Reward Redeemed";

                    if (string.IsNullOrWhiteSpace(recipientEmail))
                    {
                        recipientEmail = ConfigurationManager.AppSettings["MasterAdminEmail"];
                        subjectRewardRedeemed = subjectRewardRedeemed + " - no owner email";
                    }

                    var redeem_bodyContent = Razor.Parse(
                        System.IO.File.ReadAllText(emailContentPath),
                        new
                        {
                            Email = email,
                            FirstName = firstname,
                            LastName = lastname,
                            PartnerName = purchase.PartnerName,
                            Name = purchase.Name,
                            Category = purchase.Category,
                            Site = url,
                            VoucherUrl = voucherUrlBase
                        }
                        );

                    // create email request
                    var redeem_request = new SendEmailRequest()
                        .WithDestination(new Destination(new List<string> {recipientEmail}))
                        .WithSource("*****@*****.**")
                        .WithReturnPath("*****@*****.**")
                        .WithMessage(new Message()
                            .WithSubject(new Content(subjectRewardRedeemed))
                            .WithBody(new Body().WithHtml(new Content(redeem_bodyContent)))
                        );

                    try
                    {
                        var redeem_client = new AmazonSimpleEmailServiceClient("AKIAIDP5FFSCJUHHC4QA", "NKAzwbtwwhvKuQZj2t6OXxOhaOEuaBYh3E34Jxbs");
                        redeem_client.SendEmail(redeem_request);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }

            decimal dollartotal = addToMyWallet.Purchases.Sum(i => i.DollarCost);
            long total = addToMyWallet.Purchases.Sum(i => i.Cost);

            string emailFile;
            if (dollartotal > 0)
                emailFile = emailPathProductOrderConfirmation;
            else
                emailFile = emailPathOrderConfirmation;

            // email content
            var bodyContent = Razor.Parse(
                System.IO.File.ReadAllText(emailFile),
                new
                {
                    FirstName = firstname,
                    Time = DateTime.Now,
                    Total = total,
                    Items = addToMyWallet.Purchases,
                    HasVouchers = addToMyWallet.Purchases.Any(i => i.DollarCost == 0),
                    VoucherUrl = voucherUrlBase
                }
            );

            var recipientEmail1 = SetRecipientEmail(email);

            // create email request
            var request = new SendEmailRequest()
                .WithDestination(new Destination(new List<string> { recipientEmail1 }))
                .WithSource("*****@*****.**")
                .WithReturnPath("*****@*****.**")
                .WithMessage(new Message()
                    .WithSubject(new Content("GreenMoney Order Confirmation"))
                    .WithBody(new Body().WithHtml(new Content(bodyContent)))
                );

            try
            {
                var client = new AmazonSimpleEmailServiceClient("AKIAIDP5FFSCJUHHC4QA", "NKAzwbtwwhvKuQZj2t6OXxOhaOEuaBYh3E34Jxbs");
                client.SendEmail(request);
            }
            catch (Exception e)
            {
            }
        }