| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using ResfullApi.Models;
- using Newtonsoft.Json;
- using System.Data;
- using System.IO;
- using System.Xml;
- using Microsoft.AspNetCore.Mvc;
- namespace ApiProcess.Controllers
- {
- [ApiController]
- [Route("api/[controller]/[action]/data")]
- public class systemApi : ControllerBase
- {
- static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(systemApi));
- // {"msisdn":"50940227941","serviceId":"23","money":"2000"}
- [HttpPost]
- public IActionResult systemCharge([FromBody] dynamic sendData)
- {
- ////log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- ////log4net.Config.XmlConfigurator.Configure();
- responseObjUtil response = new responseObjUtil();
- response.status = "-1";
- response.message = "Err unknow";
- try
- {
- systemObjChargeRequest data = JsonConvert.DeserializeObject<systemObjChargeRequest>(sendData.ToString());
- logger.Info("New request income systemCharge :" + data.ToString());
- //lay ve thong tin cua CGW ung voi service
- DataSet ds_sv = systemDataAccess.SYS_GET_SERVICE_BY_ID(data.serviceId);
- logger.Info("Call database SYS_GET_SERVICE_BY_ID success:");
- if (ds_sv != null & ds_sv.Tables[0].Rows.Count > 0)
- {
- DataRow dr = ds_sv.Tables[0].Rows[0];
- string SV_CODE = dr["sv_code"].ToString();
- string sv_charg_shortcode = dr["sv_charg_shortcode"].ToString();
- string sv_command = dr["sv_command"].ToString();
- string sv_charg_url = dr["sv_charg_url"].ToString();
- string sv_charg_user = dr["sv_charg_user"].ToString();
- string sv_charg_pass = dr["sv_charg_pass"].ToString();
- string sv_char_timeout = dr["sv_char_timeout"].ToString();
- string sv_char_wscode = dr["sv_char_wscode"].ToString();
- string sv_charg_rawdata = dr["sv_charg_rawdata"].ToString();
- string sv_charg_gworder = dr["sv_charg_gworder"].ToString();
- string msisdn_temp = data.msisdn;
- string money = data.money;
- string reqid = SV_CODE + "_" + msisdn_temp+"_" + System.DateTime.Now.ToString("ddMMyyyyHHmmsss");
- string request = createdRequest(reqid, msisdn_temp, money, sv_charg_shortcode, sv_command, sv_charg_user, sv_charg_pass,
- sv_char_wscode, sv_charg_rawdata, sv_charg_gworder);
- logger.Info("Request :" + request);
- string resultPost = getContent(sv_charg_url, sv_char_timeout, request);
- logger.Info("Response :" + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- string ketqua = "-1000|unknow";
- string err_code = "-1000";
- string err_msg = "unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
- if (err_code_cmd == "0")
- {
-
- ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- err_code = ketqua.Split('|')[0];
- err_msg = ketqua.Split('|')[1];
- logger.Info("send request id :" + reqid + " err_code: " + err_code + " err_msg: " + err_msg);
-
- }
- //update log charge
- DataSet ds_updateLog = systemDataAccess.SYS_UPDATE_LOG_CHARGE(data.msisdn, data.serviceId, data.money, err_code_cmd, err_msg_cmd, err_code, err_msg, data.isRenew);
- if (ds_updateLog != null & ds_updateLog.Tables[0].Rows.Count > 0)
- {
- response.status = ds_updateLog.Tables[0].Rows[0]["status"].ToString();
- response.message = ds_updateLog.Tables[0].Rows[0]["message"].ToString();
- }
- }
- }
-
- }
- catch (Exception ex)
- {
- logger.Info("Err:" + ex.ToString());
- }
- logger.Info("Responase to web: " + response.ToString());
- return Ok(response);
- }
- private string createdRequest(string requestid, string msisdn, string money, string shortcode, string command,
- string user, string pass, string sv_char_wscode, string sv_charg_rawdata, string sv_charg_gworder)
- {
- //log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- //log4net.Config.XmlConfigurator.Configure();
- string request = "";
- try
- {
- string input = msisdn + "|" + shortcode + "|" + command + "|1|" + money + "|" + requestid;
- request = request + @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://webservice.bccsgw.viettel.com/"">";
- request = request + @"<soapenv:Header/>";
- request = request + @"<soapenv:Body>";
- request = request + @"<web:gwOperation>";
- request = request + @"<Input>";
- request = request + @"<username>" + user + @"</username>";
- request = request + @"<password>" + pass + @"</password>";
- request = request + @"<wscode>" + sv_char_wscode + @"</wscode>";
- request = request + @"<param name=""GWORDER"" value=""" + sv_charg_gworder + @"""/>";
- request = request + @"<param name=""input"" value=""" + input + @"""/>";
- request = request + @"<rawData></rawData>";
- request = request + @"</Input>";
- request = request + @"</web:gwOperation>";
- request = request + @"</soapenv:Body>";
- request = request + @"</soapenv:Envelope>";
- }
- catch (Exception ex)
- {
- logger.Info("Err when create request: " + ex.ToString());
- }
- return request;
- }
- private string getContent(string api_path, string timeout, string xml)
- {
- //log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- //log4net.Config.XmlConfigurator.Configure();
- string rp = "";
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_path);
- byte[] bytes;
- bytes = System.Text.Encoding.UTF8.GetBytes(xml);
- request.ContentType = "text/xml; encoding='utf-8'";
- request.ContentLength = bytes.Length;
- request.Method = "POST";
- request.Timeout = Convert.ToInt32(timeout);
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(bytes, 0, bytes.Length);
- requestStream.Close();
- HttpWebResponse response;
- response = (HttpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- rp = new StreamReader(responseStream).ReadToEnd();
- }
- catch (Exception ex)
- {
- logger.Info("Err when post: " + ex.ToString());
- }
- return rp;
- }
- }
- }
|