public ChannelResult CTUSMSPayRequest(float amount, string userNo, MobileInfo mobileInfo, int serviceId, int spId, string phoneNo) { ChannelResult result = new ChannelResult(); if (!CTUSMSChargeProducts.ContainsKey(amount)) { result.CTURequestStatus = CTURequestStatus.AmountError; return(result); } var CTUProductInfo = CTUSMSChargeProducts[amount]; var orderNo = PaymentsService.CreateOrder("[畅天游]产品ID_" + CTUProductInfo.SID, amount, "短信充值:" + amount.ToString(), userNo, (int)PaymentType.SMS); result.OrderNo = orderNo; #region Get MD5 key string key = orderNo + mobileInfo.Mobile + amount.ToString() + CTUProductInfo.RequestKey; byte[] hashedBytes = null; using (MD5 md5Hash = MD5.Create()) { hashedBytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(key)); } var hashedKey = new StringBuilder(); for (int i = 0; i < hashedBytes.Length; i++) { hashedKey.Append(hashedBytes[i].ToString("x2")); } #endregion var url = string.Format(CHARGE_URL, CTUProductInfo.SID, orderNo, mobileInfo.Mobile, amount, hashedKey.ToString()); var response = this.RESTfulCient.Get(url, 5000); if (!string.IsNullOrEmpty(response)) { XmlDocument resultDoc = new XmlDocument(); try { resultDoc.LoadXml(response); } catch (XmlException ex) { LogManager.GetLogger("ErrorLogger").Error(ex.Message); return(result); } var resultNode = resultDoc.SelectSingleNode("/Root/Result"); var msgNode = resultDoc.SelectSingleNode("/Root/Msg"); var resultCode = -1; int.TryParse(resultNode.InnerText, out resultCode); result.CTURequestStatus = (CTURequestStatus)resultCode; if (resultCode == 0) { result.Code = RegexHelper.GetMatchedGroupValue(msgNode.InnerText, CTUChargeRequestRegex, "instruction"); result.ServiceNumber = RegexHelper.GetMatchedGroupValue(msgNode.InnerText, CTUChargeRequestRegex, "serviceNo"); SMSChannelLog log = new SMSChannelLog { Amount = amount, ChargeStatus = SMSChargeStatus.Initial, CityId = mobileInfo.CityId, OpId = mobileInfo.OperatorId, ProvinceId = mobileInfo.ProvinceId, ServiceNumber = result.ServiceNumber, Instruction = result.Code, Mobile = phoneNo, IMSI = GetIMSI(phoneNo), OrderNo = orderNo, }; if (string.IsNullOrEmpty(result.Code) || string.IsNullOrEmpty(result.ServiceNumber)) { result.Status = ChannelRequestStatus.NoChannelFound; log.ChargeStatus = SMSChargeStatus.NotFound; } else // CTU returned a valid channel { var channel = this.Single <SMSChannel>(x => x.ServiceId == serviceId); if (channel != null) { var smsChannelSetting = this.Single <SMSChannelSetting>(x => x.ChannelId == channel.Id); if (smsChannelSetting != null) { result.SMSChannelSetting = smsChannelSetting; } } log.ChargeStatus = SMSChargeStatus.Initial; // find saved channel //var savedChannel = this.Single<SMSChannel>(x => x.ServiceId == serviceId && x.ServiceNumber == result.ServiceNumber); //// channel not found, just save it //if (savedChannel == null) //{ // ShortMessageService service = new ShortMessageService // { // SpId = spId, // Type = ServiceType.SMSCharge, // ServiceNumber = result.ServiceNumber, // Name = string.Format("[Auto]Province[{0}]City[{1}]Op[{2}]", mobileInfo.ProvinceId, mobileInfo.CityId, mobileInfo.OperatorId), // IsUnicom = mobileInfo.OperatorId == 2, // IsMobile = mobileInfo.OperatorId == 1, // IsTelcom = mobileInfo.OperatorId == 3, // }; //} //else // found saved channel, read and apply its setting and restrictions //{ //} // send channel setting info //if (channel != null) //{ // result.SMSChannelSetting = new SMSChannelSetting // { // ChannelId = channel.Id, // ChargeMessageRegex = channel.ChargeMessageRegex, // ChargeMessageTemplate = channel.ChargeMessageTemplate, // FinalConfirmMessage = channel.FinalConfirmMessage, // ConfirmMessage = channel.ConfirmMessage, // ConfirmMessageTemplate = channel.ConfirmMessageTemplate // }; //} } this.AddLog <SMSChannelLog>(log); result.LogId = log.ID; } else { LogManager.GetLogger("ErrorLogger").Error(CHARGE_URL + " response:" + response); } } return(result); }
public ChannelResult QueryChannel(string imsi, string mobile, ServiceType serviceType, float?amount, string userNo) { ChannelResult result = null; List <ShortMessageService> services = new List <ShortMessageService>(); var mobileInfo = this.GetMobileInfoByPhoneNumber(mobile); IList <ShortMessageService> smsServices; if (serviceType == ServiceType.SMS) { smsServices = this.Find <ShortMessageService>(s => s.Type == serviceType); } else { if (mobileInfo == null) { result = new ChannelResult { Status = ChannelRequestStatus.MissingMobileInfo }; return(result); } // filter by operator switch (mobileInfo.OperatorId) { case 1: smsServices = this.Find <ShortMessageService>(s => s.Type == serviceType && s.IsMobile == true); break; case 2: smsServices = this.Find <ShortMessageService>(s => s.Type == serviceType && s.IsUnicom == true); break; case 3: smsServices = this.Find <ShortMessageService>(s => s.Type == serviceType && s.IsTelcom == true); break; default: smsServices = this.Find <ShortMessageService>(s => s.Type == serviceType); break; } } if (smsServices != null && smsServices.Count > 0) { services = smsServices.ToList(); ShortMessageService service = new ShortMessageService(); Instruction selectedInstruction = new Instruction(); SMSChannel channel = new SMSChannel(); ServiceProvider sp = new ServiceProvider(); for (int i = 0; i < services.Count; i++) { service = services[i]; sp = this.Single <ServiceProvider>(x => x.Id == service.SpId); // TODO: check channel restriction here if (!service.IsManully) { var instructions = this.Find <Instruction>(s => s.ServiceId == service.Id); channel = this.Single <SMSChannel>(x => x.ServiceId == service.Id); // TODO: check instruction restriction here if (instructions != null && instructions.Count > 0) { selectedInstruction = instructions[0]; result = new ChannelResult(); result.ServiceNumber = service.ServiceNumber; result.Code = instructions[0].Code; break; } } else { switch (sp.DynamicSP) { case DynamicSP.CTU: return(CTUSMSPayRequest(amount.GetValueOrDefault(), userNo, mobileInfo, service.Id, sp.Id, mobile)); } } } if (serviceType == ServiceType.SMSCharge && selectedInstruction != null && channel != null && result != null) { // create order var orderNo = PaymentsService.CreateOrder("[短代]", selectedInstruction.Amount, "短信充值:" + selectedInstruction.Amount.ToString(), mobile); result.OrderNo = orderNo; // initial channel log if (mobileInfo == null) { mobileInfo = new MobileInfo(); } SMSChannelLog log = new SMSChannelLog { Amount = selectedInstruction.Amount, ChargeStatus = SMSChargeStatus.Initial, CityId = mobileInfo.CityId, OpId = mobileInfo.OperatorId, ProvinceId = mobileInfo.ProvinceId, ServiceNumber = result.ServiceNumber, Instruction = result.Code, Mobile = mobile, IMSI = GetIMSI(mobile), OrderNo = orderNo }; this.AddLog <SMSChannelLog>(log); result.LogId = log.ID; // set channel setting if (channel != null) { var settings = this.Find <SMSChannelSetting>(x => x.ChannelId == channel.Id); if (settings != null && settings.Count > 0) { result.SMSChannelSetting = settings[0]; } } } } return(result); }