private int AddDispensary(DispensaryModel dispensary, Dispensary entity)
        {
            foreach (string zip in dispensary.ApprovalZipCodes.Split(','))
            {
                string trimmedZip = zip.Trim();
                if (!String.IsNullOrEmpty(trimmedZip))
                {
                    entity.ApprovalZipCodes.Add(HGContext.ZipCodes.Add(new ZipCode()
                    {
                        Code = trimmedZip
                    }));
                }
            }
            foreach (string zip in dispensary.DeliveryZipCodes.Split(','))
            {
                string trimmedZip = zip.Trim();
                if (!String.IsNullOrEmpty(trimmedZip))
                {
                    entity.DeliveryZipCodes.Add(HGContext.ZipCodes.Add(new ZipCode()
                    {
                        Code = trimmedZip
                    }));
                }
            }
            HGContext.Dispensaries.Add(entity);
            var id = HGContext.SaveChanges();

            return(id);
        }
        private void UpadteDispensary(PendingDispensaryModel pendingDispensary)
        {
            var entity = HGContext.PendingDispensaries.SingleOrDefault(d => d.Id == pendingDispensary.Id);


            entity.Id                      = pendingDispensary.Id;
            entity.AddressId               = pendingDispensary.AddressId;
            entity.Email                   = pendingDispensary.Email;
            entity.Name                    = pendingDispensary.Name;
            entity.PhoneNumber             = pendingDispensary.PhoneNumber;
            entity.PendingDispensaryStatus = PendingDispensaryStatus.WaitingForApprove;
            entity.Type                    = pendingDispensary.Type;
            entity.Website                 = pendingDispensary.Website;
            HGContext.Entry(entity).State  = EntityState.Modified;

            var address = HGContext.Addresses.SingleOrDefault(a => a.Id == pendingDispensary.Id);

            if (address != null)
            {
                address.Address1 = pendingDispensary.Address.Address1;
                address.Address2 = pendingDispensary.Address.Address2;
                address.City     = pendingDispensary.Address.City;
                address.State    = pendingDispensary.Address.State;
                address.Zip      = pendingDispensary.Address.Zip;
                address.Country  = pendingDispensary.Address.Country;
                HGContext.Entry(address).State = EntityState.Modified;
            }

            HGContext.SaveChanges();
        }
        public void Delete([FromUri] int id)
        {
            Dispensary dispensary = HGContext.Dispensaries.Include(d => d.DispensaryProducts).FirstOrDefault(d => d.Id == id);

            foreach (DispensaryProduct product in dispensary.DispensaryProducts)
            {
                product.IsDeleted = true;
            }
            dispensary.IsDeleted = true;

            HGContext.SaveChanges();
        }
Example #4
0
        public IHttpActionResult EditCredits([FromBody] EditCredit credit)
        {
            var wallet = HGContext.Users.Where(u => u.Id == credit.userId).Select(u => u.Wallet).FirstOrDefault();

            if (wallet != null)
            {
                wallet.Credit = credit.amount;
            }
            else
            {
                return(NotFound());
            }

            HGContext.SaveChanges();

            return(Ok());
        }
Example #5
0
        public async Task <IHttpActionResult> Fund([FromBody] FundWalletBindingModel model)
        {
            StripeService stripe   = new StripeService();
            var           response = await stripe.ChargeCustomer(model.StripeToken, model.TransactionAmount);

            if (response)
            {
                var user = await HGContext.Users.FirstOrDefaultAsync(x => x.Id == model.UserId);

                var wallet = await HGContext.Wallets.FirstOrDefaultAsync(x => x.Id == model.WalletId);

                if (wallet != null && user.WalletId == wallet.Id)
                {
                    wallet.Transactions.Add(new Transaction()
                    {
                        TransactedAt      = DateTimeOffset.UtcNow,
                        TransactionAmount = model.TransactionAmount / 100M,
                        TransactionCharge = 0M,
                        TransactionStatus = TransactionStatusEnum.CLEARED,
                        TransactionType   = TransactionTypeEnum.DEPOSIT
                    });

                    var result = HGContext.SaveChanges();
                    if (result == 0)
                    {
                        return(BadRequest("Something went wrong"));
                    }
                    return(Ok());
                }
                return(BadRequest("Something went wrong"));
            }
            else
            {
                _logger.ErrorFormat("Failed to finalize payment user {0} amount {1}", model.UserId, model.TransactionAmount);
                return(BadRequest("Something went wrong"));
            }
            //return Ok(response);
        }
Example #6
0
        public IHttpActionResult UpdateUserRoles(UserWithRolesModel user)
        {
            User userInfo = HGContext.Users.Include(u => u.Roles).FirstOrDefault(u => u.Id == user.Id);

            if (userInfo == null)
            {
                return(NotFound());
            }

            var ids = user.RolesList.Select(r => r.Id).ToList();

            userInfo.Roles.Clear();

            foreach (int id in ids)
            {
                userInfo.Roles.Add(new UserRole()
                {
                    UserId = user.Id, RoleId = id
                });
            }
            HGContext.SaveChanges();

            return(Ok());
        }
Example #7
0
        public async Task <HttpResponseMessage> Checkout([FromBody] CheckoutBindingModel model)
        {
            var user = await HGContext.Users.FirstOrDefaultAsync(x => x.Id == model.UserId);

            var cart = await HGContext.Orders.FirstOrDefaultAsync(x => x.Id == model.OrderId);

            if (model.TransactionAmount > 0)
            {
                StripeService stripe  = new StripeService();
                var           success = await stripe.ChargeCustomer(model.StripeToken, model.TransactionAmount);

                if (!success)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Payment has been failed"));
                }

                var wallet = await HGContext.Wallets.FirstOrDefaultAsync(x => x.Id == model.WalletId);

                if (wallet != null && user.WalletId == wallet.Id)
                {
                    wallet.Transactions.Add(new Transaction()
                    {
                        TransactedAt      = DateTimeOffset.UtcNow,
                        TransactionAmount = model.TransactionAmount / 100M,
                        TransactionCharge = 0M,
                        TransactionStatus = TransactionStatusEnum.CLEARED,
                        TransactionType   = TransactionTypeEnum.DEPOSIT
                    });
                }
            }

            cart.TotalPrice = 0;
            foreach (var v in cart.DispensaryProductVariantOrders)
            {
                cart.TotalPrice = cart.TotalPrice + v.Quantity * v.UnitPrice;
            }
            if (model.DeliveryTypeId == DeliveryTypeEnumModel.DELIVERY &&
                (model.DeliveryAddress == null || string.IsNullOrWhiteSpace(model.DeliveryAddress.FormattedAddress)))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid Address"));
            }
            if (model.DeliveryTypeId == DeliveryTypeEnumModel.DELIVERY)
            {
                cart.DeliveryType = DeliveryTypeEnum.DELIVERY;
                if (cart.DeliveryAddress == null)
                {
                    cart.DeliveryAddress = new Address();
                }
                if (user.DeliveryAddress == null)
                {
                    user.DeliveryAddress = new Address();
                }
                cart.DeliveryAddress.Address1         = model.DeliveryAddress.Address1;
                cart.DeliveryAddress.Address2         = model.DeliveryAddress.Address2;
                cart.DeliveryAddress.City             = model.DeliveryAddress.City;
                cart.DeliveryAddress.State            = model.DeliveryAddress.State;
                cart.DeliveryAddress.Zip              = model.DeliveryAddress.Zip;
                cart.DeliveryAddress.Country          = "USA";
                cart.DeliveryAddress.FormattedAddress = model.DeliveryAddress.FormattedAddress;
                cart.DeliveryAddress.Latitude         = model.DeliveryAddress.Latitude;
                cart.DeliveryAddress.Longitude        = model.DeliveryAddress.Longitude;
                //user address
                user.DeliveryAddress.Address1         = model.DeliveryAddress.Address1;
                user.DeliveryAddress.Address2         = model.DeliveryAddress.Address2;
                user.DeliveryAddress.City             = model.DeliveryAddress.City;
                user.DeliveryAddress.State            = model.DeliveryAddress.State;
                user.DeliveryAddress.Zip              = model.DeliveryAddress.Zip;
                user.DeliveryAddress.Country          = "USA";
                user.DeliveryAddress.FormattedAddress = model.DeliveryAddress.FormattedAddress;
                user.DeliveryAddress.Latitude         = model.DeliveryAddress.Latitude;
                user.DeliveryAddress.Longitude        = model.DeliveryAddress.Longitude;
                cart.DeliveryCharge = 0;

                // Do Onfleet Integration here

                var onfleet = new OnfleetService();
                OnfleetRecipient recipient = null;
                if (string.IsNullOrWhiteSpace(user.OnfleetRecipientId))
                {
                    recipient = onfleet.FindRecipientByPhone(user.PhoneNumber);
                    if (recipient == null)
                    {
                        recipient = onfleet.CreateRecipient(user.FirstName + " " + user.LastName, user.PhoneNumber,
                                                            "", true, true);
                    }
                    if (recipient == null)
                    {
                        _logger.ErrorFormat("Unable to create onfleet recipient for order");
                        return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                      "Unable to process delivery orders at this time."));
                    }
                    user.OnfleetRecipientId = recipient.id;
                }

                //var pickupdestination = onfleet.CreateDestination(Mapper.Map<AddressModel>(cart.Dispensary.Address));
                //if (pickupdestination == null)
                //{
                //  return Request.CreateResponse(HttpStatusCode.BadRequest, "Unable to process delivery orders at this time.");
                //}
                var destination = onfleet.CreateDestination(Mapper.Map <AddressModel>(cart.DeliveryAddress));
                if (destination == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  "Unable to process delivery orders at this time."));
                }
                cart.OnfleetDestinationId = destination.id;
                var recipientid = user.OnfleetRecipientId ?? recipient.id;
                var task        = onfleet.CreateTask(onfleet.GetOnfleetOrganization(), destination, recipientid, "", false,
                                                     null,
                                                     false);
                if (task == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  "Unable to process delivery orders at this time."));
                }
                cart.OnfleetTaskId      = task.id;
                cart.OnfleetTrackingURL = task.trackingURL;
            }
            else
            {
                cart.DeliveryCharge = 0;
                cart.DeliveryType   = DeliveryTypeEnum.PICKUP;
            }

            cart.DispensaryId =
                cart.DispensaryProductVariantOrders.FirstOrDefault()
                .DispensaryProductVariant.DispensaryProduct.DispensaryId;


            if (model.PaymentTypeId == PaymentTypeEnumModel.CASH)
            {
                cart.PaymentType = PaymentTypeEnum.CASH;
            }
            else
            {
                var total = cart.TotalPrice;
                if (model.UseCredits)
                {
                    if (total >= user.Wallet.Credit)
                    {
                        total -= user.Wallet.Credit;
                        user.Wallet.Credit = 0;
                    }
                    else
                    {
                        user.Wallet.Credit -= total;
                        total = 0;
                    }
                }

                if (total - model.TransactionAmount != 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry, we have a billing error. Contact support, please."));
                }


                cart.PaymentType = PaymentTypeEnum.CREDIT;
                user.Wallet.Transactions.Add(new Transaction()
                {
                    TransactionAmount = total
                });
            }

            cart.CheckedOutAt = DateTimeOffset.UtcNow;
            cart.IsCheckedOut = true;

            var result = HGContext.SaveChanges();

            if (result == 0)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Unable to process order at this time"));
            }
            //var sendgrind = new SendGridEmailService();
            //if (cart.DeliveryType == DeliveryTypeEnum.DELIVERY)
            //{
            //    sendgrind.SendEmailAsync(user.Email,
            //        String.Format("AskMJane Order  #{0} Confirmation", cart.Id),
            //        "Your order has been confirmed and will be on it's way soon.");
            //}
            //else
            //{
            //    sendgrind.SendEmailAsync(user.Email,
            //        String.Format("AskMJane Order #{0} Confirmation", cart.Id),
            //        String.Format(
            //            "Your order has been confirmed and will be waiting for you at {0}\r\n{1}\r\n{2}, {3} {4}",
            //            cart.Dispensary.Name, cart.Dispensary.Address.Address1, cart.Dispensary.Address.City,
            //            cart.Dispensary.Address.State, cart.Dispensary.Address.Zip));
            //}
            return(Request.CreateResponse(HttpStatusCode.OK));
        }