protected void Page_Load(object sender, EventArgs e)
        {
            using (SPCheckOutApi oPayment = new SPCheckOutApi())
            {
                /* 服務參數 */
                oPayment.ServiceURL = "https://payment-stage.ecpay.com.tw/SP/CreateTrade";    //介接網址
                oPayment.HashKey    = "5294y06JbISpM5x9";                                     //HashKey
                oPayment.HashIV     = "v77hoKGq4kWxNNIS";                                     //v77hoKGq4kWxNNIS

                oPayment.Send.ItemName          = "可樂";                                       //商品名稱
                oPayment.Send.ReturnURL         = "http://127.0.0.1/01/CheckOutFeedback.php"; //付款完成通知回傳網址
                oPayment.Send.TotalAmount       = Convert.ToUInt32("31");                     //交易金額
                oPayment.Send.TradeDesc         = "test";                                     //交易描述
                oPayment.Send.MerchantID        = "2000132";                                  //合作特店編號
                oPayment.Send.NeedExtraPaidInfo = "N";                                        //額外回傳參數
                oPayment.Send.ClientBackURL     = "";                                         //Client端返回特店的按鈕



                #region ATM相關參數
                //oPayment.ATM.ExpireDate = Convert.ToInt32("0");  //允許繳費有效天數
                //oPayment.ATM.PaymentInfoURL = "<<PaymentInfoURL>>";  //Server 端回傳付款相關資訊

                #endregion

                #region CVS相關參數
                //oPayment.CVS.StoreExpireDate = Convert.ToInt32("10");  //超商繳費截止時間
                //oPayment.CVS.Desc_1 = "1";  //交易描述 1
                //oPayment.CVS.Desc_2 = "2";  //交易描述 2
                //oPayment.CVS.Desc_3 = "3";  //交易描述 3
                //oPayment.CVS.Desc_4 = "4";  //交易描述 4
                //oPayment.CVS.PaymentInfoURL = "http://127.0.0.1/01/CheckOutFeedback.php";  //Server 端回傳付款相關資訊
                #endregion

                #region 信用卡相關參數
                /*==Should Know 要先設置 CreditPayType選擇執行的信用卡付款方式==*/
                #region 信用卡一次付清
                oPayment.CreditPayment = CreditPayType.CreditPayOff;
                //oPayment.CreditPayOff.BindingCard = Convert.ToInt32("1");  //記憶卡號
                //oPayment.CreditPayOff.MerchantMemberID = "200013200001";  //記憶卡號識別碼
                //oPayment.CreditPayOff.Redeem = "N";  //是否使用紅利折抵
                #endregion

                #region 信用卡分期付款
                //oPayment.CreditPayment = CreditPayType.CreditInstallment;
                //oPayment.CreditInstallment.BindingCard = Convert.ToInt32("<<BindingCard>>");  //記憶卡號
                //oPayment.CreditInstallment.MerchantMemberID = "<<MerchantMemberID>>";  //記憶卡號識別碼
                //oPayment.CreditInstallment.CreditInstallment = "3,6,12";  //刷卡分期期數
                #endregion

                #region 信用卡定期定額
                //oPayment.CreditPayment = CreditPayType.CreditRSP;
                ////oPayment.CreditRSP.BindingCard = Convert.ToInt32("<<BindingCard>>");  ////記憶卡號
                ////oPayment.CreditRSP.MerchantMemberID = "<<MerchantMemberID>>";  ////記憶卡號識別碼
                //oPayment.CreditRSP.PeriodAmount = Convert.ToInt32("31");  //每次授權金額
                //oPayment.CreditRSP.PeriodType = "D";  //週期
                //oPayment.CreditRSP.Frequency = Convert.ToInt32("365");  //執行頻率
                //oPayment.CreditRSP.ExecTimes = Convert.ToInt32("50");  //執行次數
                //oPayment.CreditRSP.PeriodReturnURL = "https://www.ecpay.com.tw";  //定期定額的執行結果回應 URL
                #endregion

                #endregion

                //電子發票相關
                oPayment.EivoiceType = EInvoiceType.None;  //是否使用電子發票


                oPayment.Invoice.CustomerID         = "";           //客戶代號
                oPayment.Invoice.CustomerIdentifier = "";           //統一編號
                oPayment.Invoice.CustomerName       = "客戶名稱";       //客戶名稱
                oPayment.Invoice.CustomerAddr       = "客戶地址";       //客戶地址
                oPayment.Invoice.CustomerPhone      = "0911111111"; //客戶手機號碼
                oPayment.Invoice.CustomerEmail      = "*****@*****.**";   //客戶信箱
                //oPayment.Invoice.ClearanceMark = "<<ClearanceMark>>"; //通關方式
                oPayment.Invoice.TaxType = "1";                     //課稅類別
                //oPayment.Invoice.CarruerType = "<<CarruerType>>";  //載具類別
                //oPayment.Invoice.CarruerNum = "<<CarruerNum>>";  //載具編號
                oPayment.Invoice.Donation = "0";                            //捐贈註記
                //oPayment.Invoice.LoveCode = "";  //愛心碼
                oPayment.Invoice.Print              = "0";                  //是否列印
                oPayment.Invoice.InvoiceItemName    = "頻果";                 //商品名稱
                oPayment.Invoice.InvoiceItemCount   = "1";                  //商品數量
                oPayment.Invoice.InvoiceItemWord    = "顆";                  //商品單位
                oPayment.Invoice.InvoiceItemPrice   = "31";                 //注意數量*單價 要等於 oPayment.Send.TotalAmount
                oPayment.Invoice.InvoiceItemTaxType = "1";                  //商品課稅別
                oPayment.Invoice.DelayDay           = Convert.ToInt32("0"); //延遲天數
                oPayment.Invoice.InvType            = "07";                 //字軌類別

                //執行回傳結果
                string result = oPayment.Excute();

                //使用第三方套件將Json To Model
                TradeSPToken RtnModel = JsonConvert.DeserializeObject <TradeSPToken>(result);

                if (RtnModel.RtnCode != "1")
                {
                    //### 幕後建立訂單失敗,廠商自行撰寫語法
                    Response.Write(result + "<br/>");
                    Response.End();
                }

                //取得的MerchantID 給Js使用
                _MerchantID = RtnModel.MerchantID;
                //取得的SPToken  給Js使用
                _SPToken = RtnModel.SPToken;
            }
        }
        public async Task <IViewComponentResult> InvokeAsync(string MemberMobile, List <CartsViewModel> Carts, ContactDataViewModel ContactData, int CartTobal)
        {
            TradeSPToken             RtnModel = null;
            List <CartListViewModel> lstCLVM  = null;
            string OrderId = "";

            try
            {
                //檢查商品庫存
                foreach (var o in Carts)
                {
                    int stock = await IMR.MemberCartCheck(o.productId, o.sizeId, o.colorId, o.quantity);

                    if (stock != 0)
                    {
                        return(View(new PostOrderViewModel1 {
                            ret = stock, product = o.product
                        }));
                    }
                }

                // 8.系統讀取當日最後一張訂單號。
                DateTime         dt    = DateTime.Now;
                OrderIdViewModel Order = await IMR.GetNewOrderId(dt);

                if (Order != null)
                {
                    // 9.系統判斷8傳回值!=null。
                    // 10.系統設定OrderId=年月日+<8讀取值最後4碼加1>。
                    OrderId = Data.GetNewId(Order.OrderId, 9, 4);
                }
                else
                {
                    // 9a.系統判斷8傳回值==null。
                    //  9a-1.系統設定OrderId=年月日+<0201>。
                    //  9a-2.回11。
                    OrderId = Data.GetStartId("B", dt);
                }
                // 6.系統在ViewComponent【Cart/PostOrder】中建立一筆綠界訂單。
                string MerchantIDTmp = config["MerchantID"];
                string TradeDesc     = config["TradeDesc"];

                //測試環境轉正式環境

                //測試
                //string MerchantID = "2000132";
                //string HashKey = "5294y06JbISpM5x9";
                //string HashIV = "v77hoKGq4kWxNNIS";
                //string PostURL = "https://payment-stage.ecpay.com.tw/SP/CreateTrade";
                //正式
                string MerchantID = "3084097";
                string HashKey    = "AIlPEruFqmb1fZmz";
                string HashIV     = "oKf58WmB9vgevdrj";
                string PostURL    = "https://payment.ecpay.com.tw/SP/CreateTrade";

                SortedDictionary <string, string> PostCollection = new SortedDictionary <string, string>();

                PostCollection.Add("MerchantID", MerchantID);
                PostCollection.Add("MerchantTradeNo", $"{OrderId}{MerchantIDTmp}");
                //PostCollection.Add("MerchantTradeNo", DateTime.Now.ToString("yyyyMMddHHmmss"));
                //PostCollection.Add("MerchantTradeNo", "sdfg1561/");
                PostCollection.Add("MerchantTradeDate", dt.ToString("yyyy/MM/dd HH:mm:ss"));
                PostCollection.Add("PaymentType", "aio");
                PostCollection.Add("TotalAmount", CartTobal.ToString());
                PostCollection.Add("TradeDesc", TradeDesc);
                string ItemName = "";
                foreach (var item in Carts)
                {
                    ItemName += $"{item.product}{item.price}元#";
                }
                ItemName = ItemName.Substring(0, ItemName.Length - 1);
                PostCollection.Add("ItemName", ItemName);
                PostCollection.Add("ReturnURL", config.GetConnectionString("ReturnURL"));
                PostCollection.Add("PaymentInfoURL", config.GetConnectionString("PaymentInfoURL"));
                PostCollection.Add("ChoosePayment", "ALL");
                PostCollection.Add("EncryptType", "1");
                PostCollection.Add("NeedExtraPaidInfo", "N");
                PostCollection.Add("IgnorePayment", "");
                //記憶卡號
                //PostCollection.Add("BidingCard", "");
                //PostCollection.Add("MerchantMemberID", "");
                //分期付款
                //PostCollection.Add("StoreID", "");
                // PostCollection.Add("CustomField1", "");
                //PostCollection.Add("CustomField2", "");
                //PostCollection.Add("CustomField3", "");
                // PostCollection.Add("CustomField4", "");

                //定期定額
                //PostCollection.Add("PeriodAmount", "5");
                //PostCollection.Add("PeriodType", "Y");
                //PostCollection.Add("Frequency", "1");
                //PostCollection.Add("ExecTimes", "6");
                //PostCollection.Add("PeriodReturnURL", "http://127.0.0.1/01/CheckOutFeedback.php");
                //紅利折抵
                //PostCollection.Add("Redeem", "Y");
                //ATM繳費期限
                //PostCollection.Add("ExpireDate", "");
                //CVS&BARCODE繳費期限
                //PostCollection.Add("StoreExpireDate", "");
                //電子發票
                //PostCollection.Add("InvoiceMark", "N");//電子發票開立註記
                //PostCollection.Add("RelateNumber", DateTime.Now.ToString("yyyyMMddHHmmss"));  //會員自訂編號
                //PostCollection.Add("CustomerID", "");//客戶代號
                //PostCollection.Add("CustomerName", "");//客戶名稱
                //PostCollection.Add("CustomerAddr", "");//客戶地址
                //PostCollection.Add("CustomerPhone", "0912345678");//客戶手機號碼
                //PostCollection.Add("CustomerEmail", "");
                //PostCollection.Add("TaxType", "1");//課稅類別
                //PostCollection.Add("CarruerType", "");//載具類別
                //PostCollection.Add("CarruerNum", "");
                //PostCollection.Add("Donation", "2");//捐贈註記
                //PostCollection.Add("LoveCode", "");//捐贈註記
                //PostCollection.Add("Print", "0");//列印註記
                //PostCollection.Add("InvoiceItemName", "商品名稱");//商品名稱
                //PostCollection.Add("InvoiceItemCount", "1");//商品數量
                //PostCollection.Add("InvoiceItemWord", "個");//商品單位
                //PostCollection.Add("InvoiceItemPrice", "100");//商品價格
                //PostCollection.Add("InvoiceItemTaxType", "");//商品課稅別
                //PostCollection.Add("InvoiceRemark", "");//備註
                //PostCollection.Add("DelayDay", "0");//延遲天數
                //PostCollection.Add("InvType", "07");//字軌類別

                //計算檢查碼
                string str     = string.Empty;
                string str_pre = string.Empty;
                foreach (var test in PostCollection)
                {
                    str += string.Format("&{0}={1}", test.Key, test.Value);
                }

                str_pre += string.Format("HashKey={0}" + str + "&HashIV={1}", HashKey, HashIV);

                string urlEncodeStrPost = HttpUtility.UrlEncode(str_pre);
                string ToLower          = urlEncodeStrPost.ToLower();
                string sCheckMacValue   = Data.GetSHA256(ToLower);

                PostCollection.Add("CheckMacValue", sCheckMacValue);

                string Result          = "";
                string ParameterString = string.Join("&", PostCollection.Select(p => p.Key + "=" + p.Value));

                //### Server Post
                using (WebClient wc = new System.Net.WebClient())
                {
                    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    Result = wc.UploadString(PostURL, ParameterString);
                }

                //### 轉Model
                RtnModel = JsonConvert.DeserializeObject <TradeSPToken>(Result);

                if (RtnModel.RtnCode != "1")
                {
                    // 7a.系統判斷6執行失敗。
                    //  7a-1.系統回傳null。
                    RtnModel = null;
                }

                // 7.系統判斷6成功執行。
                lstCLVM = new List <CartListViewModel>();
                CartListViewModel CLVM;
                if (MemberMobile != "" && MemberMobile != null)
                {
                    // 11.系統判斷MemberMobile!=""。
                    // 12.系統讀取購物車清單。
                    lstCLVM = await IMER.GetCartList(MemberMobile);

                    foreach (var item in lstCLVM)
                    {
                        item.OrderId = OrderId;
                    }
                }
                else
                {
                    // 11a.系統判斷MemberMobile==""。
                    //  11a-1.系統將carts轉換為List<CartListViewModel>。
                    foreach (CartsViewModel item in Carts)
                    {
                        CLVM                = new CartListViewModel();
                        CLVM.ProductId      = item.productId;
                        CLVM.Product        = item.product;
                        CLVM.ProducSizeId   = item.sizeId;
                        CLVM.ProductColorId = item.colorId;
                        CLVM.Quantity       = item.quantity;
                        CLVM.Price          = item.price;
                        CLVM.ProductSize    = item.size;
                        CLVM.ProductColor   = item.color;
                        CLVM.OrderId        = OrderId;
                        lstCLVM.Add(CLVM);
                    }
                    //  11a-2.回13。
                }
                // 13.系統新增一筆內部訂單。
                int ret = await IMR.InsertOrder(OrderId, dt, MemberMobile, ContactData.Freight, CartTobal - ContactData.Freight, CartTobal, ContactData.MemberName, ContactData.eMail, ContactData.Phone, ContactData.ContactAddress, sCheckMacValue, lstCLVM, $"{OrderId}{MerchantIDTmp}", ContactData.MemberMobile);

                if (ret == 0)
                {
                    // 14.系統判斷13執行成功。
                    // 15.系統設定傳回值=12讀取值。
                }
                else
                {
                    // 14a.系統判斷13執行失敗。
                    //  14a-1.系統設定傳回Model=null。
                    lstCLVM  = null;
                    RtnModel = null;
                    //  14a-2.回16。
                }
            }
            catch (Exception ex)
            {
                RtnModel = null;
            }
            // 16.系統回傳View【Cart/PostOrder】,並傳回new PostOrderViewModel1
            //  { OrderDetail=15設定傳回值, ContactData=contactData, CartTobal= cartTobal, OrderId=10設定OrderId, RtnModel=金流公司的TradeSPToken }。
            return(View(new PostOrderViewModel1 {
                OrderDetail = lstCLVM, ContactData = ContactData, CartTobal = CartTobal, OrderId = OrderId, RtnModel = RtnModel
            }));
        }