public async Task <IActionResult> Post([FromBody] NewQRReq reqForm) { _logger.LogInformation("start request new QR service"); DBModels dbModel = new DBModels(); //validate TransId string transIdResult = await dbModel.checkTransId(reqForm.UserID, reqForm.TransId); if (transIdResult != "") { return(Ok(new NewQR_Fail() { Msg = transIdResult })); } //TODO: Check promotion code List <ProductDetail> prodDetail = await dbModel.GetProductDetailFromProductList(reqForm.Company, reqForm.Product); if (prodDetail.Count == 0) { return(Ok(new NewQR_Fail() { Msg = "Invalid Company or/and Product" })); } if (reqForm.NewQty <= 0) { return(Ok(new NewQR_Fail() { Msg = "Invalid new quantity" })); } Double discount = 0; if (reqForm.PromotionCode.ToUpper() == "50% OFF") { discount = 0.5; } double ttlAmt = System.Math.Round(prodDetail[0].UnitPrice * reqForm.NewQty * (1 - discount), 2);//String.Format("{0:0.00}", 123.4567); -->123.45 DateTime transTime = DateTime.Now; //getToken var s = await authService.getToken(); var data1 = JsonConvert.DeserializeObject <Dictionary <string, string> >(await authService.getToken()); string token = (data1["access_token"] ?? "").ToString(); if (token == "") { return(Unauthorized()); } //check balance if (!(await eWalletAPI.checkEWalletBalanceAsync(token, reqForm.UserID, ttlAmt))) { return(Ok(new NewQR_Fail() { Msg = "Not enough balance" })); } string newQR = ""; if (reqForm.ReqType == "Submit") { //deduct balance if (!(await eWalletAPI.deductEWalletBalanceAsync(token, reqForm.UserID, ttlAmt, reqForm.TransId, reqForm.Product, reqForm.Company, reqForm.Remark))) { return(Ok(new NewQR_Fail() { Msg = "Deduct amount fail" })); } await dbModel.InitNewQRCode(reqForm.UserID, reqForm.TransId, transTime, reqForm.NewQty, ttlAmt, reqForm.PromotionCode); //get new QR string reqResult = await iJoozAPI.reqQR("New", reqForm.TransId, reqForm.TransTime, "member", reqForm.UserID, "-", prodDetail[0].Currency, prodDetail[0].UnitPrice, reqForm.NewQty, DateTime.Now.AddMonths(24)); if (reqResult == "") { return(Ok(new NewQR_Fail() { Msg = "Get QR Code from iJooz Fail" })); } else { var data = JsonConvert.DeserializeObject <Dictionary <string, string> >(reqResult); if (data["result"] == "Success") { newQR = data["qrCode"]; } else { return(Ok(new NewQR_Fail() { Msg = "Get QR Code from iJooz Fail2" })); } } await dbModel.SaveNewQRCode(reqForm.UserID, reqForm.TransId, newQR, reqForm.Company, reqForm.NewQty, reqForm.Product, prodDetail[0].Currency, prodDetail[0].UnitPrice, DateTime.Now.AddYears(10)); } else if (reqForm.ReqType != "Preview") { return(Ok(new NewQR_Fail() { Msg = "Invalid Request Type" })); } List <NewQRDetail> detailList = new List <NewQRDetail> { new NewQRDetail() { TransId = reqForm.TransId, ReqType = reqForm.ReqType, Company = reqForm.Company, Product = reqForm.Product, QRCode = newQR, TtlQty = reqForm.NewQty, Currency = prodDetail[0].Currency, Amt = ttlAmt, ExpiredDate = DateTime.Now.AddYears(10) } }; return(Ok(new NewQR_OK() { QrDetail = detailList })); }
public async Task<IActionResult> Post([FromBody] RegenQRReq reqForm) { _logger.LogInformation("start gen QR"); DBModels dbModel = new DBModels(); //validate TransId string transIdResult = await dbModel.checkTransId(reqForm.UserID, reqForm.TransId); if (transIdResult != "") { return Ok(new RegenQR_Fail() { Msg = transIdResult }); } List<QrBalance> prodDetail = await dbModel.GetProductDetailFromQR_Status(reqForm.UserID, reqForm.QRCode); if (prodDetail.Count == 0) { return Ok(new RegenQR_Fail() { Msg = "Invalid QR Code" }); } DateTime transTime = DateTime.Now; await dbModel.InitRegenQRCode(reqForm.UserID, reqForm.QRCode, reqForm.TransId, transTime); //Regen QR (need to get new ttl qty & new expirydate) string NewQRCode = ""; int balanceQty = 0; string reqResult = await iJoozAPI.reqQR("Regen", reqForm.TransId, transTime.ToString("yyyy-MM-dd HH:mm:ss"), "member", reqForm.UserID, reqForm.QRCode, prodDetail[0].Currency, prodDetail[0].UnitPrice, 0, transTime.AddMonths(24)); if (reqResult == "") { return Ok(new RegenQR_Fail() { Msg = "Get QR Code from iJooz Fail" }); } else { var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(reqResult); if (data["result"] == "Success") { NewQRCode = data["qrCode"]; balanceQty = Convert.ToInt16(data["balanceQty"]); } else { return Ok(new RegenQR_Fail() { Msg = "Get QR Code from iJooz Fail2" }); } } await dbModel.SaveRegenQRCode(reqForm.UserID, reqForm.TransId, reqForm.QRCode, NewQRCode, balanceQty); List<QrDetail> detailList = new List<QrDetail> { new QrDetail() { TransId=reqForm.TransId, NewQRCode=NewQRCode, TtlQty=balanceQty, ExpiryDate=prodDetail[0].ExpiryDate } }; return Ok(new RegenQR_OK() { QrDetail = detailList }); }
public async Task <IActionResult> Post([FromBody] ShareQRReq reqForm) { _logger.LogInformation("start share QR balance"); DBModels dbModel = new DBModels(); //validate TransId string transIdResult = await dbModel.checkTransId(reqForm.UserID, reqForm.TransId); if (transIdResult != "") { return(Ok(new ShareQR_Fail() { Msg = transIdResult })); } //get product info based on QR List <QrBalance> prodDetail = await dbModel.GetProductDetailFromQR_Status(reqForm.UserID, reqForm.QRCode); if (prodDetail.Count == 0) { return(Ok(new ShareQR_Fail() { Msg = "Invalid QR Code" })); } if (reqForm.SharedQty <= 0) { return(Ok(new ShareQR_Fail() { Msg = "Invalid shared quantity" })); } else if (reqForm.SharedQty > prodDetail[0].Qty) { return(Ok(new ShareQR_Fail() { Msg = "Shared quantity can't be higher than available quantity" })); } DateTime transTime = DateTime.Now; int ttlQtyAfterShare = prodDetail[0].Qty - reqForm.SharedQty; DateTime ExpiryDate = prodDetail[0].ExpiryDate; await dbModel.InitShareQRCode(reqForm.UserID, reqForm.QRCode, reqForm.TransId, transTime, reqForm.SharedQty); //Share QR (need to get new ttl qty & new expirydate) string SharedQRCode = ""; string reqResult = await iJoozAPI.reqQR("Share", reqForm.TransId, reqForm.TransTime, "member", reqForm.UserID, reqForm.QRCode, prodDetail[0].Currency, prodDetail[0].UnitPrice, reqForm.SharedQty, DateTime.Now.AddMonths(24)); if (reqResult == "") { return(Ok(new ShareQR_Fail() { Msg = "Get QR Code from iJooz Fail" })); } else { var data = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(reqResult); if (data["result"] == "Success") { SharedQRCode = data["qrCode"]; ttlQtyAfterShare = Convert.ToInt16(data["balanceQty"]); } else { return(Ok(new ShareQR_Fail() { Msg = "Get QR Code from iJooz Fail2" })); } } await dbModel.SaveShareQRCode(reqForm.UserID, reqForm.TransId, reqForm.QRCode, ttlQtyAfterShare, SharedQRCode, reqForm.SharedQty, reqForm.Remark); List <QrDetail> detailList = new List <QrDetail> { new QrDetail() { TransId = reqForm.TransId, QRCode = prodDetail[0].QRCode, RemainQty = ttlQtyAfterShare, SharedQRCode = SharedQRCode, SharedQty = reqForm.SharedQty, ExpiryDate = prodDetail[0].ExpiryDate, Remark = reqForm.Remark } }; return(Ok(new ShareQR_OK() { QrDetail = detailList })); }
public async Task <IActionResult> Post([FromBody] TopUpQRReq reqForm) { _logger.LogInformation("start share QR balance"); DBModels dbModel = new DBModels(); //validate TransId string transIdResult = await dbModel.checkTransId(reqForm.UserID, reqForm.TransId); if (transIdResult != "") { return(Ok(new TopUpQR_Fail() { Msg = transIdResult })); } //get product info based on QR List <QrBalance> prodDetail = await dbModel.GetProductDetailFromQR_Status(reqForm.UserID, reqForm.QRCode); if (prodDetail.Count == 0) { return(Ok(new TopUpQR_Fail() { Msg = "Invalid QR Code" })); } if (reqForm.TopUpQty <= 0) { return(Ok(new TopUpQR_Fail() { Msg = "Invalid new quantity" })); } //TODO: update promotion Double discount = 0; if (reqForm.PromotionCode.ToUpper() == "50% OFF") { discount = 0.5; } double ttlAmt = System.Math.Round(prodDetail[0].UnitPrice * reqForm.TopUpQty * (1 - discount), 2);//String.Format("{0:0.00}", 123.4567); -->123.45 //getToken var s = await authService.getToken(); var data1 = JsonConvert.DeserializeObject <Dictionary <string, string> >(await authService.getToken()); string token = (data1["access_token"] ?? "").ToString(); if (token == "") { return(Unauthorized()); } //check balance if (!(await eWalletAPI.checkEWalletBalanceAsync(token, reqForm.UserID, ttlAmt))) { return(Ok(new NewQR_Fail() { Msg = "Not enough balance" })); } DateTime transTime = DateTime.Now; int ttlQtyAfterTopUp = reqForm.TopUpQty + prodDetail[0].Qty; DateTime ExpiryDate = DateTime.Now.AddYears(10); if (reqForm.ReqType == "Submit") { await dbModel.InitTopUpQRCode(reqForm.UserID, reqForm.QRCode, reqForm.TransId, transTime, reqForm.TopUpQty, ttlAmt, reqForm.PromotionCode); //deduct balance if (!(await eWalletAPI.deductEWalletBalanceAsync(token, reqForm.UserID, ttlAmt, reqForm.TransId, prodDetail[0].Product, prodDetail[0].Company, reqForm.Remark))) { return(Ok(new NewQR_Fail() { Msg = "Deduct amount fail" })); } //topup QR (need to get new ttl qty & new expirydate) string reqResult = await iJoozAPI.reqQR("TopUp", reqForm.TransId, reqForm.TransTime, "member", reqForm.UserID, reqForm.QRCode, prodDetail[0].Currency, prodDetail[0].UnitPrice, reqForm.TopUpQty, DateTime.Now.AddMonths(24)); if (reqResult == "") { return(Ok(new NewQR_Fail() { Msg = "Get QR Code from iJooz Fail" })); } else { var data = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(reqResult); if (data["result"] == "Success") { ttlQtyAfterTopUp = Convert.ToInt16(data["balanceQty"]); } else { return(Ok(new NewQR_Fail() { Msg = "Get QR Code from iJooz Fail2" })); } } await dbModel.SaveTopUpQRCode(reqForm.UserID, reqForm.TransTime, reqForm.QRCode, ttlQtyAfterTopUp, ExpiryDate); } else if (reqForm.ReqType != "Preview") { return(Ok(new TopUpQR_Fail() { Msg = "Invalid Request Type" })); } List <TopUpQrDetail> detailList = new List <TopUpQrDetail> { new TopUpQrDetail() { TransId = reqForm.TransId, ReqType = reqForm.ReqType, Company = prodDetail[0].Company, Product = prodDetail[0].Product, QRCode = prodDetail[0].QRCode, TopUpQty = reqForm.TopUpQty, TtlQty = ttlQtyAfterTopUp, Currency = prodDetail[0].Currency, Amt = ttlAmt, ExpiryDate = ExpiryDate } }; return(Ok(new TopUpQR_OK() { QrDetail = detailList })); }