コード例 #1
0
        public async Task <ActionResult> PlaceOrder([FromBody] OrderInfo data, string username)
        {
            try
            {
                IAsyncCursor <Address> userCursor = await _db.GetCollection <Address>("UserInfo").FindAsync(Builders <Address> .Filter.Eq("UserName", username));

                var users = userCursor.ToList();
                if (users.Count > 0)
                {
                    IAsyncCursor <Cart> cartCursor = await _db.GetCollection <Cart>("Cart").FindAsync(Builders <Cart> .Filter.Eq("UserName", username));

                    var cartDatas = cartCursor.ToList();
                    if (cartDatas.Count > 0)
                    {
                        var ordersCount = order_db.GetCollection <OrderInfo>("OrderInfo").Find(Builders <OrderInfo> .Filter.Empty).Count();
                        data.OrderId       = ordersCount + 1;
                        data.UserName      = username;
                        data.PaymentMethod = "Nil";
                        PaymentMethod     paymentMethod = new PaymentMethod();
                        List <StatusCode> paymentStatus = new List <StatusCode>();
                        paymentStatus.Add(new StatusCode {
                            Date = DateTime.UtcNow, StatusId = 1, Description = "Payment Initiated"
                        });
                        paymentMethod.Status = paymentStatus;
                        data.PaymentDetails  = paymentMethod;
                        List <Address> addressList = new List <Address>();
                        foreach (var address in users)
                        {
                            if (address.DefaultAddress == true)
                            {
                                addressList.Add(address);
                            }
                        }
                        data.Address = addressList;
                        if (data.Address.Count == 0)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "404",
                                Message = "No default address found",
                                Data = null
                            }));
                        }
                        List <ProductDetails> productList = new List <ProductDetails>();
                        foreach (var cart in cartDatas)
                        {
                            foreach (var product in MH.GetProducts(cart.ProductSKU, product_db).Result)
                            {
                                if (product.ProductStock < cart.ProductQuantity)
                                {
                                    return(BadRequest(new ResponseData
                                    {
                                        Code = "403",
                                        Message = "Order quantity is higher than the product stock.",
                                        Data = null
                                    }));
                                }
                                ProductDetails productDetails = new ProductDetails();
                                productDetails.ProductSKU = cart.ProductSKU;
                                productDetails.Status     = "Order Placed";
                                productDetails.Reviewed   = false;
                                List <StatusCode> productStatus = new List <StatusCode>();
                                productStatus.Add(new StatusCode {
                                    StatusId = 1, Date = DateTime.UtcNow, Description = "Order Placed"
                                });
                                productDetails.StatusCode    = productStatus;
                                productDetails.ProductInCart = cart;
                                productList.Add(productDetails);
                            }
                        }
                        data.ProductDetails = productList;
                        data.OrderStatus    = "Order Placed";
                        await order_db.GetCollection <OrderInfo>("OrderInfo").InsertOneAsync(data);

                        List <string> productInfoList = new List <string>();
                        foreach (var cart in cartDatas)
                        {
                            productInfoList.Add(cart.ProductSKU);
                        }
                        string        productInfo  = String.Join(":", productInfoList);
                        RegisterModel userInfo     = BsonSerializer.Deserialize <RegisterModel>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username), "Authentication", "Authentication").Result);
                        Address       addressInfo  = BsonSerializer.Deserialize <Address>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username), "UserInfo", "UserInfo").Result);
                        PaymentModel  paymentModel = new PaymentModel {
                            FirstName = userInfo.FullName, UserName = username, LastName = "", ProductInfo = productInfo, Amount = data.TotalAmount.ToString(), Email = userInfo.Email, PhoneNumber = userInfo.PhoneNumber, AddressLine1 = addressInfo.AddressLines, AddressLine2 = addressInfo.Landmark, City = addressInfo.City, State = addressInfo.State, Country = userInfo.UserLocation, ZipCode = addressInfo.PinCode, OrderId = data.OrderId
                        };
                        if (data.TotalAmount == 0)
                        {
                            var updatePaymentMethod = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentMethod", "Coupon"));

                            var updatePaymentDetails = await MH.UpdatePaymentDetails(paymentModel.OrderId);

                            var removecartItems = await MH.RemoveCartItems(paymentModel.OrderId, paymentModel.UserName, paymentModel.Email);

                            var sendGift = GlobalHelper.SendGift(paymentModel.OrderId);
                            return(Ok(new ResponseData
                            {
                                Code = "201",
                                Message = "Payment success",
                                Data = null
                            }));
                        }
                        else
                        {
                            var hashtableData = PayUHelper.GetHashtableData(paymentModel);
                            return(Ok(new ResponseData
                            {
                                Code = "200",
                                Message = "Order Placed",
                                Data = hashtableData
                            }));
                        }
                    }
                    else
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "402",
                            Message = "Cart not found",
                            Data = null
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "401",
                        Message = "UserInfo not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("OrderController", "PlaceOrder", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
コード例 #2
0
        public async Task <ActionResult> PaymentSuccess(IFormCollection paymentResponse)
        {
            if (paymentResponse != null)
            {
                PaymentModel paymentModel = new PaymentModel
                {
                    Email       = paymentResponse["email"],
                    OrderId     = Convert.ToInt16(paymentResponse["udf1"]),
                    UserName    = paymentResponse["udf2"],
                    ProductInfo = paymentResponse["productinfo"],
                    FirstName   = paymentResponse["firstname"],
                    Amount      = paymentResponse["amount"],
                };
                try
                {
                    if (PU.Generatehash512(PU.GetReverseHashString(paymentResponse["txnid"], paymentModel)) == paymentResponse["hash"])
                    {
                        var updatePaymentMethod = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentMethod", paymentResponse["mode"].ToString()));

                        var updatePaymentDetails = await MH.UpdatePaymentDetails(paymentModel.OrderId);

                        var removeCartItems    = MH.RemoveCartItems(paymentModel.OrderId, paymentModel.UserName, paymentModel.Email);
                        var sendGift           = GlobalHelper.SendGift(paymentModel.OrderId);
                        var sendProductDetails = EmailHelper.SendEmail_ProductDetails(paymentModel.Email, paymentModel.OrderId);
                        return(Redirect(GlobalHelper.ReadXML().Elements("payu").Where(x => x.Element("current").Value.Equals("Yes")).Descendants("redirectsuccess").First().Value));
                    }
                    else
                    {
                        var updatePaymentMethod = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentMethod", paymentResponse["mode"].ToString()));

                        PaymentMethod     paymentDetails = new PaymentMethod();
                        List <StatusCode> statusCodeList = new List <StatusCode>();
                        var orderData = BsonSerializer.Deserialize <OrderInfo>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo").Result);
                        foreach (var detail in orderData.PaymentDetails.Status)
                        {
                            statusCodeList.Add(detail);
                        }
                        statusCodeList.Add(new StatusCode {
                            StatusId = 3, Description = "Payment Failed", Date = DateTime.UtcNow
                        });
                        paymentDetails.Status = statusCodeList;
                        var updatePaymentDetails = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentDetails", paymentDetails));

                        return(Redirect(GlobalHelper.ReadXML().Elements("payu").Where(x => x.Element("current").Value.Equals("Yes")).Descendants("redirectfailure").First().Value));
                    }
                }
                catch (Exception ex)
                {
                    LoggerDataAccess.CreateLog("PaymentController", "PaymentSuccess", ex.Message);
                    var updatePaymentMethod = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentMethod", paymentResponse["mode"].ToString()));

                    PaymentMethod     paymentDetails = new PaymentMethod();
                    List <StatusCode> statusCodeList = new List <StatusCode>();
                    var orderData = BsonSerializer.Deserialize <OrderInfo>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo").Result);
                    foreach (var detail in orderData.PaymentDetails.Status)
                    {
                        statusCodeList.Add(detail);
                    }
                    statusCodeList.Add(new StatusCode {
                        StatusId = 3, Description = "Payment Failed", Date = DateTime.UtcNow
                    });
                    paymentDetails.Status = statusCodeList;
                    var updatePaymentDetails = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", paymentModel.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentDetails", paymentDetails));

                    return(Redirect(GlobalHelper.ReadXML().Elements("payu").Where(x => x.Element("current").Value.Equals("Yes")).Descendants("redirectfailure").First().Value));
                }
            }
            else
            {
                return(Redirect(GlobalHelper.ReadXML().Elements("payu").Where(x => x.Element("current").Value.Equals("Yes")).Descendants("redirectfailure").First().Value));
            }
        }