Beispiel #1
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);
 }
Beispiel #2
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);
        }