| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
-
- using log4net;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading.Tasks;
- using WebPortal.Models;
- using Pulsa.Texts;
- using WebService;
- using WebPortal.Extensions;
- using Microsoft.AspNetCore.Http;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json;
- namespace WebPortal.Controllers
- {
- public class HomeController : BaseController
- {
- private ILog log = LogManager.GetLogger("HomeController");
- private WsWebPortalClient wsClient = new WsWebPortalClient();
- public HomeController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment)
- {
- // init
- }
- public IActionResult Index(String noDetect, String tempMsisdn)
- {
- LoadBanner("HOME");
- LoadTheme();
- String msisdn = HttpContext.Session.GetString("account");
- if (msisdn == null && noDetect == null)
- {
- if (tempMsisdn != null && tempMsisdn.Length > 0)
- {
- msisdn = tempMsisdn;
- ViewBag.msisdn = msisdn;
- }
- else
- {
- DetectMsisdn(wsClient);
- }
- }
- ViewBag.productType = ProductType.RECHARGE;
- return View();
- }
- private void LoadTheme()
- {
- log.Info("Load theme");
- // load theme
- SetWsClient(ref wsClient);
- wsGetCurrentThemeRequest wsRequest = new wsGetCurrentThemeRequest(
- BaseController.wsUser,
- BaseController.wsPassword);
- var wsResponse = wsClient.wsGetCurrentTheme(wsRequest);
- if (wsResponse.@return.listTheme != null && wsResponse.@return.listTheme.Length > 0)
- {
- log.Info("Current theme: " + wsResponse.@return.listTheme[0].themeName);
- HttpContext.Session.SetComplexData("currentTheme", wsResponse.@return.listTheme[0]);
- }
- }
- private void LoadBanner(String type)
- {
- LoadBanner(wsClient, type, "2");
- }
- private string GetIp()
- {
- string ipRemote = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
- string ipLocal = HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
- log.Info("Ip Remote: " + ipRemote + ", IP local: " + ipLocal);
- return ipRemote;
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public JsonResult RechargeScratch(string msisdn,
- string code,
- string captcharesponse)
- {
- try
- {
- if (Check(captcharesponse) == false)
- {
- return Json(new
- {
- error = "16",
- content = Lang.CaptchaInvalid
- });
- }
- wsBuyCardRequest req = new wsBuyCardRequest(wsUser, wsPassword, msisdn, code, "1", getCurrentLang());
- SetWsClient(ref wsClient);
- var res = wsClient.wsBuyCard(req);
- return Json(new
- {
- error = res.@return.code,
- content = res.@return.message
- });
- }
- catch (Exception ex)
- {
- log.Error("Exception RechargeScratch", ex);
- return Json(new
- {
- error = "-1",
- content = "System failed"
- });
- }
- }
- private bool Check(string response)
- {
- //string Response = HttpContext.Current.Request.QueryString["g-recaptcha-response"];//Getting Response String Append to Post Method
- bool Valid = false;
- //Request to Google Server
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create
- ("https://www.google.com/recaptcha/api/siteverify?secret=" + BaseController.CaptchaSecretKey + "&response=" + response);
- try
- {
- //Google recaptcha Response
- using (WebResponse wResponse = req.GetResponse())
- {
- using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
- {
- string jsonResponse = readStream.ReadToEnd();
- //JavaScriptSerializer js = new JavaScriptSerializer();
- //MyObject data = js.Deserialize<MyObject>(jsonResponse);// Deserialize Json
- MyObject data = JsonConvert.DeserializeObject<MyObject>(jsonResponse);
- Valid = Convert.ToBoolean(data.success);
- }
- }
- return Valid;
- }
- catch (WebException ex)
- {
- throw ex;
- }
- }
- public class MyObject
- {
- public string success { get; set; }
- }
- }
- }
|