public bool CompleteOrder(int approved, string errorcode, string errormessage, CheckoutRequest request, GatewayOrderDetails gatewayOrderDetails)
        {
            var success = false;

            var randomKey     = Guid.NewGuid().ToString("N");
            var privateKey    = ConfigurationManager.AppSettings["AppTestSecretKey"];
            var transactionId = gatewayOrderDetails.reference_id;

            var completeOrderRequest = new CompleteOrderRequest()
            {
                orderid       = request.orderid,
                transactionid = transactionId,
                invoice       = request.invoice,
                errormessage  = errormessage,
                errorcode     = errorcode,
                amount        = request.amounttotal,
                approved      = approved,
                randomkey     = randomKey,
                signature     = Md5Helper.GetMd5Hash(randomKey + privateKey + request.orderid + request.invoice + transactionId)
            };

            try
            {
                var response = _webApiClient.HTTPPostRequest(request.notificationurl, "", completeOrderRequest, null);

                CompleteOrderResponse completeOrderResponse = null;

                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (var streamReader = new StreamReader(response.GetResponseStream()))
                        {
                            var responseText = streamReader.ReadToEnd();
                            completeOrderResponse = JsonConvert.DeserializeObject <CompleteOrderResponse>(responseText);
                        }
                    }
                }

                if (completeOrderResponse != null)
                {
                    if (completeOrderResponse.processed == 1)
                    {
                        _repository.UpdateCheckoutRequestStatus(request.id, Status.Completed);
                        success = true;
                    }
                    else
                    {
                        _repository.UpdateCheckoutRequestStatus(request.id, Status.Failed);
                    }
                }
            }
            catch {
                _repository.UpdateCheckoutRequestStatus(request.id, Status.Failed);
            }

            return(success);
        }
示例#2
0
        public async Task OnGet(int orderId)
        {
            CompleteOrderRequest request = new CompleteOrderRequest
            {
                OrderId = orderId
            };

            CompleteOrderResponse response = await mediator.Send(request);

            ProductName = response.ProductName;
        }
        public async Task Consume(ConsumeContext <CompleteOrderRequest> context)
        {
            var request = context.Message;

            Domain.Order order = await repo.GetOrder(request.OrderId);

            order.Confirm();
            order = await repo.SaveAsync(order);

            var response = new CompleteOrderResponse(true, ResponseAction.Updated);

            await context.RespondAsync(response);
        }
示例#4
0
        public IHttpActionResult Create(OfferViewModel offerViewModel)
        {
            var checkoutVM = new CompleteOrderResponse();

            try
            {
                string username = Utilities.GetUserNameFromRequest(Request);
                var    user     = userService.GetUsers().Where(u => u.UserName == username).FirstOrDefault();
                Post   post     = postService.GetPost(offerViewModel.PostId);
                Offer  offer    = offerService.GetOffer(offerViewModel.OfferId);
                if (post == null || offer == null)
                {
                    return(BadRequest("Not found data"));
                }
                //update create
                Order order = new Order()
                {
                    IsActive     = true,
                    IsDelete     = false,
                    BuyerId      = user.Id,
                    TravellerId  = offerViewModel.TravelerId,
                    Total        = post.Price * post.Quantity + offer.ShippingFee,
                    ShippingFee  = offer.ShippingFee,
                    DateCreated  = DateTime.UtcNow.AddHours(7),
                    DeliveryDate = DateTime.UtcNow.AddHours(7),
                    DateUpdated  = DateTime.UtcNow.AddHours(7),
                    OfferId      = offerViewModel.OfferId,
                    Status       = (int)OrderType.Transit
                };

                orderService.CreateOrder(order);
                orderService.SaveOrder();

                //update post
                post.Type = Post.PostType.Transit;
                var deposit = post.Deposit;
                post.Deposit = (deposit == 0) ? offerViewModel.Deposit : (offerViewModel.Deposit + post.Deposit);
                postService.UpdatePost(post);
                postService.SavePost();

                //create transaction
                Transaction transaction = new Transaction()
                {
                    DateCreated = DateTime.UtcNow.AddHours(7),
                    Description = offerViewModel.Description,
                    IsActive    = true,
                    IsDelete    = false,
                    IsSuccess   = true,
                    Value       = offerViewModel.Deposit,
                    OrderId     = order.Id,
                    UserId      = user.Id
                };
                transactionService.CreateTransaction(transaction);
                transactionService.SaveTransaction();
                checkoutVM.Deposit       = post.Deposit;
                checkoutVM.OrderId       = order.Id;
                checkoutVM.TransactionId = transaction.Id;
                //delete post on redis
                Utilities.deletePostRedis("Post_" + post.BuyFrom.Name.ToString() + "_" + post.DeliveryTo.Name.ToString(), post.Id.ToString());
            }
            catch
            {
                return(BadRequest("Error"));
            }
            return(Ok(checkoutVM));
        }
示例#5
0
        public IHttpActionResult CompleteOrder(PostViewModel model)
        {
            string username = Utilities.GetUserNameFromRequest(Request);

            if (String.IsNullOrEmpty(username))
            {
                return(Unauthorized());
            }
            CompleteOrderResponse response = new CompleteOrderResponse();

            try
            {
                var user = userService.GetUsers().Where(u => u.UserName.Equals(username)).FirstOrDefault();
                if (user == null)
                {
                    return(Unauthorized());
                }
                var    order      = orderService.GetOrders().Where(o => o.Offer.PostId == model.Id).FirstOrDefault();
                var    post       = order.Offer.Post;
                double amount     = order.Total - post.Deposit;
                bool   IsHasMoney = true;
                if (amount > 0) // nếu chưa thanh toán hết total => kiểm tra ví => tạo transaction => cập nhật post
                {
                    // 1. kiểm tra tiền trong ví
                    if (amount > user.Current_Money) // không đủ tiền
                    {
                        response.Status_code = "E01";
                        IsHasMoney           = false;
                    }
                    else // đủ tiền
                    {
                        // 2. tạo transaction
                        transactionService.CreateTransaction(new Transaction()
                        {
                            DateCreated = DateTime.Now,
                            IsActive    = true,
                            IsDelete    = false,
                            IsSuccess   = true,
                            Value       = order.Total - post.Deposit,
                            OrderId     = order.Id,
                            UserId      = user.Id,
                            PostId      = post.Id,
                            Type        = Transaction.TypeTrans.PostIn,
                            Description = "Hoàn tất đơn hàng " + order.Offer.Post.ProductName
                        });
                        user.Current_Money -= amount;
                        userService.UpdateUser(user);
                    }
                }
                // Chưa thanh toán hết:  đủ tiền => IsHasMoney = true / không đủ tiền => IsHasMoney = false
                // Đã thanh toán hết: IsHasMoney = true
                if (IsHasMoney)
                {
                    // cập nhật lại phần đã thanh toán. Complete => thanh toán hết
                    post.Deposit = order.Total;
                    post.Type    = Post.PostType.Completed;
                    postService.UpdatePost(post);

                    // cập nhật lại trạng thái order -> completed
                    order.Status      = (int)OrderType.WaitAdmin;
                    order.DateUpdated = DateTime.UtcNow.AddHours(7);
                    orderService.UpdateOrder(order);

                    //save to db
                    orderService.SaveOrder();

                    response.Status_code   = "E00";
                    response.Deposit       = post.Deposit;
                    response.OrderId       = order.Id;
                    response.Current_Money = user.Current_Money;

                    trackingOrderService.CreateTrackingOrder(new TrackingOrder()
                    {
                        Status   = (int)TrackingOrder.TrackingOrderType.Finished,
                        OrderId  = order.Id,
                        IsDelete = false,
                    });

                    trackingOrderService.SaveTrackingOrder();
                    // notify cho thằng traveler: buyer đã xác nhận
                    string con = order.Offer.Traveller.SignalConnect;
                    if (con != null)
                    {
                        notificationService.CreateNotification(new Notification
                        {
                            DateCreated = DateTime.UtcNow.AddHours(7),
                            IsRead      = false,
                            Message     = user.LastName + " đã xác nhận hoàn thành đơn hàng " + order.Offer.Post.ProductName,
                            Title       = "Chuyến đi " + order.TripId,
                            UserId      = order.Offer.TravellerId
                        });
                        notificationService.SaveNotification();
                        HubUtilities.GetHub().Clients.Client(con).completeOrder(user.LastName + " đã xác nhận hoàn thành đơn hàng " + order.Offer.Post.ProductName, order.Id);
                    }
                }
            }
            catch
            {
                return(BadRequest());
            }
            return(Ok(response));
        }