コード例 #1
0
        public static IRestResponse SendEmailNewOrder(OrderDetail orderDetail, Customer customer, Dictionary <Product, int> dicProductCount)
        {
            try
            {
                var path = AppGlobal.GetAppPath() + AppGlobal.DEFAULT_FOLDER_CONFIG + "TemplateEmail.html";
                if (!File.Exists(path))
                {
                    Logger.Write("Khong tim thay file emailTemplate", true);
                    throw new Exception("Khong tim thay file emailTemplate");
                }
                var pathBaoGia = AppGlobal.GetAppPath() + AppGlobal.DEFAULT_FOLDER_CONFIG + "TemplateBaogia.html";
                if (!File.Exists(pathBaoGia))
                {
                    Logger.Write("Khong tim thay file TemplateBaogia", true);
                    throw new Exception("Khong tim thay file TemplateBaogia");
                }

                var           customerName   = customer.Name ?? "Khách hàng";
                var           fullText       = File.ReadAllText(path);
                var           fullTextBaoGia = File.ReadAllText(pathBaoGia);
                StringBuilder tableContent   = new StringBuilder();
                // tao table content
                int         stt        = 1;
                decimal     totalPrice = 0;
                CultureInfo cul        = CultureInfo.GetCultureInfo("vi-VN"); // try with "en-US"
                foreach (var product in dicProductCount)
                {
                    string content = fullTextBaoGia.Replace("#stt", stt.ToString());
                    content = content.Replace("#productname", product.Key.Name);
                    content = content.Replace("#quantity", product.Value.ToString());
                    decimal unitPrice = product.Key.UnitPrice.HasValue ? product.Key.UnitPrice.Value : 0;
                    content = content.Replace("#cost", unitPrice.ToString("#,###", cul.NumberFormat));
                    decimal price = product.Value * unitPrice;
                    totalPrice += price;
                    content     = content.Replace("#money", price.ToString("#,###", cul.NumberFormat));
                    tableContent.Append(content);
                    stt++;
                }

                #region tao content bao gia

                #endregion
                fullText = fullText.Replace("#Username", customerName);                                  // ten khách hàng
                fullText = fullText.Replace("#timeorder", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")); // ngay dat
                fullText = fullText.Replace("#name", customerName);
                fullText = fullText.Replace("#phone", customer.Phone);
                fullText = fullText.Replace("#address", customer.Address);
                fullText = fullText.Replace("#contenttable", tableContent.ToString());
                fullText = fullText.Replace("#totalmoney", totalPrice.ToString("#,###", cul.NumberFormat));

                //set du lieu cho link confirm
                var baseUrl = AppGlobal.ElectricConfig.BaseUrl;
                baseUrl  = baseUrl.Replace("2000", AppGlobal.ElectricConfig.Port.ToString());
                baseUrl += "api/confirmorder?req=";
                baseUrl += EmailUtils.ConfirmOrderToString(new ConfirmOrder {
                    OrderId = orderDetail.Id
                });
                fullText = fullText.Replace("#linkconfirm", baseUrl);

                RestClient client = new RestClient();
                client.BaseUrl       = new Uri("https://api.mailgun.net/v3");
                client.Authenticator =
                    new HttpBasicAuthenticator("api", AppGlobal.ElectricConfig.MailGunToken);
                RestRequest request = new RestRequest();
                request.AddParameter("domain", "musickid.vn", ParameterType.UrlSegment);
                request.Resource = "{domain}/messages";
                request.AddParameter("from", "ElectricShop <*****@*****.**>");
                request.AddParameter("to", customer.Email);
                request.AddParameter("subject", "Xác nhận đơn hàng");
                request.AddParameter("html", fullText);
                request.Method = Method.POST;
                return(client.Execute(request));
            }
            catch (Exception e)
            {
                Logger.Write("Loi gui mail xac nhan don hang", true);
                Logger.Write(e.ToString(), true);
                throw;
            }
        }
コード例 #2
0
        public HttpResponseMessage  Get(string req)
        {
            try
            {
                var    path = AppGlobal.GetAppPath() + AppGlobal.DEFAULT_FOLDER_CONFIG + "TemplateConfirmOrder.html";
                string Html = File.ReadAllText(path);
                if (req == null)
                {
                    Html = Html.Replace("#title", "Sorry!");
                    Html = Html.Replace("#content", "There was a problem during auto-order verification, please contact administrator for processing.");
                }
                else
                {
                    var cfOrderObj = EmailUtils.StringToConfirmOrder(req);
                    if (cfOrderObj == null)
                    {
                        Html = Html.Replace("#title", "Sorry!");
                        Html = Html.Replace("#content", "There was a problem during auto-order verification, please contact administrator for processing.");
                    }
                    else
                    {
                        var orderDetail = MemoryInfo.GetOrderDetail(cfOrderObj.OrderId);
                        if (orderDetail == null)
                        {
                            Html = Html.Replace("#title", "Sorry!");
                            Html = Html.Replace("#content", "There was a problem during auto-order verification, please contact administrator for processing.");
                        }
                        else
                        {
                            // xu li cap nhap trang thai lenh
                            orderDetail.OrderStatus = OrderStatus.Approval.ToString();
                            UpdateEntitySql updateEntitySql = new UpdateEntitySql();
                            var             lstCommand      = new List <EntityCommand>();
                            lstCommand.Add(new EntityCommand {
                                BaseEntity = new Entity.Entity(orderDetail), EntityAction = EntityAction.Update
                            });
                            bool isOkDone = updateEntitySql.UpdateDefault(lstCommand);
                            if (!isOkDone)
                            {
                                Html = Html.Replace("#title", "Sorry!");
                                Html = Html.Replace("#content", "There was a problem during auto-order verification, please contact administrator for processing.");
                            }
                            else
                            {
                                // update memory
                                MemorySet.UpdateAndInsertEntity(orderDetail);
                                Html = Html.Replace("#title", "Great!");
                                Html = Html.Replace("#content", "Confirm Order Succces!");
                            }
                        }
                    }
                }
                StringBuilder sb       = new StringBuilder(Html);
                var           response = new HttpResponseMessage();
                response.Content = new StringContent(sb.ToString());
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                response.ReasonPhrase = "success";
                // what code should i write here
                return(response);
            }
            catch (Exception ex)
            {
                Logger.Write(ex.ToString());
            }

            return(null);
        }