| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- using LotteryWebApp.Common;
- using LotteryWebApp.Components;
- using LotteryWebApp.Extensions;
- using LotteryWebApp.Languages;
- using LotteryWebApp.Models;
- using LotteryWebApp.Service;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Net.Sockets;
- using static LotteryWebApp.Common.Constants;
- using static Microsoft.AspNetCore.Razor.Language.TagHelperMetadata;
- namespace LotteryWebApp.Controllers
- {
- [AutoValidateAntiforgeryToken]
- public class BuyTicketController : BaseController
- {
- private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
- IConfiguration configuration;
- private readonly IWebHostEnvironment webHostEnvironment;
- APIFunctions api = new APIFunctions();
- public BuyTicketController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment)
- {
- configuration = _configuration;
- webHostEnvironment = hostEnvironment;
- }
- public String GetParameter(String key)
- {
- return configuration.GetSection(key).Value;
- }
- public IActionResult Index(string termType)
- {
- if (!CheckAuthToken())
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- HttpContext.Session.SetComplexData("navigator", Constants.BUY_TICKET_NAVIGATOR);
- BuyTicket_ViewModel model = new BuyTicket_ViewModel();
- model.termType = termType != null ? termType : Constants.GameId.Direct4D;
- HttpContext.Session.SetComplexData("termType", model.termType);
- return View(model);
- }
- [ValidateAntiForgeryToken]
- public IActionResult LastTermResult(string termType, String seqPage)
- {
- if (!CheckAuthToken())
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- LastTermResult_ViewModel model = new LastTermResult_ViewModel();
- model.listTerm = new List<Term>();
- try
- {
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- String token = HttpContext.Session.GetComplexData<String>("token");
- string fromDate = DateTime.Now.ToString("dd/MM/yyyy");
- string toDate = DateTime.Now.AddDays(7).ToString("dd/MM/yyyy");
- if (seqPage == "1")
- {
- HttpContext.Session.DeleteComplexData("listTerms");
- }
- // get result
- ResultOfTermRequest resultOfTermRequest = new ResultOfTermRequest();
- resultOfTermRequest.gameId = termType;
- resultOfTermRequest.token = token;
- resultOfTermRequest.type = Constants.TERM_HAS_NOT_RESULT_TYPE;
- resultOfTermRequest.order = Constants.DECS;
- resultOfTermRequest.fromDate = fromDate;
- resultOfTermRequest.toDate = toDate;
- resultOfTermRequest.rowsOnPage = Constants.ROW_ON_PAGE;
- resultOfTermRequest.seqPage = seqPage;
- resultOfTermRequest.id = Constants.ALL_DATA;
- ResultOfTermResponse result = api.GetResultOfTermApi(configuration, resultOfTermRequest);
- if (result.responseCode == Code.SESSION_EXPIRED)
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- else if (result.responseCode == Code.SUCCESS)
- {
- //if (result.listTerm != null)
- model.listTermNotDrawn = result.listTerm;
- model.termType = termType;
- }
- //resultOfTermRequest.type = Constants.TERM_HAS_RESULT_TYPE;
- //result = api.GetResultOfTermApi(configuration, resultOfTermRequest);
- //if (result.responseCode == Code.SESSION_EXPIRED)
- //{
- // return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- //}
- //else if (result.responseCode == Code.SUCCESS)
- //{
- // if (result.listTerm != null)
- // {
- // model.listTerm = result.listTerm;
- // //if (termType == Constants.BOULCHANS_CODE)
- // //{
- // // List<Term> listTerms = HttpContext.Session.GetComplexData<List<Term>>("listTerms");
- // // if (listTerms != null && listTerms.Count > 0)
- // // {
- // // listTerms.AddRange(result.listTerm);
- // // }
- // // else
- // // {
- // // listTerms = result.listTerm;
- // // }
- // // HttpContext.Session.SetComplexData("listTerms", listTerms);
- // //}
- // }
- //}
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return PartialView("LastTermResult", model);
- }
- public IActionResult Choose(string ticketType, string termType)
- {
- if (!CheckAuthToken())
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- BuyTicketChoose_ViewModel model = new BuyTicketChoose_ViewModel();
- if (ticketType == null)
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Home");
- }
- if (termType != null)
- {
- HttpContext.Session.SetComplexData("termType", termType);
- }
- model.ticketType = ticketType;
- return View(model);
- }
- [ValidateAntiForgeryToken]
- public IActionResult AddTicket_Action(string ticketType)
- {
- ChooseFormModel model = new ChooseFormModel();
- model.tics = DateTime.Now.Ticks;
- model.ticketType = ticketType;
- return ViewComponent("ChooseForm", model);
- }
- [ValidateAntiForgeryToken]
- public JsonResult ConfirmTicket_Action(string ticketType, string tickets)
- {
- try
- {
- // check tickets
- int check = 1;
- string[] ticketsList = tickets.Split(',');
- for (int i = 0; i < ticketsList.Length; i++)
- {
- if (ticketType == Constants.TicketType._2D && ticketsList[i].Length != 2)
- {
- check *= 0;
- }
- if (ticketType == Constants.TicketType._3D && ticketsList[i].Length != 3)
- {
- check *= 0;
- }
- if (ticketType == Constants.TicketType._4D && ticketsList[i].Length != 4)
- {
- check *= 0;
- }
- }
- if (check == 1)
- {
- HttpContext.Session.SetComplexData("ticketType", ticketType);
- HttpContext.Session.SetComplexData("tickets", tickets);
- return Json(new
- {
- code = Code.SUCCESS,
- });
- }
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.ticket_invalid
- });
- }
- public IActionResult Payment(string step)
- {
- if (!CheckAuthToken())
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- BuyTicketPayment_ViewModel model = new BuyTicketPayment_ViewModel();
- Profile profile = HttpContext.Session.GetComplexData<Profile>("profile");
- String tickets = HttpContext.Session.GetComplexData<String>("tickets");
- String ticketType = HttpContext.Session.GetComplexData<String>("ticketType");
- String termType = HttpContext.Session.GetComplexData<String>("termType");
- ConfirmTicketDataResponse confirmTicketDataResponse = HttpContext.Session.GetComplexData<ConfirmTicketDataResponse>("confirmTicketDataResponse");
- model.tickets = tickets;
- model.step = step != null ? step : Constants.PAYMENT_SHOW;
- model.ticketType = ticketType;
- model.confirmTicketDataResponse = confirmTicketDataResponse;
- model.profile = profile;
- model.termType = termType;
- if (step == Constants.PAYMENT_RESULT)
- {
- ConfirmBuyingTicketResponse confirmBuyingTicketResponse = HttpContext.Session.GetComplexData<ConfirmBuyingTicketResponse>("confirmBuyingTicketResponse");
- model.confirmBuyingTicketResponse = confirmBuyingTicketResponse;
- }
- return View(model);
- }
- [ValidateAntiForgeryToken]
- public JsonResult PaymentSendOTP_Action(string ticketMoney)
- {
- try
- {
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- String token = HttpContext.Session.GetComplexData<String>("token");
- String termType = HttpContext.Session.GetComplexData<String>("termType");
- String tickets = HttpContext.Session.GetComplexData<String>("tickets");
- String ticketType = HttpContext.Session.GetComplexData<String>("ticketType");
- string[] ticketMoneyReal = ticketMoney.Split(',');
- string[] ticketNumberReal = tickets.Split(',');
- if (ticketMoneyReal.Length != ticketNumberReal.Length)
- {
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- int totalMoney = 0;
- List<TicketInfo> ticketInfos = new List<TicketInfo>();
- for (int i = 0; i < ticketMoneyReal.Length; i++)
- {
- TicketInfo ticketInfo = new TicketInfo();
- ticketInfo.money = ticketMoneyReal[i];
- ticketInfo.code = ticketNumberReal[i];
- ticketInfos.Add(ticketInfo);
- totalMoney += int.Parse(ticketInfo.money);
- }
- ConfirmTicketDataRequest confirmTicketDataRequest = new ConfirmTicketDataRequest();
- confirmTicketDataRequest.token = token;
- confirmTicketDataRequest.msisdn = msisdn;
- confirmTicketDataRequest.ticket = ticketInfos;
- confirmTicketDataRequest.gameId = ConvertToGameID(termType, ticketType);
- confirmTicketDataRequest.requestId = RandomString(100, true);
- ConfirmTicketDataResponse confirmTicketDataResponse = api.ConfirmTicketDataApi(configuration, confirmTicketDataRequest);
- if (confirmTicketDataResponse.responseCode == Code.SUCCESS)
- {
- //string drawnTime = DateTime.ParseExact(confirmTicketDataResponse.termObj.endDate, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture).ToString("HH:mm");
- //string content = Lang.confirm_payment_1 + " " + totalMoney + " " + Lang.confirm_payment_2 + " " + ConvertGameIdToName(confirmTicketDataRequest.gameId)
- // + " " + Lang.confirm_payment_3 + " 0 " + Lang.confirm_payment_4 + " " +
- // ConvertDrawnTimeFromTerm(termType, confirmTicketDataResponse.termObj) + Lang.confirm_payment_5;
- //Confirm payment %totalPayment% cent (%totalMoney% cents to buy ticket, fee %totalFee% cents), date time %buyTime%. Please fill OTP:
- string content = Lang.confirm_payment;
- content = content.Replace("%totalPayment%", Functions.FormatNumber(confirmTicketDataResponse.totalMoneyPayment));
- content = content.Replace("%totalMoney%", Functions.FormatNumber(confirmTicketDataResponse.totalMoney));
- content = content.Replace("%totalFee%", Functions.FormatNumber(confirmTicketDataResponse.totalMoneyFee));
- content = content.Replace("%buyTime%", DateTime.Now.ToString("HH:mm dd/MM/yyyy"));
- content = content.Replace("%drawTime%",
- DateTime.ParseExact(confirmTicketDataResponse.termObj.randomDate, "dd/MM/yyyy HH:mm:ss", null).ToString("HH:mm dd/MM/yyyy"));
-
- //
- HttpContext.Session.SetComplexData("confirmTicketDataResponse", confirmTicketDataResponse);
- String channel = GetParameter(Constants.CHANNEL);
- if (channel == ApiConstants.WALLET)
- {
- // ma hoa
- String param = "transactionId=" + confirmTicketDataResponse.transId + "&requestId=" + confirmTicketDataResponse.requestId + "&money=" + totalMoney;
- String url = CreatePrivateURL(configuration, param, "seconds", "0", "0", GetParameter("rsaPolicy"));
- // redirect
- return Json(new
- {
- code = "110",
- param = url,
- });
- }
- else
- {
- // send OTP
- SendOTPRequest sendOTPRequest = new SendOTPRequest();
- sendOTPRequest.msisdn = msisdn;
- sendOTPRequest.token = token;
- SendOTPResponse sendOTPResponse = api.SendOTPApi(configuration, sendOTPRequest);
- return Json(new
- {
- code = int.Parse(sendOTPResponse.responseCode),
- message = sendOTPResponse.responseMessage,
- content = content,
- });
- }
- }
- return Json(new
- {
- code = int.Parse(confirmTicketDataResponse.responseCode),
- message = GetLangFromCode(confirmTicketDataResponse.responseCode),
- });
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- [ValidateAntiForgeryToken]
- public JsonResult PaymentConfirmOTP_Action(string otp)
- {
- try
- {
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- String token = HttpContext.Session.GetComplexData<String>("token");
- ConfirmTicketDataResponse confirmTicketDataResponse = HttpContext.Session.GetComplexData<ConfirmTicketDataResponse>("confirmTicketDataResponse");
- ConfirmOTPRequest confirmOTPRequest = new ConfirmOTPRequest();
- confirmOTPRequest.otp = otp;
- confirmOTPRequest.token = token;
- confirmOTPRequest.msisdn = msisdn;
- ConfirmOTPResponse confirmOTPResponse = api.ConfirmOTPApi(configuration, confirmOTPRequest);
- if (confirmOTPResponse.responseCode == Code.SUCCESS)
- {
- // confirm ticket
- ConfirmBuyingTicketRequest confirmBuyingTicketRequest = new ConfirmBuyingTicketRequest();
- confirmBuyingTicketRequest.requestId = confirmTicketDataResponse.requestId;
- confirmBuyingTicketRequest.transIdByTicket = confirmTicketDataResponse.transId;
- confirmBuyingTicketRequest.token = token;
- ConfirmBuyingTicketResponse confirmBuyingTicketResponse = api.ConfirmBuyingTicketApi(configuration, confirmBuyingTicketRequest);
- HttpContext.Session.SetComplexData("confirmBuyingTicketResponse", confirmBuyingTicketResponse);
- return Json(new
- {
- code = int.Parse(confirmBuyingTicketResponse.responseCode),
- message = confirmBuyingTicketResponse.responseMessage,
- });
- }
- return Json(new
- {
- code = int.Parse(confirmOTPResponse.responseCode),
- message = GetLangFromCode(confirmOTPResponse.responseCode),
- });
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- [AutoValidateAntiforgeryToken]
- public JsonResult PaymentResentOTP_Action()
- {
- try
- {
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- String token = HttpContext.Session.GetComplexData<String>("token");
- // send OTP
- SendOTPRequest sendOTPRequest = new SendOTPRequest();
- sendOTPRequest.msisdn = msisdn;
- sendOTPRequest.token = token;
- SendOTPResponse sendOTPResponse = api.SendOTPApi(configuration, sendOTPRequest);
- return Json(new
- {
- code = int.Parse(sendOTPResponse.responseCode),
- message = GetLangFromCode(sendOTPResponse.responseCode),
- });
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- public IActionResult BackToApp()
- {
- return View();
- }
- }
- }
|