AlipayHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using Aop.Api;
  2. using Aop.Api.Domain;
  3. using Aop.Api.Request;
  4. using Aop.Api.Response;
  5. using EMIS.Utility;
  6. using EMIS.Utility.OnlinePay.Alipay;
  7. using EMIS.Utility.OnlinePay.Alipay.Models;
  8. using Newtonsoft.Json.Linq;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Drawing.Imaging;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Web;
  17. using System.Web.Mvc;
  18. using System.Xml.Serialization;
  19. namespace EMIS.Utility.OnlinePay
  20. {
  21. public class AlipayHelper
  22. {
  23. 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);
  24. public static FileContentResult GetPayQRCode(string productId, decimal fee, string feeTypeName)
  25. {
  26. List<GoodsDetail> gList = new List<GoodsDetail>();
  27. GoodsDetail goods = new GoodsDetail();
  28. goods.GoodsId = productId;
  29. goods.GoodsName = feeTypeName;
  30. goods.Price = fee.ToString("#.00");
  31. goods.Quantity = 1;
  32. gList.Add(goods);
  33. AlipayTradePrecreateModel model = new AlipayTradePrecreateModel()
  34. {
  35. //收款账号
  36. SellerId = Config.pid,
  37. //订单编号
  38. OutTradeNo = productId,
  39. //订单总金额
  40. TotalAmount = fee.ToString("#.00").Trim(),
  41. //订单名称
  42. Subject = feeTypeName,
  43. //自定义超时时间
  44. TimeoutExpress = "5m",
  45. //订单描述
  46. Body = "",
  47. //门店编号,很重要的参数,可以用作之后的营销
  48. StoreId = "01",
  49. //操作员编号,很重要的参数,可以用作之后的营销
  50. OperatorId = "01",
  51. //传入商品信息详情
  52. GoodsDetail = gList
  53. };
  54. string notify_url = "http://liot.bowintek.com/alipay/PayNotify"; //商户接收异步通知的地址
  55. AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
  56. request.BizContent = new AopModelParser().serializeAopObject(model).ToString();
  57. request.SetNotifyUrl(notify_url);
  58. AlipayTradePrecreateResponse precreateResult = client.Execute(request);
  59. switch (precreateResult.Code)
  60. {
  61. case ResultCode.SUCCESS:
  62. var memoryStream = new MemoryStream();
  63. var qrcodeImage = QRCodeHelper.GenerateQRCode(precreateResult.QrCode);
  64. qrcodeImage.Save(memoryStream, ImageFormat.Png);
  65. return new FileContentResult(memoryStream.GetBuffer(), "image/png");
  66. case ResultCode.FAIL:
  67. throw new Exception(precreateResult.Body);
  68. case ResultCode.ERROR:
  69. default:
  70. if (precreateResult == null)
  71. {
  72. throw new Exception("配置或网络异常,请检查后重试");
  73. }
  74. else
  75. {
  76. throw new Exception("系统异常,请更新外部订单后重新发起请求");
  77. }
  78. }
  79. }
  80. public static void CloseOrder(string productId)
  81. {
  82. AlipayTradeCloseModel model = new AlipayTradeCloseModel()
  83. {
  84. OutTradeNo = productId,
  85. OperatorId = "01"
  86. };
  87. AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
  88. request.BizContent = new AopModelParser().serializeAopObject(model).ToString();
  89. AlipayTradeCloseResponse closeResult = client.Execute(request);
  90. switch (closeResult.Code)
  91. {
  92. case ResultCode.FAIL:
  93. throw new Exception(closeResult.Body);
  94. case ResultCode.ERROR:
  95. default:
  96. if (closeResult == null)
  97. {
  98. throw new Exception("配置或网络异常,请检查后重试");
  99. }
  100. else
  101. {
  102. throw new Exception("系统异常,请更新外部订单后重新发起请求");
  103. }
  104. }
  105. }
  106. public static AlipayTradeQueryResponse OrderQuery(string productId)
  107. {
  108. AlipayTradeQueryModel model = new AlipayTradeQueryModel();
  109. model.OutTradeNo = productId;
  110. AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
  111. request.BizContent = new AopModelParser().serializeAopObject(model).ToString();
  112. var result = client.Execute(request);
  113. return result;
  114. }
  115. public static bool IsTradeFinished(string tradeStatus)
  116. {
  117. if (tradeStatus == "TRADE_CLOSED"
  118. || tradeStatus == "TRADE_SUCCESS"
  119. || tradeStatus == "TRADE_FINISHED")
  120. {
  121. return true;
  122. }
  123. else
  124. {
  125. return false;
  126. }
  127. }
  128. }
  129. public class AopModelParser
  130. {
  131. /// <summary>
  132. /// Json序列化AopObject对象
  133. /// </summary>
  134. /// <param name="res"></param>
  135. /// <returns></returns>
  136. public JObject serializeAopObject(AopObject res)
  137. {
  138. PropertyInfo[] pis = res.GetType().GetProperties();
  139. JObject jo = new JObject();
  140. foreach (PropertyInfo pi in pis)
  141. {
  142. if (!pi.CanRead)
  143. {
  144. continue;
  145. }
  146. String elementName = getElementName(pi);
  147. Object value = getPiValue(res, pi);
  148. if (!String.IsNullOrEmpty(elementName))
  149. {
  150. Object serialized = serializeValue(value);
  151. if (serialized != null)
  152. {
  153. jo.Add(elementName, JToken.FromObject( serialized));
  154. }
  155. }
  156. }
  157. return jo;
  158. }
  159. /// <summary>
  160. /// 序列化某个对象
  161. /// </summary>
  162. /// <param name="value"></param>
  163. /// <returns></returns>
  164. private Object serializeValue(Object value)
  165. {
  166. if (value == null)
  167. {
  168. return null;
  169. }
  170. Object result = null;
  171. if (value is IDictionary)
  172. {
  173. result = serializeDicValue((IDictionary)value);
  174. }
  175. else if (value is ICollection)
  176. {
  177. result = serializeArrayValue((ICollection)value);
  178. }
  179. else
  180. {
  181. result = serializeElementValue(value);
  182. }
  183. return result;
  184. }
  185. /// <summary>
  186. /// 序列化一个元素
  187. /// </summary>
  188. /// <param name="value"></param>
  189. /// <returns></returns>
  190. private Object serializeElementValue(Object value)
  191. {
  192. if (value == null)
  193. {
  194. return null;
  195. }
  196. Object result = null;
  197. if (value is AopObject)
  198. {
  199. result = serializeAopObject((AopObject)value);
  200. }
  201. else
  202. {
  203. result = value;
  204. }
  205. return result;
  206. }
  207. /// <summary>
  208. /// 序列化一个字典
  209. /// </summary>
  210. /// <param name="dic"></param>
  211. /// <returns></returns>
  212. private Object serializeDicValue(IDictionary dic)
  213. {
  214. if (dic == null)
  215. {
  216. return null;
  217. }
  218. JObject jo = new JObject();
  219. foreach (String key in dic.Keys)
  220. {
  221. Object dicValue = dic[key];
  222. Object serializedValue = serializeValue(dicValue);
  223. jo.Add(key, JToken.FromObject(serializedValue));
  224. }
  225. return jo;
  226. }
  227. /// <summary>
  228. /// 序列化一个数组
  229. /// </summary>
  230. /// <param name="collection"></param>
  231. /// <returns></returns>
  232. private Object serializeArrayValue(ICollection collection)
  233. {
  234. if (collection == null)
  235. {
  236. return null;
  237. }
  238. JArray ja = new JArray();
  239. foreach (var item in collection)
  240. {
  241. ja.Add(serializeValue(item));
  242. }
  243. return ja;
  244. }
  245. /// <summary>
  246. /// 获取对象的属性值
  247. /// </summary>
  248. /// <param name="res"></param>
  249. /// <param name="pi"></param>
  250. /// <returns></returns>
  251. private Object getPiValue(Object res, PropertyInfo pi)
  252. {
  253. if (!pi.CanRead)
  254. {
  255. return null;
  256. }
  257. Object value = pi.GetValue(res, null);
  258. return value;
  259. }
  260. /// <summary>
  261. /// 获取Xml属性名
  262. /// </summary>
  263. /// <param name="pi"></param>
  264. /// <returns></returns>
  265. private String getElementName(PropertyInfo pi)
  266. {
  267. if (pi == null)
  268. {
  269. return null;
  270. }
  271. // 获取XmlElement属性
  272. XmlElementAttribute[] xeas = pi.GetCustomAttributes(typeof(XmlElementAttribute), true) as XmlElementAttribute[];
  273. String elementName = null;
  274. if (xeas != null && xeas.Length > 0)
  275. {
  276. elementName = xeas[0].ElementName;
  277. }
  278. // 如果获取XmlElement属性为空,则去获取XmlArray属性
  279. if (String.IsNullOrEmpty(elementName))
  280. {
  281. XmlArrayAttribute[] xaas = pi.GetCustomAttributes(typeof(XmlArrayAttribute), true) as XmlArrayAttribute[];
  282. if (xaas != null && xaas.Length > 0)
  283. {
  284. elementName = xaas[0].ElementName;
  285. }
  286. }
  287. return elementName;
  288. }
  289. }
  290. }