public void ValidateTest()
        {
            var target = new SpGatewayResponse()
            {
                MerchantId = "AAA",
                Status     = "SUCCESS",
                Version    = "1.4",
                TradeInfo  = "ff91c8aa01379e4de621a44e5f11f72e4d25bdb1a18242db6cef9ef07d80b0165e476fd1d9acaa53170272c82d122961e1a0700a7427cfa1cf90db7f6d6593bbc93102a4d4b9b66d9974c13c31a7ab4bba1d4e0790f0cbbbd7ad64c6d3c8012a601ceaa808bff70f94a8efa5a4f984b9d41304ffd879612177c622f75f4214fa",
                TradeSha   = "EA0A6CC37F40C1EA5692E7CBB8AE097653DF3E91365E6A9CD7E91312413C7BB8",
                Key        = "12345678901234567890123456789012",
                Vi         = "1234567890123456"
            };

            Assert.AreEqual(false, target.Validate("asb"));
            Assert.AreEqual(true, target.Validate("AAA"));
        }
        public ActionResult bluenewRequest(SpGatewayResponse response)
        {
            // Product productModel = new MallRepository().Get(pm);
            //User.Identity.GetUserId()

            /* var user = UserManager.FindById(User.Identity.GetUserId());
             *
             *   ProductRecord assr = new ProductRecord
             *   {
             *       UserID = User.Identity.GetUserId(),
             *       unitSn = 1,
             *       assets = (double)productModel.transform,
             *       inpdate = DateTime.Now,
             *       type = 1,
             *       ProductId = pm
             *   };
             *   new AssetsRepository().AddBearByAssets(assr);*/
            //new AssetsRepository().AddAssetsByAssets(AssetsRecord ar)
            ///return Json(result, "application/json", JsonRequestBehavior.AllowGet);
            ///
            //var json = DecryptAES256(TradeInfo.ToString());

            response.Key = "CON3KthrvPulsAWQQiQ3jsswLIzxxgQK";
            response.Vi  = "ugZbqRhI6x5LGI94";
            var success = response.Validate("MS15822085");

            if (success)
            {
                var tradInfoModel = response.GetResponseModel <TradeInfoModel>();
                //var OrderNoToProuctId = tradInfoModel.Result.MerchantOrderNo.OrderNoToProuctId();

                User_CashReturn user         = new MallRepository().GetUserPRecord(tradInfoModel.Result.MerchantOrderNo);
                Product         productModel = new MallRepository().Get((int)user.productId);


                ProductRecord assr = new ProductRecord
                {
                    UserID    = user.userId,
                    unitSn    = 1,
                    assets    = productModel.transform,
                    inpdate   = DateTime.Now,
                    type      = 1,
                    ProductId = productModel.id
                };
                new AssetsRepository().AddBearByAssets(assr);


                /* User_CashReturn re = new User_CashReturn
                 * {
                 *   Status = response.Status,
                 *   MerchantID = OrderNoToProuctId,
                 *   TradeInfo = response.TradeInfo,
                 *   TradeSha = response.TradeSha,
                 *   Version = "1.5"
                 * };
                 *
                 * new MallRepository().CreateReturnRecord(re);*/
                //return Json(re, "application/json", JsonRequestBehavior.AllowGet);
            }
            return(View());

            // var json = DecryptAES256(Tradeinfo);
            //string jsonString = JsonConvert.SerializeObject(json, Formatting.Indented, new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii });

            //var json = new string[]{ "123" };


            //return Json();
        }
        public ActionResult bluenewRequest(SpGatewayResponse response)
        {
            //正式
            response.Vi  = "PmNER6HP23jikkcC";
            response.Key = "382go6Z9UrDy3XBuJnCHzNusEYFnBfls";

            //test
            //response.Key = "CON3KthrvPulsAWQQiQ3jsswLIzxxgQK";
            //response.Vi = "ugZbqRhI6x5LGI94";

            //test
            //var success = response.Validate("MS15822085");

            //正式
            var success = response.Validate("MS3276146654");

            if (success)
            {
                var tradInfoModel = response.GetResponseModel <TradeInfoModel>();
                var resuccess     = true;
                //var OrderNoToProuctId = tradInfoModel.Result.MerchantOrderNo.OrderNoToProuctId();
                var wait = new InvoiceRepository().GetWait(tradInfoModel.Result.MerchantOrderNo);
                var inv  = new InvoiceRepository().GetInvoice(wait.id);
                if (inv != null)
                {
                    resuccess = false;
                }

                if (resuccess)
                {
                    User_CashReturn user         = new MallRepository().GetUserPRecord(tradInfoModel.Result.MerchantOrderNo);
                    Product         productModel = new MallRepository().Get((int)user.productId);
                    Order           order        = new Order
                    {
                        UserId            = user.userId,
                        ProductId         = productModel.id,
                        Pay               = tradInfoModel.Result.PaymentType,
                        Order_No          = tradInfoModel.Result.MerchantOrderNo,
                        PayStore          = tradInfoModel.Result.PayStore,
                        Barcode_1         = tradInfoModel.Result.Barcode_1,
                        Barcode_2         = tradInfoModel.Result.Barcode_2,
                        Barcode_3         = tradInfoModel.Result.Barcode_3,
                        PayBankCode       = tradInfoModel.Result.PayBankCode,
                        CodeNo            = tradInfoModel.Result.CodeNo,
                        PayerAccount5Code = tradInfoModel.Result.PayerAccount5Code,
                        inpdate           = tradInfoModel.Result.PayTime
                    };

                    //加入購物記錄
                    var reutrnorder = new MallRepository().OrderCreate(order);



                    ProductRecord assr = new ProductRecord
                    {
                        UserID    = user.userId,
                        unitSn    = 1,
                        assets    = productModel.transform,
                        inpdate   = DateTime.Now,
                        type      = 1,
                        ProductId = productModel.id
                    };
                    new AssetsRepository().AddBearByAssets(assr);

                    var pr = new PreferentialRepository().getPRecordsAll(user.userId).Where(x => x.PreferentialID == 5 && x.productID == productModel.id).FirstOrDefault();
                    //2倍

                    /*
                     * if (pr.Count != 0)
                     * {
                     *  assr = new ProductRecord
                     *  {
                     *      UserID = user.userId,
                     *      unitSn = 1,
                     *      assets = productModel.transform,
                     *      inpdate = DateTime.Now,
                     *      type = 5,
                     *      ProductId = productModel.id
                     *  };
                     *  new AssetsRepository().AddBearByAssets(assr);
                     *  var prds = new PreferentialRecords
                     *  {
                     *      id = pr.id,
                     *      UserId = user.userId,
                     *      Count = 0,
                     *      inpdate = DateTime.Now,
                     *      PreferentialID = pr.PreferentialID,
                     *      productID = pr.productID
                     *  };
                     *  new PreferentialRepository().PRecordsUpdate(prds);
                     * }*/



                    //普通首儲

                    /*if(pr.Count != 0)
                     * {
                     *  assr = new ProductRecord
                     *  {
                     *      UserID = user.userId,
                     *      unitSn = 1,
                     *      assets = new PreferentialRepository().getpreferential(1).assets,
                     *      inpdate = DateTime.Now,
                     *      type = 5,
                     *      ProductId = productModel.id
                     *  };
                     *  new AssetsRepository().AddBearByAssets(assr);
                     *  var prds = new PreferentialRecords {
                     *      id = pr.id,
                     *      UserId = user.userId,
                     *      Count = 0,
                     *      inpdate = DateTime.Now,
                     *      PreferentialID = 1
                     *  };
                     *  new PreferentialRepository().PRecordsUpdate(prds);
                     * }*/
                    //InvoiceModel invoice = new InvoiceModel();
                    //發送發票
                    var smilePayEinvoice = new InvoiceRepository().invoiceSend(tradInfoModel.Result.MerchantOrderNo, productModel);

                    Invoice invoice = new Invoice
                    {
                        invoiceNumber = smilePayEinvoice.InvoiceNumber,
                        RandomNumber  = smilePayEinvoice.RandomNumber,
                        inpdate       = DateTime.Parse(smilePayEinvoice.InvoiceDate + " " + smilePayEinvoice.InvoiceTime),
                        IwaitId       = wait.id,
                        CarrierID     = smilePayEinvoice.CarrierID,
                        orderId       = reutrnorder.id
                    };


                    new InvoiceRepository().InvoiceCreate(invoice);
                }
            }
            return(View());
        }