HomeController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 
  2. using log4net;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using WebPortal.Models;
  13. using Pulsa.Texts;
  14. using WebService;
  15. using WebPortal.Extensions;
  16. using Microsoft.AspNetCore.Http;
  17. using System.Net;
  18. using System.IO;
  19. using Newtonsoft.Json;
  20. namespace WebPortal.Controllers
  21. {
  22. public class HomeController : BaseController
  23. {
  24. private ILog log = LogManager.GetLogger("HomeController");
  25. private WsWebPortalClient wsClient = new WsWebPortalClient();
  26. public HomeController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment)
  27. {
  28. // init
  29. }
  30. public IActionResult Index(String noDetect, String tempMsisdn)
  31. {
  32. LoadBanner("HOME");
  33. LoadTheme();
  34. String msisdn = HttpContext.Session.GetString("account");
  35. if (msisdn == null && noDetect == null)
  36. {
  37. if (tempMsisdn != null && tempMsisdn.Length > 0)
  38. {
  39. msisdn = tempMsisdn;
  40. ViewBag.msisdn = msisdn;
  41. }
  42. else
  43. {
  44. DetectMsisdn(wsClient);
  45. }
  46. }
  47. ViewBag.productType = ProductType.RECHARGE;
  48. return View();
  49. }
  50. private void LoadTheme()
  51. {
  52. log.Info("Load theme");
  53. // load theme
  54. SetWsClient(ref wsClient);
  55. wsGetCurrentThemeRequest wsRequest = new wsGetCurrentThemeRequest(
  56. BaseController.wsUser,
  57. BaseController.wsPassword);
  58. var wsResponse = wsClient.wsGetCurrentTheme(wsRequest);
  59. if (wsResponse.@return.listTheme != null && wsResponse.@return.listTheme.Length > 0)
  60. {
  61. log.Info("Current theme: " + wsResponse.@return.listTheme[0].themeName);
  62. HttpContext.Session.SetComplexData("currentTheme", wsResponse.@return.listTheme[0]);
  63. }
  64. }
  65. private void LoadBanner(String type)
  66. {
  67. LoadBanner(wsClient, type, "2");
  68. }
  69. private string GetIp()
  70. {
  71. string ipRemote = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
  72. string ipLocal = HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
  73. log.Info("Ip Remote: " + ipRemote + ", IP local: " + ipLocal);
  74. return ipRemote;
  75. }
  76. [HttpPost]
  77. [ValidateAntiForgeryToken]
  78. public JsonResult RechargeScratch(string msisdn,
  79. string code,
  80. string captcharesponse)
  81. {
  82. try
  83. {
  84. if (Check(captcharesponse) == false)
  85. {
  86. return Json(new
  87. {
  88. error = "16",
  89. content = Lang.CaptchaInvalid
  90. });
  91. }
  92. wsBuyCardRequest req = new wsBuyCardRequest(wsUser, wsPassword, msisdn, code, "1", getCurrentLang());
  93. SetWsClient(ref wsClient);
  94. var res = wsClient.wsBuyCard(req);
  95. return Json(new
  96. {
  97. error = res.@return.code,
  98. content = res.@return.message
  99. });
  100. }
  101. catch (Exception ex)
  102. {
  103. log.Error("Exception RechargeScratch", ex);
  104. return Json(new
  105. {
  106. error = "-1",
  107. content = "System failed"
  108. });
  109. }
  110. }
  111. private bool Check(string response)
  112. {
  113. //string Response = HttpContext.Current.Request.QueryString["g-recaptcha-response"];//Getting Response String Append to Post Method
  114. bool Valid = false;
  115. //Request to Google Server
  116. HttpWebRequest req = (HttpWebRequest)WebRequest.Create
  117. ("https://www.google.com/recaptcha/api/siteverify?secret=" + BaseController.CaptchaSecretKey + "&response=" + response);
  118. try
  119. {
  120. //Google recaptcha Response
  121. using (WebResponse wResponse = req.GetResponse())
  122. {
  123. using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
  124. {
  125. string jsonResponse = readStream.ReadToEnd();
  126. //JavaScriptSerializer js = new JavaScriptSerializer();
  127. //MyObject data = js.Deserialize<MyObject>(jsonResponse);// Deserialize Json
  128. MyObject data = JsonConvert.DeserializeObject<MyObject>(jsonResponse);
  129. Valid = Convert.ToBoolean(data.success);
  130. }
  131. }
  132. return Valid;
  133. }
  134. catch (WebException ex)
  135. {
  136. throw ex;
  137. }
  138. }
  139. public class MyObject
  140. {
  141. public string success { get; set; }
  142. }
  143. }
  144. }