/// <summary> /// 把请求要素按照“参数=参数值”的模式用“&”字符拼接成字符串 /// </summary> /// <param name="para">请求要素</param> /// <param name="sort">是否需要根据key值作升序排列</param> /// <param name="encode">是否需要URL编码</param> /// <returns>拼接成的字符串</returns> public static String CreateLinkString(Dictionary <String, String> para, bool sort, bool encode) { List <String> list = new List <String>(para.Keys); if (sort) { list.Sort(); } StringBuilder sb = new StringBuilder(); foreach (String key in list) { String value = para[key]; if (encode && value != null) { try { value = HttpUtility.UrlEncode(value, Encoding.GetEncoding(UpmpConfig.GetInstance().CHARSET)); } catch (Exception ex) { //LogError(ex); return("#ERROR: HttpUtility.UrlEncode Error!" + ex.Message); } } sb.Append(key).Append(QSTRING_EQUAL).Append(value).Append(QSTRING_SPLIT); } return(sb.Remove(sb.Length - 1, 1).ToString()); }
/// <summary> /// 交易查询处理 /// </summary> /// <param name="req">请求要素</param> /// <param name="resp">应答要素</param> /// <returns>是否成功</returns> public static bool Query(Dictionary <String, String> req, Dictionary <String, String> resp) { String nvp = BuildReq(req, resp); String respString = UpmpCore.Post(UpmpConfig.GetInstance().QUERY_URL, nvp); return(VerifyResponse(respString, resp)); }
/// <summary> /// 生成签名 /// </summary> /// <param name="req">需要签名的要素</param> /// <returns>签名结果字符串</returns> public static String BuildSignature(Dictionary <String, String> req) { // 除去数组中的空值和签名参数 Dictionary <String, String> para = ParaFilter(req); String prestr = CreateLinkString(para, true, false); prestr = prestr + QSTRING_SPLIT + MD5(UpmpConfig.GetInstance().SECURITY_KEY); return(MD5(prestr)); }
/// <summary> /// 获取UpmpConfig单例 /// </summary> /// <returns></returns> public static UpmpConfig GetInstance() { if (_instance == null) { _instance = new UpmpConfig(); } return(_instance); }
/// <summary> /// 异步通知消息验证 /// </summary> /// <param name="para">异步通知消息</param> /// <returns>验证结果</returns> public static bool VerifySignature(Dictionary <String, String> para) { String signature = UpmpCore.BuildSignature(para); String respSignature = para[UpmpConfig.GetInstance().SIGNATURE]; if (null != respSignature && respSignature.Equals(signature)) { return(true); } return(false); }
/// <summary> /// 拼接请求字符串 /// </summary> /// <param name="req"></param> /// <param name="resp"></param> /// <returns></returns> private static String BuildReq(Dictionary <String, String> req, Dictionary <String, String> resp) { // 生成签名结果 String signature = UpmpCore.BuildSignature(req); // 签名结果与签名方式加入请求提交参数组中 req[UpmpConfig.GetInstance().SIGNATURE] = signature; req[UpmpConfig.GetInstance().SIGN_METHOD] = UpmpConfig.GetInstance().SIGN_TYPE; return(UpmpCore.CreateLinkString(req, false, true)); }
/// <summary> /// 计算MD5 /// </summary> /// <param name="str">计算MD5的输入字符串</param> /// <returns>MD5后的结果</returns> public static string MD5(string str) { byte[] MD5Source = Encoding.GetEncoding(UpmpConfig.GetInstance().CHARSET).GetBytes(str); System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hash_byte = get_md5.ComputeHash(MD5Source, 0, MD5Source.Length); string result = System.BitConverter.ToString(hash_byte); result = result.Replace("-", ""); return(result.ToLower()); }
public TradeResult PayQuery(TradeQueryRequest traderequest) { bool tradesuccess = false; // 请求要素 Dictionary <String, String> req = new Dictionary <String, String>(); req["version"] = UpmpConfig.GetInstance().VERSION; // 版本号 req["charset"] = UpmpConfig.GetInstance().CHARSET; // 字符编码 req["transType"] = "01"; // 交易类型 req["merId"] = UpmpConfig.GetInstance().MER_ID; // 商户代码 req["orderTime"] = traderequest.TradeDate.ToString("yyyyMMddHHmmss"); // 交易开始日期时间yyyyMMddHHmmss req["orderNumber"] = traderequest.TradeNO; // 订单号(商户根据自己需要生成订单号) Dictionary <String, String> resp = new Dictionary <String, String>(); if (UpmpService.Query(req, resp)) { // 服务器应答签名验证成功 if (resp["respCode"] == "00" && resp.ContainsKey("transStatus") && resp["transStatus"] == "00") { //StringBuilder rsinpfo = new StringBuilder(); //StringBuilder sbreq = new StringBuilder(); //foreach (var item in req.Keys) //{ // sbreq.Append(item + "=" + req[item] + "&"); //} //StringBuilder sbresp = new StringBuilder(); //foreach (var item in resp.Keys) //{ // sbresp.Append(item + "=" + resp[item] + "&"); //} //System.Diagnostics.Debug.WriteLine(sbreq.ToString()); //System.Diagnostics.Debug.WriteLine(sbresp.ToString()); tradesuccess = true; } } TradeResult t = new TradeResult() { Code = 1, TradeNO = traderequest.TradeNO, PayStatus = tradesuccess ? PayStatus.Paid : PayStatus.UnPaid }; return(t); }
public NetAppPayPackage ChoosePay(TradeModel trade) { NetAppPayPackage package = new NetAppPayPackage() { tn = "" }; // 请求要素 Dictionary <String, String> req = new Dictionary <String, String>(); req["version"] = UpmpConfig.GetInstance().VERSION; // 版本号 req["charset"] = UpmpConfig.GetInstance().CHARSET; // 字符编码 req["transType"] = "01"; // 交易类型 req["merId"] = UpmpConfig.GetInstance().MER_ID; // 商户代码 req["backEndUrl"] = UpmpConfig.GetInstance().MER_BACK_END_URL; // 通知URL req["frontEndUrl"] = UpmpConfig.GetInstance().MER_FRONT_END_URL; // 前台通知URL(可选) req["orderDescription"] = trade.Name; // 订单描述(可选) req["orderTime"] = trade.CreateTime.ToString("yyyyMMddHHmmss"); // 交易开始日期时间yyyyMMddHHmmss //req["orderTimeout"] = trade.CreateTime.AddMinutes(SysConfig.OrderValidMins).ToString("yyyyMMddHHmmss");// 订单超时时间yyyyMMddHHmmss(可选) req["orderNumber"] = trade.TradeNO; // 订单号(商户根据自己需要生成订单号) req["orderAmount"] = trade.Payment.ToString("#0.00").Replace(".", ""); // 订单金额 req["orderCurrency"] = "156"; // 交易币种(可选) req["reqReserved"] = ""; // 请求方保留域(可选,用于透传商户信息) // 保留域填充方法 //Dictionary<String, String> merReservedMap = new Dictionary<String, String>(); //merReservedMap["account"] = order.Idcard + "-" + order.Bankcard; //req["merReserved"] = UpmpService.BuildReserved(merReservedMap);// 商户保留域(可选) Dictionary <String, String> resp = new Dictionary <String, String>(); //logger.Debug("下单请求:" + string.Join(",", req.Keys) + "==>" + string.Join(",", req.Values)); bool validResp = UpmpService.Trade(req, resp); //logger.Debug("下单应答:" + string.Join(",", resp.Keys) + "==>" + string.Join(",", resp.Values)); // 验证通过 if (validResp) { package.respCode = resp["respCode"]; package.respMsg = resp.ContainsKey("respMsg") ? resp["respMsg"] : ""; package.tn = resp["tn"]; package.transType = resp["transType"]; package.charset = resp["charset"]; package.signature = resp["signature"]; package.signMethod = resp["signMethod"]; } return(package); }
/// <summary> /// 除去请求要素中的空值和签名参数 /// </summary> /// <param name="para">请求要素</param> /// <returns>去掉空值与签名参数后的请求要素</returns> public static Dictionary <String, String> ParaFilter(Dictionary <String, String> para) { Dictionary <String, String> result = new Dictionary <String, String>(); if (para == null || para.Count <= 0) { return(result); } foreach (String key in para.Keys) { String value = para[key]; if (value == null || value.Equals("") || String.Equals(key, UpmpConfig.GetInstance().SIGNATURE, StringComparison.CurrentCultureIgnoreCase) || String.Equals(key, UpmpConfig.GetInstance().SIGN_METHOD, StringComparison.CurrentCultureIgnoreCase)) { continue; } result[key] = value; } return(result); }
private static void putKeyValueToDictionary(StringBuilder temp, bool isKey, String key, Dictionary <String, String> Dictionary) { if (isKey) { key = temp.ToString(); if (key.Length == 0) { throw new System.Exception("QString format illegal"); } Dictionary[key] = ""; } else { if (key.Length == 0) { throw new System.Exception("QString format illegal"); } Dictionary[key] = HttpUtility.UrlDecode(temp.ToString(), Encoding.GetEncoding(UpmpConfig.GetInstance().CHARSET)); } }
/// <summary> /// 获取UpmpConfig单例 /// </summary> /// <returns></returns> public static UpmpConfig GetInstance() { if (_instance == null) { _instance = new UpmpConfig(); } return _instance; }