private ECPayForm GetECPayForm(Guid orderId) { Order order = _checkoutService.GetOrder(orderId); OrderDetail od = _checkoutService.GetOrderDetail(order); string merchantTradeNo = order.MerchantTradeNo; string url = WebConfigurationManager.AppSettings["WebsiteUrl"]; decimal finalAmount = od.FinalPrice; string productName = od.ProductName; ECPayForm ecpayForm = new ECPayForm { ChoosePayment = "ALL", EncryptType = "1", ItemName = "uCleaner打掃服務", MerchantID = "2000132", MerchantTradeDate = DateTime.UtcNow.AddHours(8).ToString("yyyy/MM/dd HH:mm:ss"), MerchantTradeNo = merchantTradeNo, OrderResultURL = url + "/Checkout/Success", PaymentType = "aio", ReturnURL = url + "/Checkout/ECPayReturn", TotalAmount = Math.Round(finalAmount).ToString(), TradeDesc = HttpUtility.UrlEncode(productName), }; string HashKey = "5294y06JbISpM5x9"; string HashIV = "v77hoKGq4kWxNNIS"; Dictionary <string, string> paramList = new Dictionary <string, string> { { "ChoosePayment", ecpayForm.ChoosePayment }, { "EncryptType", ecpayForm.EncryptType }, { "ItemName", ecpayForm.ItemName }, { "MerchantID", ecpayForm.MerchantID }, { "MerchantTradeDate", ecpayForm.MerchantTradeDate }, { "MerchantTradeNo", ecpayForm.MerchantTradeNo }, { "OrderResultURL", ecpayForm.OrderResultURL }, { "PaymentType", ecpayForm.PaymentType }, { "ReturnURL", ecpayForm.ReturnURL }, { "TotalAmount", ecpayForm.TotalAmount }, { "TradeDesc", ecpayForm.TradeDesc }, }; string Parameters = string.Join("&", paramList.Select(x => $"{x.Key}={x.Value}").OrderBy(x => x)); ecpayForm.CheckMacValue = GetCheckMacValue(HashKey, Parameters, HashIV); return(ecpayForm); }
public ActionResult AddOrder(UserForm post) { DateTime now = DateTime.Now; string accountName; string productName; string url = WebConfigurationManager.AppSettings["WebsiteUrl"]; string merchantTradeNo; Guid favoriteId; decimal finalAmount; Guid? couponDetailId; if (post.CouponDetailId == null) { couponDetailId = null; } else { couponDetailId = Guid.Parse(post.CouponDetailId); } try { accountName = Helpers.DecodeCookie(Request.Cookies["user"]["user_accountname"]); favoriteId = Guid.Parse(post.FavoriteId); _checkoutService.CheckAccountExist(accountName); _checkoutService.CheckFavoriteId(accountName, favoriteId); finalAmount = _checkoutService.GetTotalPrice(favoriteId); if (couponDetailId != null) { finalAmount -= _checkoutService.GetDiscountAmount(couponDetailId); } merchantTradeNo = _checkoutService.GetNextMerchantTradeNo(); OrderData orderData = new OrderData { AccountName = accountName, FavoriteId = favoriteId, CouponDetailId = couponDetailId, FinalPrice = finalAmount, MerchantTradeNo = merchantTradeNo, Now = now, }; var result = _checkoutService.CreateOrder(post, orderData, out productName); if (result.IsSuccessful) { _checkoutService.SaveMerchantTradeNo(merchantTradeNo); } else { throw new Exception("訂單建立失敗"); } } catch (Exception ex) { return(Json(ex.Message)); } ECPayForm ecpayForm = new ECPayForm(); ecpayForm.ChoosePayment = "ALL"; ecpayForm.EncryptType = "1"; ecpayForm.ItemName = "uCleaner打掃服務"; ecpayForm.MerchantID = "2000132"; ecpayForm.MerchantTradeDate = now.ToString("yyyy/MM/dd HH:mm:ss"); ecpayForm.MerchantTradeNo = merchantTradeNo; ecpayForm.OrderResultURL = url + "/Checkout/SuccessView"; ecpayForm.PaymentType = "aio"; ecpayForm.ReturnURL = url + "/Checkout/ECPayReturn"; ecpayForm.TotalAmount = Math.Round(finalAmount).ToString(); ecpayForm.TradeDesc = HttpUtility.UrlEncode(productName); string HashKey = "5294y06JbISpM5x9"; string HashIV = "v77hoKGq4kWxNNIS"; Dictionary <string, string> paramList = new Dictionary <string, string> { { "ChoosePayment", ecpayForm.ChoosePayment }, //{ "ClientBackURL", ecpayForm.ClientBackURL }, { "EncryptType", ecpayForm.EncryptType }, { "ItemName", ecpayForm.ItemName }, { "MerchantID", ecpayForm.MerchantID }, { "MerchantTradeDate", ecpayForm.MerchantTradeDate }, { "MerchantTradeNo", ecpayForm.MerchantTradeNo }, { "OrderResultURL", ecpayForm.OrderResultURL }, { "PaymentType", ecpayForm.PaymentType }, { "ReturnURL", ecpayForm.ReturnURL }, { "TotalAmount", ecpayForm.TotalAmount }, { "TradeDesc", ecpayForm.TradeDesc }, }; string Parameters = string.Join("&", paramList.Select(x => $"{x.Key}={x.Value}").OrderBy(x => x)); ecpayForm.CheckMacValue = GetCheckMacValue(HashKey, Parameters, HashIV); return(Json(ecpayForm)); }
public async Task <ActionResult> Repay(string orderId) { Guid orderGuid = Guid.Parse(orderId); Order order = _checkoutService.GetOrder(orderGuid); OrderDetail od = _checkoutService.GetOrderDetail(order); //雙重檢查,檢查資料庫訂單狀態、綠界的付款狀態 if (!_checkoutService.CheckIsUnpaid(orderGuid)) { string errorMsg = JsonConvert.SerializeObject(new { IsSuccessful = false, Message = "此筆訂單已付款" }); return(Content(errorMsg, "application/json")); } if (DateTime.UtcNow.AddHours(8).Date >= order.DateService.Date) { string errorMsg = JsonConvert.SerializeObject(new { IsSuccessful = false, Message = "訂單已過付款期限,需在服務日前一天以前付款" }); return(Content(errorMsg, "application/json")); } ECPayForm form = GetECPayForm(orderGuid); //向綠界查詢訂單,確認此訂單未付款 var dictionary = new Dictionary <string, string> { { "MerchantID", form.MerchantID }, { "MerchantTradeNo", form.MerchantTradeNo }, { "TimeStamp", DateTimeOffset.Now.ToUnixTimeSeconds().ToString() }, }; string Parameters = string.Join("&", dictionary.Select(x => $"{x.Key}={x.Value}").OrderBy(x => x)); string HashKey = "5294y06JbISpM5x9"; string HashIV = "v77hoKGq4kWxNNIS"; string checkMacValue = GetCheckMacValue(HashKey, Parameters, HashIV); dictionary.Add("CheckMacValue", checkMacValue); var content = new FormUrlEncodedContent(dictionary); var response = await client.PostAsync("https://payment-stage.ecpay.com.tw/Cashier/QueryTradeInfo/V5", content); string responseStr = await response.Content.ReadAsStringAsync(); var paramArray = responseStr.Split('&'); var responseDictionary = new Dictionary <string, string>(); foreach (var param in paramArray) { var kv = param.Split('='); responseDictionary.Add(kv[0], kv[1]); } if (responseDictionary["TradeStatus"] == "1") { return(Content($"此筆訂單已付款")); } //確認完畢,給新的MerchantTradeNo form.MerchantTradeDate = DateTime.UtcNow.AddHours(8).ToString("yyyy/MM/dd HH:mm:ss"); form.MerchantTradeNo = _checkoutService.GetNextMerchantTradeNo(); var newDictionary = new Dictionary <string, string> { { "ChoosePayment", form.ChoosePayment }, { "EncryptType", form.EncryptType }, { "ItemName", form.ItemName }, { "MerchantID", form.MerchantID }, { "MerchantTradeDate", form.MerchantTradeDate }, { "MerchantTradeNo", form.MerchantTradeNo }, { "OrderResultURL", form.OrderResultURL }, { "PaymentType", form.PaymentType }, { "ReturnURL", form.ReturnURL }, { "TotalAmount", form.TotalAmount }, { "TradeDesc", form.TradeDesc }, }; string newParameters = string.Join("&", newDictionary.Select(x => $"{x.Key}={x.Value}").OrderBy(x => x)); form.CheckMacValue = GetCheckMacValue(HashKey, newParameters, HashIV); //1.更新資料庫原訂單 var result = _checkoutService.RepayUpdateOrder(order, od, newDictionary); //儲存上一次使用的MerchantTradeNo if (result.IsSuccessful) { _checkoutService.SaveMerchantTradeNo(newDictionary["MerchantTradeNo"]); } else { throw new Exception("訂單建立失敗"); } string paramsJson = JsonConvert.SerializeObject(form); //2.送回參數在綠界重新建立訂單 return(Content(paramsJson, "application/json")); }