123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- 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<GoodsDetail> gList = new List<GoodsDetail>();
- 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
- {
- /// <summary>
- /// Json序列化AopObject对象
- /// </summary>
- /// <param name="res"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 序列化某个对象
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 序列化一个元素
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 序列化一个字典
- /// </summary>
- /// <param name="dic"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 序列化一个数组
- /// </summary>
- /// <param name="collection"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取对象的属性值
- /// </summary>
- /// <param name="res"></param>
- /// <param name="pi"></param>
- /// <returns></returns>
- private Object getPiValue(Object res, PropertyInfo pi)
- {
- if (!pi.CanRead)
- {
- return null;
- }
- Object value = pi.GetValue(res, null);
- return value;
- }
- /// <summary>
- /// 获取Xml属性名
- /// </summary>
- /// <param name="pi"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|