public void GetResponseModelTest()
        {
            var target = new SpGatewayResponse()
            {
                MerchantId = "MS31756909",
                Status     = "SUCCESS",
                Version    = "1.4",
                TradeInfo  = "9d9d94de7cdaa5c73ba288bb9fafac953691b4e72d865e4e098d7e96eded1122d4accc05946ccec051dd621b070cad3a7ff98656178bcfebbbd76b6fe11684476a0d4815c67195694106b33f6a922398047291f5775c2202553896d4b6abbdf5df5e0e86af72ae9b41329657b4e23dd9ec287464186928f30ee0fd859ed96c6982ccae7836102e97402699a2c780a08fa564a04bda3e7ca305efba44f535f0dbb4f54bea55fc56512a97910fd13712ea046d7c411ce2385fba959241899dbc60eeb4ebe54fe76a14087e3e979217ecfb111406b1c64c6f4a66186fab9eb0c4566909a128d6743e2bf560ce699eaa97a01b983be4b09dc706e69927d271443a3d3bb23789991893043c5c929d6f2586d82f920eb7c6075efa4d3019b13daca3f456448b4d8da93d6243962a062c70c603dea5c1ef60df97b3185302a83bc512bf43462321eeae92f064b2d33c8d2c434e3fbc83b3cad22f6b48f2b690395a0f08",
                TradeSha   = "0F271B65D055B3264D6C6C00FD6FDCC6BC7F653F8DAC38399C158F54AB2375B6",
                Key        = "xbEznJ0PExvWzd5ct72dLPCMPIBUw8K3",
                Vi         = "kpDI8DxNb2JKgOXK"
            };
            var actual = target.GetResponseModel <TradeInfoModel>();

            var expected = new TradeInfoModel
            {
                Status  = "SUCCESS",
                Message = "\u4ed8\u6b3e\u6210\u529f",
                Result  = new Result
                {
                    MerchantID      = "MS31756909",
                    Amt             = 900,
                    TradeNo         = "17060802213088297",
                    MerchantOrderNo = "636324851460397211",
                    RespondType     = "JSON",
                    IP                = "180.217.182.136",
                    EscrowBank        = "Esun",
                    PaymentType       = "WEBATM",
                    PayTime           = "2017-06-08 02:21:30",
                    PayerAccount5Code = "12345",
                    PayBankCode       = "808"
                }
            };

            expected.ShouldBeEquivalentTo(actual);
        }
        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());
        }