using NEducation.GenKeyWs; using Microsoft.Extensions.Configuration; using NEducation.Controllers; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using System.ServiceModel; using NEducation; using NEducation.Models; using System.Web.Mvc; using System.Web; using System.Net; using NEducation.Code; using NEducation.Content.Texts; namespace NEducation.Controllers { public class Data { public String key { get; set; } public String value { get; set; } public Data(String key, String value) { this.key = key; this.value = value; } } public class WapController : BaseController { static public log4net.ILog log = log4net.LogManager.GetLogger("WapController"); String wsUser; String wsPass; //static public String wsUrl = "http://paymentgateway.metfone.com.kh/MPS/"; //static public String wsUrlRes = "http://happycall.fun/Wap/ResponseFromWap"; ////static public String wsUrlRes = "https://localhost:44310/Wap/ResponseFromWap"; static private String PRO = "GTS"; static private String SER = "GTS_MEDU"; //static private String SUB = "GTS_HappyCalling_Daily"; static private String SUP_CP = "GTS"; static private String CONT = null; static private String TYPE = null; GenKeyWs.WsGenKeyClient wsClient = new GenKeyWs.WsGenKeyClient(); public async Task ResponseFromWapAsync(string DATA, string SIG) { try { DATA = "al12Rg2wdrLixNin9i19FdMMqBF6Xe+dQzXarlvBMqGQeh/PRD5F9zBrZc0lGlN/fGoJ/NuuMh96ymeuOtyv6rlu9+KOBMGXiAH43J81zBLMJ9+aF3mQR7OT7mQnQkFe4i2gpMnYi2NRUSyJAZm1NnTeoDswqsYWVcke+jMtubSQdmBkSmm2pUTW+rLgHi4sMrPLyRlw0T5HsEuT6YUpqRKaweF97pLZDNUaJePRRPyJASHvT3yOy7VTfAIcVSC1DGrYOv3Kyj18pKhxjskCWdYUd3r4jngN/iMALnQvgAWtsBsfH50KS77rs0r6MIMpN6rv96BrtpHn0gmqqYTDGtykNa5FsRyclqyI3CLgn9hQi2TWUVMWHETqdR0E8VrdIXMNGhgaNclDgKFFyTl/V02vSH+RbeuPU05XLfUaBD/e8SKA0rTlQfGVAyJZtRvMreg76dQBZFBbda96M95Mdphhp/ZR5XrVRKt+dM1+8FxLpQefsBp21L3HFJrzgcPxkz5qQN1rdx2Umhzf0W4rQErsGk7xZoYlxVGo2/7Ouwi0BJU1ocK3/z5bSskAQTc3UWdzzm0q+KDs/gEd1ouqoA3R2n5PuMqMTZhztcpxIJODKfmSPS/pTZNPQrXSpbAMqN11FRTxzt7yjRdFrOd4zDVRPYXcQ7ExYvdY0JrXAZE="; SIG = "Bwdr1erav2UsFY7OpAGFNN%2BZPQ90cLDhqniE6Ta2kaD3kEtrKIprsfIz6BAL%2BIY8KhC1Xx0N7cSZyU1uAfEHRG%2BCJ5hbM5s%2B7kzMv4ZE7CFHqF9LuZrQQqz8IHzkPRHBgyWDy7RtESE1y%2BMRC1X9sAAdNFKxEL0NSCQigepfFD6YTnNFJix1OPAYwy1KfRTVAU10%2F%2BlkNuGegk651SH%2FgZ8yptwpnoIYdlUPQh3D404vm58EpFz7%2BtetzcolF6ajvJFGov%2BOvAOataD7%2BQDrJpqQ%2B7pf8A3fdAPAqRTmpUjCYtL0gRL8TILkPrF%2BmpaWCyWFzYug7CGvr%2F2qQForFhPH0xQj2SFI1tfBfYQOg4V7oCnOI9Jq0haiQ2mCCA5o1cQ0dEAK43NdRP2dfmwh%2BgVK%2BVeZpWxqOLhegeU%2B9bFA0iUmjXCxLNcSbsHqA7j7IvmPb4Ms2zhv4B9ALQ94YZft9Ut5ZRzvO8LYrmhmWqvK8mFQwIGwQNTKMd9UCCoSiTV8an7MFG2uzweS9Q65wwNbbaEstD8mOvD9Jkwv2x71WGI%2FFwkGPrbxzrfATD5DgQ7IFIpDD0Rr%2BPG4S5WlJlSYTPks%2FRgLNyHgQ%2BX3UGPINNKzj3xaC25RCN2iKoN7TY%2F4FEB8Day0gfd3aMfvFCpOXF5h6uGOpAp7ETsOgP0%3D"; // Pre-process input data string dataGet = DATA.Replace(" ", "+"); var sigGet = Uri.EscapeDataString(SIG).ToString(); var data = "DATA=" + dataGet + "&SIG=" + sigGet; log.Debug("data " + data); // Load configuration settings String wsUrlRes = ConfigurationManager.AppSettings["domainRes"]; String wsUrl = ConfigurationManager.AppSettings["paymentUrl"]; String packageName = ConfigurationManager.AppSettings["packname"]; String packNameCannel = ConfigurationManager.AppSettings["packNameCannel"]; String packNameREGISTER_ToBe = ConfigurationManager.AppSettings["packNameREGISTER_ToBe"]; String packNameCannel_ToBe = ConfigurationManager.AppSettings["packNameCannel_ToBe"]; String DIR = ConfigurationManager.AppSettings["directoryPath"]; log.Debug("DIR_Wap " + DIR); if (packageName == null) { Session["register-result-message"] = "Register unsuccessful"; Session["register-result-status"] = "-1"; return Redirect("/Home/RegisterResult"); } // Decrypt the key GenKeyWs.wsDecryptKeyResponse decrypt = await wsClient.wsDecryptKeyAsync(null, null, DIR + packageName.ToUpper() + "\\", wsUrl, data); log.Debug("errorCode " + decrypt.@return.errorCode); log.Debug("decrypt " + decrypt.@return.descrypt); // Log details if available if (decrypt.@return.mpsResponse != null) { log.Debug("CMD " + decrypt.@return.mpsResponse.CMD); log.Debug("MOBILE " + decrypt.@return.mpsResponse.MOBILE); log.Debug("PRICE " + decrypt.@return.mpsResponse.PRICE); log.Debug("RES " + decrypt.@return.mpsResponse.RES); log.Debug("REQ " + decrypt.@return.mpsResponse.REQ); log.Debug("SOURCE " + decrypt.@return.mpsResponse.SOURCE); log.Debug("STATUS " + decrypt.@return.mpsResponse.STATUS); log.Debug("SUB_END_DATE " + decrypt.@return.mpsResponse.SUB_END_DATE); log.Debug("SUB_NEW " + decrypt.@return.mpsResponse.SUB_NEW); log.Debug("SUB_SERVICE " + decrypt.@return.mpsResponse.SUB_SERVICE); log.Debug("SUB_START_DATE " + decrypt.@return.mpsResponse.SUB_START_DATE); } else { log.Debug("mpsResponse null "); } if (decrypt.@return.errorCode == UtilsController.WapConstant.SUCCESS) { // Handle different command cases if (decrypt.@return.mpsResponse.CMD == UtilsController.WapConstant.REGISTER) { log.Info("REGISTER MOBILE " + decrypt.@return.mpsResponse.MOBILE); if (!string.IsNullOrEmpty(decrypt.@return.mpsResponse.MOBILE)) { // Validate and process phone number String phoneNumber = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE); if (!string.IsNullOrEmpty(phoneNumber)) { Session["msisdn-detecting"] = phoneNumber; // Perform registration request UserRequest userRequest = new UserRequest { users = decrypt.@return.mpsResponse.MOBILE, msisdn = decrypt.@return.mpsResponse.MOBILE, subServiceCode = packNameREGISTER_ToBe + "|" + decrypt.@return.mpsResponse.PRICE, requestId = "-1", otp = decrypt.@return.mpsResponse.RES, serviceId = "3" }; String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.MpsRequest); UserActionResult ress = new UserActionResult(rs); if (ress.responseCode == UtilsController.Constant.SUCCESS) { Charging charging = new Charging(rs); Session["charging"] = charging; Session["msisdn"] = decrypt.@return.mpsResponse.MOBILE; Session["package"] = packNameCannel; Session["CHARGE_ACTION"] = UtilsController.Constant.REGISTER; Session["CHARGE_DATA"] = userRequest; } else if (ress.status == "2") { return Json(new { error_code = ress.status, error_content = UtilsController.GetErrorCodeCharging(ress.status) }); } return Json(new { error_code = ress.responseCode, error_content = UtilsController.GetErrorCodeCharging(ress.responseCode) }); } else { log.Info("Cannel Can not detecting"); } } Session["register-result-message"] = "Register successful"; Session["register-result-status"] = "0"; return Redirect("/Home/RegisterResult"); } else if (decrypt.@return.mpsResponse.CMD == UtilsController.WapConstant.CANCEL) { String phoneNumber = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE); if (!string.IsNullOrEmpty(phoneNumber)) { if (decrypt.@return.mpsResponse.RES == UtilsController.WapConstant.SUCCESS || decrypt.@return.mpsResponse.RES == UtilsController.WapConstant.CANCEL_USER_IN_CHARGING_LIST) { if (!string.IsNullOrEmpty(decrypt.@return.mpsResponse.MOBILE)) { String phoneNumbers = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE); if (!string.IsNullOrEmpty(phoneNumbers)) { Session["msisdn-detecting"] = phoneNumber; var checkSub = UtilsController.checkAccountSub(phoneNumber, packNameCannel_ToBe); if (!checkSub) { UserRequest userRequest = new UserRequest { users = decrypt.@return.mpsResponse.MOBILE, msisdn = decrypt.@return.mpsResponse.MOBILE, subServiceCode = packNameCannel_ToBe + "|" + decrypt.@return.mpsResponse.PRICE, requestId = "-1", otp = decrypt.@return.mpsResponse.RES, serviceId = "3" }; String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.MpsRequest); UserActionResult ress = new UserActionResult(rs); if (ress.responseCode == UtilsController.Constant.SUCCESS) { Charging charging = new Charging(rs); Session["charging"] = charging; Session["msisdn"] = decrypt.@return.mpsResponse.MOBILE; Session["package"] = packageName; Session["CHARGE_ACTION"] = UtilsController.Constant.REGISTER; Session["CHARGE_DATA"] = userRequest; } else if (ress.status == "2") { return Json(new { error_code = ress.status, error_content = UtilsController.GetErrorCodeCharging(ress.status) }); } return Json(new { error_code = ress.responseCode, error_content = UtilsController.GetErrorCodeCharging(ress.responseCode) }); } else { return Json(new { error_code = UtilsController.Constant.USER_EXISTED, error_content = UtilsController.GetErrorCodeCharging(UtilsController.Constant.USER_EXISTED) }); } } else { return Json(new { error_code = UtilsController.Constant.INVALID_MSISDN, error_content = @Lang.InvalidMsisdn, }); } } } else { log.Error("CANCEL unsuccessful"); Session["register-result-message"] = "Cancel unsuccessful"; Session["register-result-status"] = "-1"; return Redirect("/Home/RegisterResult"); } Session["register-result-message"] = "Cancel successful"; Session["register-result-status"] = "0"; return Redirect("/Home/RegisterResult"); } Session["register-result-message"] = "Cancel unsuccessful"; Session["register-result-status"] = "-1"; return Redirect("/Home/RegisterResult"); } else if (decrypt.@return.mpsResponse.CMD == UtilsController.WapConstant.MSISDN) { if (!string.IsNullOrEmpty(decrypt.@return.mpsResponse.MOBILE) && decrypt.@return.mpsResponse.MOBILE != "null") { // Validate and process phone number String phoneNumber = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE); if (!string.IsNullOrEmpty(phoneNumber)) { // Auto login for this user Session["msisdn-detecting"] = phoneNumber; return Redirect("/Home?detecting=0"); } } else { log.Info("Can not detecting"); } } else { log.Info("Command: " + decrypt.@return.mpsResponse.CMD); } } else { // Handle unsuccessful registration log.Info("Error "); Session["register-result-message"] = "Register unsuccessful"; Session["register-result-status"] = "-1"; return Redirect("/Home/RegisterResult"); } return Redirect("/Home?detecting=0"); } catch (Exception ex) { log.Info("Exception: " + ex); return Redirect("/Home?detecting=0"); } } // GET: Wap public static async Task UserDetectingAsync(IConfiguration configuration, GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams) { GenKeyWs.response response = new GenKeyWs.response(); response.errorCode = UtilsController.WapConstant.FAILURE; //String DIR = configuration.GetSection("directoryPath").Value; //String wsUrl = configuration.GetSection("paymentUrl").Value; String DIR = configuration.GetSection("directoryPath").Value; String wsUrl = configuration.GetSection("paymentUrl").Value; GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "mobile.html?", PRO, SER, mpsparams.SUB, mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE); log.Debug("UserDetecting build req: " + makeUrl.@return.enscrypt); //HttpContext.Session.SetComplexData("mps-params", mpsparams); return makeUrl.@return; } public static async Task UserRegisteringAsync(GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams) { GenKeyWs.response response = new GenKeyWs.response(); response.errorCode = UtilsController.WapConstant.FAILURE; String DIR = ConfigurationManager.AppSettings["directoryPath"] + mpsparams.SUB.ToUpper() + "/"; log.Debug("UserRegistering build DIR: " + DIR); String wsUrl = ConfigurationManager.AppSettings["paymentUrl"]; //String DIR = configuration.GetSection("directoryPath").Value + mpsparams.SUB.ToUpper() + "/"; //String wsUrl = configuration.GetSection("paymentUrl").Value; log.Debug("UserRegistering makeUrl:"); GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "charge.html?", PRO, SER, mpsparams.SUB, mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE); log.Debug("UserRegistering build req: " + makeUrl.@return.enscrypt); // save session //HttpContext.Session.SetComplexData("mps-params", mpsparams); return makeUrl.@return; } public static async Task UserChargingAsync(GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams) { GenKeyWs.response response = new GenKeyWs.response(); response.errorCode = UtilsController.WapConstant.FAILURE; response.errorCode = UtilsController.WapConstant.FAILURE; String DIR = ConfigurationManager.AppSettings["directoryPath"]; String wsUrl = ConfigurationManager.AppSettings["paymentUrl"]; //String DIR = configuration.GetSection("directoryPath").Value; //String wsUrl = configuration.GetSection("paymentUrl").Value; GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "charge.html?", PRO, SER, mpsparams.SUB, mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE); log.Debug("UserCharging build req: " + makeUrl.@return.enscrypt); //HttpContext.Session.SetComplexData("mps-params", mpsparams); return makeUrl.@return; } public static async Task UserCancelingAsync(GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams) { GenKeyWs.response response = new GenKeyWs.response(); response.errorCode = UtilsController.WapConstant.FAILURE; response.errorCode = UtilsController.WapConstant.FAILURE; String DIR = ConfigurationManager.AppSettings["directoryPath"] + mpsparams.SUB.ToUpper() + "/"; String wsUrl = ConfigurationManager.AppSettings["paymentUrl"]; //String DIR = configuration.GetSection("directoryPath").Value + mpsparams.SUB.ToUpper() + "/"; //String wsUrl = configuration.GetSection("paymentUrl").Value; GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "charge.html?", PRO, SER, mpsparams.SUB, mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE); log.Debug("UserCanceling build req: " + makeUrl.@return.enscrypt); // save session //HttpContext.Session.SetComplexData("mps-params", mpsparams); return makeUrl.@return; } static public String PostData(string mpsurl) { String url = mpsurl; log.Debug("Request: " + url); using (var client = new HttpClient()) { var response = client.GetAsync(url).Result; if (response.IsSuccessStatusCode) { var responseContent = response.Content; // by calling .Result you are synchronously reading the result string responseString = responseContent.ReadAsStringAsync().Result; log.Debug("Response: " + responseString); return responseString; } else { log.Error("Response: " + response.StatusCode.ToString()); return response.StatusCode.ToString(); } } } // push to webservice public async Task RegisterOtp(String package, String msisdn, String code, String action) { try { MpsReq req = new MpsReq(); req.msisdn = msisdn; req.subServiceCode = package; req.otp = code; // not send to mps //NcGamesWs.subRequestPGWResponse res = await wsGGameClient.subRequestPGWAsync(wsUser, wsPass, // msisdn, UtilsController.Constant.SERVICE_ID, // package, action); //MpsRes res = new MpsRes(rs); // check success if ( //res.@return == "0" && (code == UtilsController.WapConstant.SUCCESS || code == UtilsController.WapConstant.CANCEL_USER_IN_CHARGING_LIST)) { //HttpContext.Session.SetComplexData("msisdn", msisdn); //HttpContext.Session.SetComplexData("msisdn-detecting", msisdn); return true; } return false; } catch (Exception ex) { log.Error("Exception: " + ex); return false; } } } }