using Aop.Api; using Aop.Api.Domain; using Aop.Api.Request; using Aop.Api.Response; using EMIS.Utility; using EMIS.Utility.OnlinePay.Alipay; using EMIS.Utility.OnlinePay.Alipay.Models; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Reflection; using System.Web; using System.Web.Mvc; using System.Xml.Serialization; namespace EMIS.Utility.OnlinePay { public class AlipayHelper { static IAopClient client = new DefaultAopClient(Config.serverUrl, Config.appId, Config.merchant_private_key, "json", Config.version, Config.sign_type, Config.alipay_public_key, Config.charset, false); public static FileContentResult GetPayQRCode(string productId, decimal fee, string feeTypeName) { List gList = new List(); GoodsDetail goods = new GoodsDetail(); goods.GoodsId = productId; goods.GoodsName = feeTypeName; goods.Price = fee.ToString("#.00"); goods.Quantity = 1; gList.Add(goods); AlipayTradePrecreateModel model = new AlipayTradePrecreateModel() { //收款账号 SellerId = Config.pid, //订单编号 OutTradeNo = productId, //订单总金额 TotalAmount = fee.ToString("#.00").Trim(), //订单名称 Subject = feeTypeName, //自定义超时时间 TimeoutExpress = "5m", //订单描述 Body = "", //门店编号,很重要的参数,可以用作之后的营销 StoreId = "01", //操作员编号,很重要的参数,可以用作之后的营销 OperatorId = "01", //传入商品信息详情 GoodsDetail = gList }; string notify_url = "http://liot.bowintek.com/alipay/PayNotify"; //商户接收异步通知的地址 AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); request.BizContent = new AopModelParser().serializeAopObject(model).ToString(); request.SetNotifyUrl(notify_url); AlipayTradePrecreateResponse precreateResult = client.Execute(request); switch (precreateResult.Code) { case ResultCode.SUCCESS: var memoryStream = new MemoryStream(); var qrcodeImage = QRCodeHelper.GenerateQRCode(precreateResult.QrCode); qrcodeImage.Save(memoryStream, ImageFormat.Png); return new FileContentResult(memoryStream.GetBuffer(), "image/png"); case ResultCode.FAIL: throw new Exception(precreateResult.Body); case ResultCode.ERROR: default: if (precreateResult == null) { throw new Exception("配置或网络异常,请检查后重试"); } else { throw new Exception("系统异常,请更新外部订单后重新发起请求"); } } } public static void CloseOrder(string productId) { AlipayTradeCloseModel model = new AlipayTradeCloseModel() { OutTradeNo = productId, OperatorId = "01" }; AlipayTradeCloseRequest request = new AlipayTradeCloseRequest(); request.BizContent = new AopModelParser().serializeAopObject(model).ToString(); AlipayTradeCloseResponse closeResult = client.Execute(request); switch (closeResult.Code) { case ResultCode.FAIL: throw new Exception(closeResult.Body); case ResultCode.ERROR: default: if (closeResult == null) { throw new Exception("配置或网络异常,请检查后重试"); } else { throw new Exception("系统异常,请更新外部订单后重新发起请求"); } } } public static AlipayTradeQueryResponse OrderQuery(string productId) { AlipayTradeQueryModel model = new AlipayTradeQueryModel(); model.OutTradeNo = productId; AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.BizContent = new AopModelParser().serializeAopObject(model).ToString(); var result = client.Execute(request); return result; } public static bool IsTradeFinished(string tradeStatus) { if (tradeStatus == "TRADE_CLOSED" || tradeStatus == "TRADE_SUCCESS" || tradeStatus == "TRADE_FINISHED") { return true; } else { return false; } } } public class AopModelParser { /// /// Json序列化AopObject对象 /// /// /// public JObject serializeAopObject(AopObject res) { PropertyInfo[] pis = res.GetType().GetProperties(); JObject jo = new JObject(); foreach (PropertyInfo pi in pis) { if (!pi.CanRead) { continue; } String elementName = getElementName(pi); Object value = getPiValue(res, pi); if (!String.IsNullOrEmpty(elementName)) { Object serialized = serializeValue(value); if (serialized != null) { jo.Add(elementName, JToken.FromObject( serialized)); } } } return jo; } /// /// 序列化某个对象 /// /// /// private Object serializeValue(Object value) { if (value == null) { return null; } Object result = null; if (value is IDictionary) { result = serializeDicValue((IDictionary)value); } else if (value is ICollection) { result = serializeArrayValue((ICollection)value); } else { result = serializeElementValue(value); } return result; } /// /// 序列化一个元素 /// /// /// private Object serializeElementValue(Object value) { if (value == null) { return null; } Object result = null; if (value is AopObject) { result = serializeAopObject((AopObject)value); } else { result = value; } return result; } /// /// 序列化一个字典 /// /// /// private Object serializeDicValue(IDictionary dic) { if (dic == null) { return null; } JObject jo = new JObject(); foreach (String key in dic.Keys) { Object dicValue = dic[key]; Object serializedValue = serializeValue(dicValue); jo.Add(key, JToken.FromObject(serializedValue)); } return jo; } /// /// 序列化一个数组 /// /// /// private Object serializeArrayValue(ICollection collection) { if (collection == null) { return null; } JArray ja = new JArray(); foreach (var item in collection) { ja.Add(serializeValue(item)); } return ja; } /// /// 获取对象的属性值 /// /// /// /// private Object getPiValue(Object res, PropertyInfo pi) { if (!pi.CanRead) { return null; } Object value = pi.GetValue(res, null); return value; } /// /// 获取Xml属性名 /// /// /// private String getElementName(PropertyInfo pi) { if (pi == null) { return null; } // 获取XmlElement属性 XmlElementAttribute[] xeas = pi.GetCustomAttributes(typeof(XmlElementAttribute), true) as XmlElementAttribute[]; String elementName = null; if (xeas != null && xeas.Length > 0) { elementName = xeas[0].ElementName; } // 如果获取XmlElement属性为空,则去获取XmlArray属性 if (String.IsNullOrEmpty(elementName)) { XmlArrayAttribute[] xaas = pi.GetCustomAttributes(typeof(XmlArrayAttribute), true) as XmlArrayAttribute[]; if (xaas != null && xaas.Length > 0) { elementName = xaas[0].ElementName; } } return elementName; } } }