//====================================================================== // InterfaceController // ZMF_Demo.ZMF // // Created by Chen Jialuo on 3/2/2017 4:45:40 PM. // Copyright © 2017 ZHONGMAKEJI. All rights reserved. // http://www.zhongmakj.com/ // //====================================================================== //------版本-----------更新日期-----------------适用接口文档版本---------- //------v1.0-----------2017.03.03--------------v2.0--------------------- //====================================================================== /* * 注释下方define语句以禁用JSON解析 * 若要启用JSON解析,使用前须先将第三方组件Newtonsoft.Json.dll导入至工程。 Newtonsoft.Json网址: http://www.newtonsoft.com/json * 此Demo工程业已包含上述第三方组件 * 禁用JSON解析则无需导入上述组件 * 启用JSON解析时 各个接口调用方法返回JObject对象 * 禁用JSON解析时 各个接口调用方法返回string变量 */ #define ZMF_SHOULD_USE_JSON using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; #if ZMF_SHOULD_USE_JSON using Newtonsoft.Json; using Newtonsoft.Json.Linq; #endif namespace MobileServicePlatform.Common { /// /// API接口控制器 /// public sealed class InterfaceController { #region 接口配置 // TODO: 配置填写,请先将合作方的APP_ID和KEY在此处填写完整 /// /// 服务器地址 /// private const string ZMF_SERVER_HOST = "http://118.178.235.230/open-api"; /// /// APP ID /// private const string ZMF_CONFIG_APPID = "G0000032"; /// /// KEY /// private const string ZMF_CONFIG_KEY = "8163372a618170ae27d91772c62117d1"; #endregion #region 构造方法 /// /// 不需要新建对象,构造函数为私有 /// private InterfaceController() { ; } #endregion #region 公共方法 /// /// 获取签名 /// /// 键值对内容 /// 签名值 private static string GetSign(Hashtable param) { /* * *******这段注释的内容运行结果与下方代码相同******* //结果字符串 string finalString = ""; //键名排序 ArrayList al = new ArrayList(param.Keys); al.Sort(); //加入字符串 foreach (string key in al) { if (param[key] != null && param[key].ToString() != "") finalString += key + "=" + param[key].ToString() + "&"; } //最后加入appid和key finalString += string.Format("appId={0}&key={1}", ZMF_CONFIG_APPID, ZMF_CONFIG_KEY); //MD5 MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(Encoding.UTF8.GetBytes(finalString)); finalString = BitConverter.ToString(result).Replace("-", "").ToLower(); return finalString; */ return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(string.Join("&", param.Cast().Where(x => x.Value != null && x.Value.ToString() != "").OrderBy(x => x.Key).Union(new List { new DictionaryEntry("appId", ZMF_CONFIG_APPID), new DictionaryEntry("key", ZMF_CONFIG_KEY) }).Select(x => string.Format("{0}={1}", x.Key, x.Value)))))).Replace("-", "").ToLower(); } /// /// 同步方式发送请求 /// /// 方法名,使用System.Reflection.MethodBase.GetCurrentMethod().Name来传入 /// 接口url具体名称 /// 参数 /// 调用成功则返回接口返回内容;失败时返回null #if ZMF_SHOULD_USE_JSON private static JObject Access(string method, string ifName, params object[] list) { #else private static string Access(string method, string ifName, params object[] list) { #endif //建立参数字典 Hashtable param = new Hashtable(); //参数名列表 ParameterInfo[] pis = typeof(InterfaceController).GetMethod(method).GetParameters(); //传入有误 if (pis == null || (list != null && pis.Length != list.Length)) { // TODO: 传入参数有误,错误日志写入 return null; } //键值对加入 for (var i = 0; i < pis.Length; i++) { param.Add(pis[i].Name, list[i].ToString()); } //加入签名 param.Add("signature", GetSign(param)); //加入APP ID param.Add("appId", ZMF_CONFIG_APPID); //转为NameValueCollection NameValueCollection nvc = new NameValueCollection(); foreach (DictionaryEntry entry in param) { nvc.Add(entry.Key.ToString(), entry.Value.ToString()); } //完整URL string url = ZMF_SERVER_HOST + ifName; #if ZMF_SHOULD_USE_JSON //调用HTTP POST 并进行JSON解析 try { return JsonConvert.DeserializeObject(HttpHelper.Post(url, nvc)); } catch { // TODO: JSON解析失败,错误日志写入 return null; } #else return HttpHelper.Post(url, nvc); #endif } #endregion #region 各个接口 /// /// 商户注册 /// /// 商户名称 /// 商家地址 /// 商家手机号 /// 商家姓名 /// 商家身份证号 /// 商家审核状态回调接口 /// 业务类别1 /// 业务类别2 /// MCC码 /// 区编号 /// 省编号 /// 市编号 /// 营业执照号 /// 营业执照有效期 /// #if ZMF_SHOULD_USE_JSON public static JObject StoreAdd(string merchantName, string address, string mobile, string applicantName, string applicantIdno, string notifyUrl, string businessCatagory1, string businessCatagory2, string mcc, string area, string prov, string city, string licenseno, string licensedate) { #else public static string StoreAdd(string merchantName, string address, string mobile, string applicantName, string applicantIdno, string notifyUrl, string businessCatagory1, string businessCatagory2, string mcc, string area, string prov, string city, string licenseno, string licensedate) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/storeInfo/add", merchantName, address, mobile, applicantName, applicantIdno, notifyUrl, businessCatagory1, businessCatagory2, mcc, area, prov, city, licenseno, licensedate); } /// /// 商户基本信息更新 /// /// 商户编码 /// 商户名称 /// 商家地址 /// 商家手机号 /// 商家姓名 /// 商家身份证号 /// 商家审核状态回调接口 /// 业务类别1 /// 业务类别2 /// MCC码 /// #if ZMF_SHOULD_USE_JSON public static JObject StoreModify(string memberCode, string merchantName, string address, string mobile, string applicantName, string applicantIdno, string notifyUrl, string businessCatagory1, string businessCatagory2, string mcc) { #else public static string StoreModify(string memberCode, string merchantName, string address, string mobile, string applicantName, string applicantIdno, string notifyUrl, string businessCatagory1, string businessCatagory2, string mcc) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/storeInfo/modify", memberCode, merchantName, address, mobile, applicantName, applicantIdno, notifyUrl, businessCatagory1, businessCatagory2, mcc); } /// /// 设置申请人结算卡信息 /// /// 商户名称 /// 商家编号 /// 手机号 /// 支行名称 /// 支行号 /// 卡号 /// 银行名 /// #if ZMF_SHOULD_USE_JSON public static JObject SetCard(string name, string memberCode, string mobile, string branchName, string branchNo, string cardNo, string bankName) { #else public static string SetCard(string name, string memberCode, string mobile, string branchName, string branchNo, string cardNo, string bankName) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/applicant/setCard", name, memberCode, mobile, branchName, branchNo, cardNo, bankName); } /// /// 商户绑定 /// /// 商家编号 /// 合作商号 /// #if ZMF_SHOULD_USE_JSON public static JObject StoreBand(string memberCode, string partnerUser) { #else public static string StoreBand(string memberCode, string partnerUser) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/store/band", memberCode, partnerUser); } /// /// 交易订单详情 /// /// 订单号 /// 商家编号 /// #if ZMF_SHOULD_USE_JSON public static JObject BillDetail(string orderNo, string memberCode) { #else public static string BillDetail(string orderNo, string memberCode) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/bill/detail", orderNo, memberCode); } /// /// 交易记录列表 /// /// 商家编号 /// 起始时间 /// 结束时间 /// 页索引号 /// 行数 /// #if ZMF_SHOULD_USE_JSON public static JObject BillLists(string memberCode, string start, string end, string page, string rows) { #else public static string BillLists(string memberCode, string start, string end, string page, string rows) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/bill/lists", memberCode, start, end, page, rows); } /// /// MCC码列表 /// /// #if ZMF_SHOULD_USE_JSON public static JObject MccLists() { #else public static string MccLists() { #endif return Access(MethodBase.GetCurrentMethod().Name, "/mcc/lists", null); } /// /// 商户一级类别列表 /// /// #if ZMF_SHOULD_USE_JSON public static JObject MccLevel1() { #else public static string MccLevel1() { #endif return Access(MethodBase.GetCurrentMethod().Name, "/mcc/level1", null); } /// /// 商户二级类别列表 /// /// 商户一级类别id /// #if ZMF_SHOULD_USE_JSON public static JObject MccLevel2(string level1Id) { #else public static string MccLevel2(string level1Id) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/mcc/level2", level1Id); } /// /// 图片上传 /// /// 商家编号 /// 商店内部照片URL /// 银行卡照片URL /// 营业执照URL /// 身份证正面照URL /// 身份证反面照URL /// 手持身份证半身照URL /// 申请人与门店合照URL /// #if ZMF_SHOULD_USE_JSON public static JObject UploadImage(string memberCode, string storeInnerImageUrl, string storeBankCardImageUrl, string storeBusinessImageUrl, string idCardFontImageUrl, string idCardBackImageUrl, string holdCardImageUrl, string storeImageUrl) { #else public static string UploadImage(string memberCode, string storeInnerImageUrl, string storeBankCardImageUrl, string storeBusinessImageUrl, string idCardFontImageUrl, string idCardBackImageUrl, string holdCardImageUrl, string storeImageUrl) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/storeInfo/imageUpload", memberCode, storeInnerImageUrl, storeBankCardImageUrl, storeBusinessImageUrl, idCardFontImageUrl, idCardBackImageUrl, holdCardImageUrl, storeImageUrl); } /// /// 商家信息获取 /// /// 商家编号 /// #if ZMF_SHOULD_USE_JSON public static JObject StoreInfo(string memberCode) { #else public static string StoreInfo(string memberCode) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/storeInfo/get", memberCode); } /// /// 省份列表获取 /// /// #if ZMF_SHOULD_USE_JSON public static JObject Province() { #else public static string Province() { #endif return Access(MethodBase.GetCurrentMethod().Name, "/card/province", null); } /// /// 省份下城市列表获取 /// /// 省份编号 /// #if ZMF_SHOULD_USE_JSON public static JObject City(string provinceId) { #else public static string City(string provinceId) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/card/city", provinceId); } /// /// 卡信息获取 /// /// 卡号 /// #if ZMF_SHOULD_USE_JSON public static JObject CardInfo(string cardNo) { #else public static string CardInfo(string cardNo) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/card/cardinfo", cardNo); } /// /// 卡所在支行信息获取 /// /// 卡号 /// 城市名称 /// 查询关键字 /// #if ZMF_SHOULD_USE_JSON public static JObject CardBranch(string cardNo, string city, string keyWord) { #else public static string CardBranch(string cardNo, string city, string keyWord) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/card/cardbranchNo", cardNo, city, keyWord); } /// /// 用户扫商家(正扫支付)微信 /// /// 商家编号 /// 交易对象 /// 交易结果通知接口Url /// 优惠金额 /// 实收金额 /// 外部订单号 /// #if ZMF_SHOULD_USE_JSON public static JObject QRCodePayWechat(string memberCode, string subject, string notifyUrl, string discountAmt, string transAmt, string orderNo) { #else public static string QRCodePayWechat(string memberCode, string subject, string notifyUrl, string discountAmt, string transAmt, string orderNo) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/unionpay/customerscan/type/wechat", memberCode, subject, notifyUrl, discountAmt, transAmt, orderNo); } /// /// 用户扫商家(正扫支付)支付宝 /// /// 商家编号 /// 交易对象 /// 交易结果通知接口Url /// 优惠金额 /// 实收金额 /// 外部订单号 /// #if ZMF_SHOULD_USE_JSON public static JObject QRCodePayAlipay(string memberCode, string subject, string notifyUrl, string discountAmt, string transAmt, string orderNo) { #else public static string QRCodePayAlipay(string memberCode, string subject, string notifyUrl, string discountAmt, string transAmt, string orderNo) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/unionpay/customerscan/type/alipay", memberCode, subject, notifyUrl, discountAmt, transAmt, orderNo); } /// /// 商家扫用户(反扫支付) /// /// 商家编号 /// 交易对象 /// 交易结果通知接口Url /// 优惠金额 /// 实收金额 /// 用户支付码 /// 交易订单号 /// #if ZMF_SHOULD_USE_JSON public static JObject BarcodePay(string memberCode, string subject, string notifyUrl, string discountAmt, string transAmt, string qrcode, string orderNo) { #else public static string BarcodePay(string memberCode, string subject, string notifyUrl, string discountAmt, string transAmt, string qrcode, string orderNo) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/unionpay/merchantscan/type/" + (qrcode.StartsWith("1") ? "wechat" : "alipay"), memberCode, subject, notifyUrl, discountAmt, transAmt, qrcode, orderNo); } /// /// JS支付 微信 /// /// 商家编号 /// 交易结果通知接口Url /// 优惠金额 /// 实收金额 /// 用户支付ID /// 外部订单号 /// 交易对象 /// 跳转页面 /// #if ZMF_SHOULD_USE_JSON public static JObject WebPayWechat(string memberCode, string notifyUrl, string discountAmt, string transAmt, string userid, string orderNo, string subject, string returnUrl) { #else public static string WebPayWechat(string memberCode, string notifyUrl, string discountAmt, string transAmt, string userid, string orderNo, string subject, string returnUrl) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/unionpay/webpay/type/wechat", memberCode, notifyUrl, discountAmt, transAmt, userid, orderNo, subject, returnUrl); } /// /// JS支付 支付宝 /// /// 商家编号 /// 交易结果通知接口Url /// 优惠金额 /// 实收金额 /// 用户支付ID /// 外部订单号 /// 交易对象 /// 跳转页面 /// #if ZMF_SHOULD_USE_JSON public static JObject WebPayAlipay(string memberCode, string notifyUrl, string discountAmt, string transAmt, string userid, string orderNo, string subject, string returnUrl) { #else public static string WebPayAlipay(string memberCode, string notifyUrl, string discountAmt, string transAmt, string userid, string orderNo, string subject, string returnUrl) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/unionpay/webpay/type/alipay", memberCode, notifyUrl, discountAmt, transAmt, userid, orderNo, subject, returnUrl); } /// /// 退款 /// /// 退款单号 /// 商家编号 /// #if ZMF_SHOULD_USE_JSON public static JObject Refund(string authcode, string memberCode) { #else public static string Refund(string authcode, string memberCode) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/refund/authcode", authcode, memberCode); } /// /// 扣率初始化 /// /// 商家编号 /// 扣率 /// #if ZMF_SHOULD_USE_JSON public static JObject SetRate(string memberCode, string rate) { #else public static string SetRate(string memberCode, string rate) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/store/setRate", memberCode, rate); } /// /// 查询省市区列表 /// /// 查询对象 /// 查询编码 /// #if ZMF_SHOULD_USE_JSON public static JObject AdministrativeDivisions(string type, string code) { #else public static string AdministrativeDivisions(string type, string code) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/channeladdr/lists", type, code); } /// /// 查询退款单号 /// /// 退款单号 /// #if ZMF_SHOULD_USE_JSON public static JObject QueryRefund(string authcode) { #else public static JObject QueryRefund(string authcode) { #endif return Access(MethodBase.GetCurrentMethod().Name, "/refund/query", authcode); } #endregion } }