コード例 #1
0
ファイル: DecaAPI.cs プロジェクト: war-man/BIBIAM
        public static DC_Order_Response createOCMOrder(DC_OCM_Create_Order order)
        {
            DC_Order_Response apiModel = new DC_Order_Response();

            try
            {
                try
                {
                    using (var dbConn = Helpers.OrmliteConnection.openConn())
                    {
                        var log = new DC_OCM_Create_Order_Log();
                        log.log       = order;
                        log.CreatedAt = DateTime.Now;
                        log.CreatedBy = "apiLog";
                        dbConn.Insert(log);
                    }
                }
                catch (Exception)
                {
                }

                var request = JsonConvert.SerializeObject(order);
                var data    = RestfulClient.POST(ConfigurationManager.AppSettings["decaOrderAPI"].ToString().Trim(), request);

                try
                {
                    using (var dbConn = Helpers.OrmliteConnection.openConn())
                    {
                        var log = new DC_OCM_Create_Order_Log();
                        log.log = new DC_OCM_Create_Order {
                            code_token = order.code_token, ghi_chu = data
                        };
                        log.CreatedAt = DateTime.Now;
                        log.CreatedBy = "apiLog";
                        dbConn.Insert(log);
                    }
                }
                catch (Exception)
                {
                }

                if (!String.IsNullOrEmpty(data))
                {
                    JavaScriptSerializer objJavascript = new JavaScriptSerializer {
                        MaxJsonLength = Int32.MaxValue, RecursionLimit = 100
                    };
                    apiModel = objJavascript.Deserialize <DC_Order_Response>(data);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(apiModel);
        }
コード例 #2
0
        //createOCMOrder
        public ActionResult CreateOCMOrder(string data)
        {
            try
            {
                using (var dbConn = Helpers.OrmliteConnection.openConn())
                {
                    if (asset.Update)
                    {
                        string[] separators = { "@@" };
                        var      listid     = data.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                        var      listData   = dbConn.Select <Deca_Order_Header>("ID IN (" + string.Join(",", listid.Select(s => s)) + ") AND Status <> 10");
                        foreach (var order in listData)
                        {
                            //da tao order tren ocm
                            if (!String.IsNullOrEmpty(order.RefID))
                            {
                                if (order.PaymentStatus == "1")
                                {
                                    var bankTrans = dbConn.SingleOrDefault <Deca_Bank_Transactions>("OrderID={0}", order.OrderID);

                                    //thong in update truyen di
                                    var updateOrderAPI = new DC_OCM_Update();
                                    updateOrderAPI.ma_don_hang            = order.RefID;
                                    updateOrderAPI.code_token             = order.OrderID;
                                    updateOrderAPI.ma_giao_dich           = bankTrans.BillTransRef;
                                    updateOrderAPI.ngay_giao_dich         = order.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss");
                                    updateOrderAPI.p_tai_khoan_thanh_toan = "*****@*****.**";
                                    updateOrderAPI.ma_ngan_hang           = !string.IsNullOrEmpty(order.Bank) ? order.Bank : "BA00001";
                                    updateOrderAPI.goi_tra_gop            = 12;
                                    updateOrderAPI.p_id_cong_thanh_toan   = 5;
                                    updateOrderAPI.p_loai_thanh_toan      = 9;
                                    updateOrderAPI.api_key             = "8b8a689ce420";
                                    updateOrderAPI.api_secret          = "0124ccc29bc3c7e287a714d7eb99b2ce";
                                    updateOrderAPI.trang_thai_don_hang = 1;

                                    //goi api update
                                    var decaOrderApi = Helpers.DecaAPI.updateOCMOrder(updateOrderAPI);

                                    if (decaOrderApi.response_code != "0")
                                    {
                                        return(Json(new { success = false, message = order.OrderID + ": " + decaOrderApi.message }));
                                    }
                                }
                                else
                                {
                                    return(Json(new { success = false, message = order.OrderID + ": Đã được tạo trên OCM" }));
                                }
                            }
                            else
                            {
                                //da thanh toan va chua tao order tren ocm
                                if (order.PaymentStatus == "1")
                                {
                                    var orderitem = dbConn.SingleOrDefault <Deca_Order_Detail>("OrderID={0}", order.OrderID);
                                    //lay thong tin transaction
                                    var bankTrans = dbConn.SingleOrDefault <Deca_Bank_Transactions>("OrderID={0}", order.OrderID);

                                    var item    = new List <KeyValuePair <string, DC_OCM_Item> >();
                                    var element = new KeyValuePair <string, DC_OCM_Item>(orderitem.ProductID, new DC_OCM_Item()
                                    {
                                        price = orderitem.UnitPrice, number = orderitem.Quantity, code_price = orderitem.PriceID.ToString()
                                    });
                                    item.Add(element);

                                    //tao request
                                    DC_OCM_Create_Order orderApi = new DC_OCM_Create_Order();
                                    orderApi.p_id_nguoi_mua_hang             = "67995";//config
                                    orderApi.p_id_gian_hang                  = int.Parse(order.ShopID);
                                    orderApi.san_pham                        = item.ToDictionary(s => s.Key, s => s.Value);
                                    orderApi.p_id_tinh                       = order.ShippingCity;
                                    orderApi.p_id_quan_huyen                 = order.ShippingDistrict;
                                    orderApi.p_so_luong_san_pham             = 1;
                                    orderApi.p_loai_nt                       = "VND";//config
                                    orderApi.p_ten_nguoi_nhan_hang           = order.CustomerName;
                                    orderApi.p_dia_chi_nhan_hang             = order.ShippingAddress;
                                    orderApi.p_dien_thoai_nhan_hang          = order.MobilePhone;
                                    orderApi.p_ten_nguoi_nhan_hang_khong_dau = Helpers.convertToUnSign3.Init(order.CustomerName);
                                    orderApi.p_dia_chi_nhan_hang_khong_dau   = Helpers.convertToUnSign3.Init(order.ShippingAddress);
                                    orderApi.p_dien_thoai_di_dong_chuan      = order.MobilePhone;
                                    orderApi.p_id_cong_thanh_toan            = 5;                //config
                                    orderApi.p_tai_khoan_thanh_toan          = "*****@*****.**"; //config
                                    orderApi.ma_ngan_hang                    = !String.IsNullOrEmpty(order.Bank) ? order.Bank : "ShinhanBank";
                                    orderApi.p_loai_thanh_toan               = 9;                //config
                                    orderApi.gia_tri_thanh_toan              = orderitem.UnitPrice * orderitem.Quantity;
                                    orderApi.code_token                      = orderitem.OrderID;
                                    orderApi.ma_giao_dich                    = bankTrans.BillTransRef;//giao dich cong thanh toan
                                    orderApi.ngay_giao_dich                  = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                    orderApi.api_key               = "8b8a689ce420";
                                    orderApi.api_secret            = "0124ccc29bc3c7e287a714d7eb99b2ce";
                                    orderApi.goi_tra_gop           = 12;
                                    orderApi.trang_thai_thanh_toan = 1;
                                    orderApi.ghi_chu               = !string.IsNullOrEmpty(order.Note) ? order.Note : "không có thông tin";
                                    //call helper create order OCM
                                    var decaOrderApi = Helpers.DecaAPI.createOCMOrder(orderApi);

                                    if (decaOrderApi.response_code == "0")
                                    {
                                        dbConn.Execute("UPDATE Deca_Order_Header SET RefID = '" + decaOrderApi.code_order + "', UpdatedAt = GETDATE(),UpdatedBy = '" + currentUser.UserName + "' WHERE OrderID = '" + order.OrderID + "'");
                                    }
                                    else
                                    {
                                        return(Json(new { success = false, message = order.OrderID + ": " + decaOrderApi.error_message }));
                                    }
                                }
                                else // chua thanh toan va tao order tren ocm
                                {
                                    var orderitem = dbConn.SingleOrDefault <Deca_Order_Detail>("OrderID={0}", order.OrderID);

                                    var item    = new List <KeyValuePair <string, DC_OCM_Item> >();
                                    var element = new KeyValuePair <string, DC_OCM_Item>(orderitem.ProductID, new DC_OCM_Item()
                                    {
                                        price = orderitem.UnitPrice, number = orderitem.Quantity, code_price = orderitem.PriceID.ToString()
                                    });
                                    item.Add(element);

                                    DC_OCM_Create_Order orderApi = new DC_OCM_Create_Order();
                                    orderApi.p_id_nguoi_mua_hang             = "67995";//config
                                    orderApi.p_id_gian_hang                  = int.Parse(order.ShopID);
                                    orderApi.san_pham                        = item.ToDictionary(s => s.Key, s => s.Value);
                                    orderApi.p_id_tinh                       = order.ShippingCity;
                                    orderApi.p_id_quan_huyen                 = order.ShippingDistrict;
                                    orderApi.p_so_luong_san_pham             = 1;
                                    orderApi.p_loai_nt                       = "VND";//config
                                    orderApi.p_ten_nguoi_nhan_hang           = order.CustomerName;
                                    orderApi.p_dia_chi_nhan_hang             = order.ShippingAddress;
                                    orderApi.p_dien_thoai_nhan_hang          = order.MobilePhone;
                                    orderApi.p_ten_nguoi_nhan_hang_khong_dau = Helpers.convertToUnSign3.Init(order.CustomerName);
                                    orderApi.p_dia_chi_nhan_hang_khong_dau   = Helpers.convertToUnSign3.Init(order.ShippingAddress);
                                    orderApi.p_dien_thoai_di_dong_chuan      = order.MobilePhone;
                                    orderApi.p_id_cong_thanh_toan            = 5;                //config
                                    orderApi.p_tai_khoan_thanh_toan          = "*****@*****.**"; //config
                                    orderApi.ma_ngan_hang                    = !String.IsNullOrEmpty(order.Bank) ? order.Bank : "ShinhanBank";
                                    orderApi.p_loai_thanh_toan               = 9;                //config
                                    orderApi.gia_tri_thanh_toan              = orderitem.UnitPrice * orderitem.Quantity;
                                    orderApi.code_token                      = orderitem.OrderID;
                                    orderApi.ma_giao_dich                    = orderitem.OrderID;//giao dich cong thanh toan
                                    orderApi.ngay_giao_dich                  = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                    orderApi.api_key               = "8b8a689ce420";
                                    orderApi.api_secret            = "0124ccc29bc3c7e287a714d7eb99b2ce";
                                    orderApi.goi_tra_gop           = 12;
                                    orderApi.trang_thai_thanh_toan = 0;
                                    orderApi.ghi_chu               = !string.IsNullOrEmpty(order.Note) ? order.Note : "không có thông tin";
                                    //call helper create order OCM
                                    var decaOrderApi = Helpers.DecaAPI.createOCMOrder(orderApi);

                                    if (decaOrderApi.response_code == "0")
                                    {
                                        order.RefID     = decaOrderApi.code_order;
                                        order.UpdatedAt = DateTime.Now;
                                        order.UpdatedBy = currentUser.UserName;
                                        dbConn.Update(order);
                                    }
                                    else
                                    {
                                        return(Json(new { success = false, message = order.OrderID + ": " + decaOrderApi.error_message }));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        return(Json(new { success = false, message = "Không có quyền gửi" }));
                    }
                }
                return(Json(new { success = true }));
            }
            catch (Exception e)
            {
                return(Json(new { success = false, message = e.Message }));
            }
        }