コード例 #1
0
        public IHttpActionResult SpgatewayNotify()
        {
            // 取法同SpgatewayResult
            var httpRequestBase = new HttpRequestWrapper(HttpContext.Current.Request);

            RazorExtensions.LogFormData(httpRequestBase, "SpgatewayNotify(支付完成)");
            // Status 回傳狀態
            // MerchantID 回傳訊息
            // TradeInfo 交易資料AES 加密
            // TradeSha 交易資料SHA256 加密
            // Version 串接程式版本
            NameValueCollection collection = HttpContext.Current.Request.Form;

            if (collection["MerchantID"] != null && string.Equals(collection["MerchantID"], _bankInfoModel.MerchantID) &&
                collection["TradeInfo"] != null && string.Equals(collection["TradeSha"], CryptoUtil.EncryptSHA256($"HashKey={_bankInfoModel.HashKey}&{collection["TradeInfo"]}&HashIV={_bankInfoModel.HashIV}")))
            {
                var decryptTradeInfo = CryptoUtil.DecryptAESHex(collection["TradeInfo"], _bankInfoModel.HashKey, _bankInfoModel.HashIV);

                // 取得回傳參數(ex:key1=value1&key2=value2),儲存為NameValueCollection
                NameValueCollection      decryptTradeCollection = HttpUtility.ParseQueryString(decryptTradeInfo);
                SpgatewayOutputDataModel convertModel           = LambdaUtil.DictionaryToObject <SpgatewayOutputDataModel>(decryptTradeCollection.AllKeys.ToDictionary(k => k, k => decryptTradeCollection[k]));

                //LogUtil.WriteLog(JsonConvert.SerializeObject(convertModel));

                // TODO 將回傳訊息寫入資料庫

                VipOrder pay = db.VipOrders.Find(Convert.ToInt32(convertModel.MerchantOrderNo));
                pay.Message = convertModel.Message;
                pay.Status  = convertModel.Status;
                db.VipOrders.Add(pay);
                // return Content(JsonConvert.SerializeObject(convertModel));

                if (pay.Status == "SUCCESS")
                {
                    //MerchantOrderNo = "O202009100034"
                    //convertModel.MerchantOrderNo
                    VipOrder vip = db.VipOrders.Find(pay.BrandId);
                    vip.Status = "已付款成功";

                    db.Entry(vip).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else//付款失敗
                {
                }
                return(Ok());
            }
            else
            {
                //LogUtil.WriteLog("MerchantID/TradeSha驗證錯誤");
            }

            return(Ok());
        }
コード例 #2
0
ファイル: PayController.cs プロジェクト: ga503306/pet
        public IHttpActionResult SpgatewayNotify()
        {
            // 取法同SpgatewayResult
            var httpRequestBase = new HttpRequestWrapper(HttpContext.Current.Request);

            RazorExtensions.LogFormData(httpRequestBase, "SpgatewayNotify(支付完成)");
            // Status 回傳狀態
            // MerchantID 回傳訊息
            // TradeInfo 交易資料AES 加密
            // TradeSha 交易資料SHA256 加密
            // Version 串接程式版本
            NameValueCollection collection = HttpContext.Current.Request.Form;

            if (collection["MerchantID"] != null && string.Equals(collection["MerchantID"], _bankInfoModel.MerchantID) &&
                collection["TradeInfo"] != null && string.Equals(collection["TradeSha"], CryptoUtil.EncryptSHA256($"HashKey={_bankInfoModel.HashKey}&{collection["TradeInfo"]}&HashIV={_bankInfoModel.HashIV}")))
            {
                var decryptTradeInfo = CryptoUtil.DecryptAESHex(collection["TradeInfo"], _bankInfoModel.HashKey, _bankInfoModel.HashIV);

                // 取得回傳參數(ex:key1=value1&key2=value2),儲存為NameValueCollection
                NameValueCollection      decryptTradeCollection = HttpUtility.ParseQueryString(decryptTradeInfo);
                SpgatewayOutputDataModel convertModel           = LambdaUtil.DictionaryToObject <SpgatewayOutputDataModel>(decryptTradeCollection.AllKeys.ToDictionary(k => k, k => decryptTradeCollection[k]));

                //LogUtil.WriteLog(JsonConvert.SerializeObject(convertModel));

                // TODO 將回傳訊息寫入資料庫

                // return Content(JsonConvert.SerializeObject(convertModel));

                if (convertModel.Status == "SUCCESS")
                {
                    //MerchantOrderNo = "O202009100034"
                    //convertModel.MerchantOrderNo
                    Order order = db.Order.Find(convertModel.MerchantOrderNo);
                    order.state           = 1;
                    db.Entry(order).State = EntityState.Modified;
                    db.SaveChanges();

                    //signalr即時通知 改寫法
                    Utility.signalR_notice(order.memberseq, order.companyseq, order.orderseq, "", Noticetype.單通知);
                    var context   = GlobalHost.ConnectionManager.GetHubContext <DefaultHub>();
                    var connectid = db.Signalr.Where(x => x.whoseq == order.companyseq).Select(x => x.connectid).ToList();//需要通知的廠商signalr connectid
                    var notices   = db.Notice.Where(x => x.toseq == order.companyseq).ToList();
                    var unread    = notices.Where(x => x.state == Convert.ToBoolean(Noticestate.未讀)).Count();

                    List <Notice> notices_ = notices.OrderBy(x => x.state).ThenByDescending(x => x.postday).Take(10).ToList();
                    var           result   = new
                    {
                        unread  = unread,
                        notices = notices_.Select(
                            x => new
                        {
                            x.noticeseq,
                            x.fromseq,
                            x.toseq,
                            state = Enum.Parse(typeof(Noticestate), x.state.GetHashCode().ToString()).ToString(),
                            x.text,
                            type = Enum.Parse(typeof(Noticetype), x.type.ToString()).ToString(),
                            time = Convert.ToDateTime(x.postday).ToString("yyyy-MM-dd HH:mm")
                        })
                    };
                    foreach (var c in connectid)
                    {
                        context.Clients.Client(c).Get(result);
                    }

                    ////signalr即時通知
                    //Utility.signalR_notice(order.memberseq, order.companyseq, order.orderseq, "", Noticetype.下單通知);
                    //var context = GlobalHost.ConnectionManager.GetHubContext<DefaultHub>();
                    //var connectid = db.Signalr.Where(x => x.whoseq == order.companyseq).Select(x => x.connectid).ToList();//需要通知的廠商signalr connectid
                    //foreach (var c in connectid)
                    //{
                    //    context.Clients.Client(c).Get();
                    //}
                }
                else//付款失敗
                {
                }
                return(Ok());
            }
            else
            {
                //LogUtil.WriteLog("MerchantID/TradeSha驗證錯誤");
            }

            return(Ok());
        }