using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace POSV { #region 基类 /// /// 基础返回结果(微信支付返回结果基类) /// public class WxPayV3Result { public string return_code { get; set; } public string return_msg { get; set; } protected XDocument _resultXml; public WxPayV3Result(string resultXml) { _resultXml = XDocument.Parse(resultXml); return_code = GetXmlValue("return_code"); // res.Element("xml").Element if (!IsReturnCodeSuccess()) { return_msg = GetXmlValue("return_msg"); // res.Element("xml").Element } } /// /// 获取Xml结果中对应节点的值 /// /// /// public string GetXmlValue(string nodeName) { if (_resultXml == null || _resultXml.Element("xml") == null || _resultXml.Element("xml").Element(nodeName) == null) { return null; } return _resultXml.Element("xml").Element(nodeName).Value; } /// /// 获取Xml结果中对应节点的集合值 /// /// /// public IList GetXmlValues(string nodeName) { var result = new List(); try { if (_resultXml != null) { var xElement = _resultXml.Element("xml"); if (xElement != null) { var nodeList = xElement.Elements().Where(z => z.Name.LocalName.ToString().StartsWith(nodeName)).Select(x => x.Value); result = nodeList.Cast().ToList(); } } } catch (System.Exception) { result = null; } return result; } public bool IsReturnCodeSuccess() { return return_code == "SUCCESS"; } } /// /// 统一支付接口在 return_code为 SUCCESS的时候有返回 /// public class Result : WxPayV3Result { /// /// 微信分配的公众账号ID /// public string appid { get; set; } /// /// 微信支付分配的商户号 /// public string mch_id { get; set; } #region 服务商 /// /// 子商户公众账号ID /// public string sub_appid { get; set; } /// /// 子商户号 /// public string sub_mch_id { get; set; } #endregion /// /// 随机字符串,不长于32 位 /// public string nonce_str { get; set; } /// /// 签名 /// public string sign { get; set; } /// /// SUCCESS/FAIL /// public string result_code { get; set; } public string err_code { get; set; } public string err_code_des { get; set; } public Result(string resultXml) : base(resultXml) { result_code = GetXmlValue("result_code"); // res.Element("xml").Element if (base.IsReturnCodeSuccess()) { appid = GetXmlValue("appid") ?? ""; mch_id = GetXmlValue("mch_id") ?? ""; #region 服务商 sub_appid = GetXmlValue("sub_appid") ?? ""; sub_mch_id = GetXmlValue("sub_mch_id") ?? ""; #endregion nonce_str = GetXmlValue("nonce_str") ?? ""; sign = GetXmlValue("sign") ?? ""; err_code = GetXmlValue("err_code") ?? ""; err_code_des = GetXmlValue("err_code_des") ?? ""; } } /// /// result_code == "SUCCESS" /// /// public bool IsResultCodeSuccess() { return result_code == "SUCCESS"; } } #endregion /// /// 统一支付接口在return_code 和result_code 都为SUCCESS 的时候有返回详细信息 /// public class UnifiedorderResult : Result { /// /// 微信支付分配的终端设备号 /// public string device_info { get; set; } /// /// 交易类型:JSAPI、NATIVE、APP /// public string trade_type { get; set; } /// /// 微信生成的预支付ID,用于后续接口调用中使用 /// public string prepay_id { get; set; } /// /// trade_type为NATIVE时有返回,此参数可直接生成二维码展示出来进行扫码支付 /// public string code_url { get; set; } ///// ///// 子商户公众账号ID ///// //public string sub_appid { get; set; } ///// ///// 子商户号 ///// //public string sub_mch_id { get; set; } public UnifiedorderResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { device_info = GetXmlValue("device_info") ?? ""; //sub_appid = GetXmlValue("sub_appid") ?? ""; //sub_mch_id = GetXmlValue("sub_mch_id") ?? ""; if (base.IsResultCodeSuccess()) { trade_type = GetXmlValue("trade_type") ?? ""; prepay_id = GetXmlValue("prepay_id") ?? ""; code_url = GetXmlValue("code_url") ?? ""; } } } } /// /// 刷卡支付 /// 提交被扫支付 /// public class MicropayResult : Result { /// /// 调用接口提交的终端设备号 /// public string device_info { get; set; } /// /// 用户在商户appid 下的唯一标识 /// public string openid { get; set; } /// /// 用户是否关注公众账号,仅在公众账号类型支付有效,取值范围:Y或N;Y-关注;N-未关注 /// public string is_subscribe { get; set; } /// /// 支付类型为MICROPAY(即扫码支付) /// public string trade_type { get; set; } /// /// 银行类型,采用字符串类型的银行标识 /// public string bank_type { get; set; } /// /// 符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string fee_type { get; set; } /// /// 订单总金额,单位为分,只能为整数 /// public string total_fee { get; set; } /// /// 应结订单金额=订单金额-非充值代金券金额,应结订单金额<=订单金额 /// public string settlement_total_fee { get; set; } /// /// “代金券”金额<=订单金额,订单金额-“代金券”金额=现金支付金额 /// public string coupon_fee { get; set; } /// /// 符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string cash_fee_type { get; set; } /// /// 订单现金支付金额 /// public string cash_fee { get; set; } /// /// 微信支付订单号 /// public string transaction_id { get; set; } /// /// 商户系统的订单号,与请求一致 /// public string out_trade_no { get; set; } /// /// 商家数据包,原样返回 /// public string attach { get; set; } /// /// 订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010 /// public string time_end { get; set; } public MicropayResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { device_info = GetXmlValue("device_info") ?? ""; if (base.IsResultCodeSuccess()) { openid = GetXmlValue("openid") ?? ""; is_subscribe = GetXmlValue("is_subscribe") ?? ""; trade_type = GetXmlValue("trade_type") ?? ""; bank_type = GetXmlValue("bank_type") ?? ""; fee_type = GetXmlValue("fee_type") ?? ""; total_fee = GetXmlValue("total_fee") ?? ""; settlement_total_fee = GetXmlValue("settlement_total_fee") ?? ""; coupon_fee = GetXmlValue("coupon_fee") ?? ""; cash_fee_type = GetXmlValue("cash_fee_type") ?? ""; cash_fee = GetXmlValue("cash_fee") ?? ""; transaction_id = GetXmlValue("transaction_id") ?? ""; out_trade_no = GetXmlValue("out_trade_no") ?? ""; attach = GetXmlValue("attach") ?? ""; time_end = GetXmlValue("time_end") ?? ""; } } } } /// /// 短链接转换接口 /// public class ShortUrlResult : Result { /// /// 转换后的URL /// public string short_url { get; set; } public ShortUrlResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { short_url = GetXmlValue("short_url") ?? ""; } } } /// /// 撤销订单接口 /// public class ReverseResult : Result { /// /// 是否需要继续调用撤销,Y-需要,N-不需要 /// public string recall { get; set; } public ReverseResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { recall = GetXmlValue("recall") ?? ""; } } } /// /// 查询订单接口返回结果 /// public class OrderQueryResult : Result { /// /// 微信支付分配的终端设备号 /// public string device_info { get; set; } /// /// 用户在商户appid下的唯一标识 /// public string openid { get; set; } /// /// 用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效 /// public string is_subscribe { get; set; } /// /// 用户子标识[服务商] /// public string sub_openid { get; set; } /// /// 是否关注子公众账号[服务商] /// public string sub_is_subscribe { get; set; } /// /// 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP,MICROPAY /// public string trade_type { get; set; } /// ///SUCCESS—支付成功 ///REFUND—转入退款 ///NOTPAY—未支付 ///CLOSED—已关闭 ///REVOKED—已撤销(刷卡支付) ///USERPAYING--用户支付中 ///PAYERROR--支付失败(其他原因,如银行返回失败) /// public string trade_state { get; set; } /// /// 银行类型,采用字符串类型的银行标识 /// public string bank_type { get; set; } /// /// 商品详情[服务商] /// public string detail { get; set; } /// /// 订单总金额,单位为分 /// public string total_fee { get; set; } /// /// 应结订单金额=订单金额-非充值代金券金额,应结订单金额<=订单金额 /// public string settlement_total_fee { get; set; } /// /// 货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string fee_type { get; set; } /// /// 现金支付金额订单现金支付金额 /// public string cash_fee { get; set; } /// /// 货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string cash_fee_type { get; set; } /// /// “代金券”金额<=订单金额,订单金额-“代金券”金额=现金支付金额 /// public string coupon_fee { get; set; } /// /// 代金券使用数量 /// public string coupon_count { get; set; } /// /// CASH--充值代金券 ///NO_CASH---非充值代金券 ///订单使用代金券时有返回(取值:CASH、NO_CASH)。$n为下标,从0开始编号,举例:coupon_type_$0 ///coupon_type_$n /// public IList coupon_type_values { get; set; } /// /// 代金券ID, $n为下标,从0开始编号 /// coupon_id_$n /// public IList coupon_id_values { get; set; } /// /// 单个代金券支付金额, $n为下标,从0开始编号 /// coupon_fee_$n /// public IList coupon_fee_values { get; set; } /// /// 微信支付订单号 /// public string transaction_id { get; set; } /// /// 商户系统的订单号,与请求一致。 /// public string out_trade_no { get; set; } /// /// 附加数据,原样返回 /// public string attach { get; set; } /// /// 订单支付时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010 /// public string time_end { get; set; } /// /// 对当前查询订单状态的描述和下一步操作的指引 /// public string trade_state_desc { get; set; } public OrderQueryResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { if (base.IsResultCodeSuccess()) { device_info = GetXmlValue("device_info") ?? ""; openid = GetXmlValue("openid") ?? ""; is_subscribe = GetXmlValue("is_subscribe") ?? ""; sub_openid = GetXmlValue("sub_openid") ?? ""; //用户子标识[服务商] sub_is_subscribe = GetXmlValue("sub_is_subscribe") ?? ""; //是否关注子公众账号[服务商] trade_type = GetXmlValue("trade_type") ?? ""; trade_state = GetXmlValue("trade_state") ?? ""; bank_type = GetXmlValue("bank_type") ?? ""; detail = GetXmlValue("detail") ?? ""; //商品详情[服务商] total_fee = GetXmlValue("total_fee") ?? ""; settlement_total_fee = GetXmlValue("settlement_total_fee") ?? ""; fee_type = GetXmlValue("fee_type") ?? ""; cash_fee = GetXmlValue("cash_fee") ?? ""; cash_fee_type = GetXmlValue("cash_fee_type") ?? ""; coupon_fee = GetXmlValue("coupon_fee") ?? ""; coupon_count = GetXmlValue("coupon_count") ?? ""; #region 特殊"$n" coupon_type_values = GetXmlValues("coupon_type_") ?? new List(); coupon_id_values = GetXmlValues("coupon_id_") ?? new List(); coupon_fee_values = GetXmlValues("coupon_fee_") ?? new List(); #endregion transaction_id = GetXmlValue("transaction_id") ?? ""; out_trade_no = GetXmlValue("out_trade_no") ?? ""; attach = GetXmlValue("attach") ?? ""; time_end = GetXmlValue("time_end") ?? ""; trade_state_desc = GetXmlValue("trade_state_desc") ?? ""; } } } } /// /// 关闭订单接口 /// public class CloseOrderResult : Result { /// /// 对于业务执行的详细描述 /// public string result_msg { get; set; } public CloseOrderResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { result_msg = GetXmlValue("result_msg") ?? ""; } } } /// /// 退款查询接口 /// public class RefundQueryResult : Result { /// /// 退款记录数 /// public string total_refund_count { get; set; } /// /// 微信订单号 /// public string transaction_id { get; set; } /// /// 商户系统内部的订单号 /// public string out_trade_no { get; set; } /// /// 订单总金额,单位为分,只能为整数 /// public string total_fee { get; set; } /// /// 应结订单金额=订单金额-非充值代金券金额,应结订单金额<=订单金额 /// public string settlement_total_fee { get; set; } /// /// 订单金额货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string fee_type { get; set; } /// /// 现金支付金额,单位为分,只能为整数 /// public string cash_fee { get; set; } /// /// 当前返回退款笔数 /// public string refund_count { get; set; } /// /// 商户退款单号 /// public IList out_refund_no_list { get; set; } /// /// 微信退款单号 /// public IList refund_id_list { get; set; } /// /// ORIGINAL—原路退款 //BALANCE—退回到余额 /// public IList refund_channel_list { get; set; } /// /// 退款总金额,单位为分,可以做部分退款 /// public IList refund_fee_list { get; set; } /// /// 退款金额=申请退款金额-非充值代金券退款金额,退款金额<=申请退款金额 /// public IList settlement_refund_fee_list { get; set; } /// /// REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款/基本账户 //REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款 /// public string refund_account { get; set; } /// /// CASH--充值代金券 //NO_CASH---非充值代金券 //订单使用代金券时有返回(取值:CASH、NO_CASH)。$n为下标,从0开始编号,举例:coupon_type_$0 /// public IList coupon_type_list { get; set; } /// /// 代金券退款金额<=退款金额,退款金额-代金券或立减优惠退款金额为现金 /// public IList coupon_refund_fee_list { get; set; } /// /// 退款代金券使用数量 ,$n为下标,从0开始编号 /// public IList coupon_refund_count_list { get; set; } /// /// 退款代金券ID, $n为下标,$m为下标,从0开始编号 /// public IList coupon_refund_id_list { get; set; } /// /// 单个退款代金券支付金额, $n为下标,$m为下标,从0开始编号 /// public IList coupon_refund_fee_item_list { get; set; } /// /// 退款状态: ///SUCCESS—退款成功 ///FAIL—退款失败 ///PROCESSING—退款处理中 ///CHANGE—转入代发,退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,资金回流到商户的现金帐号,需要商户人工干预,通过线下或者财付通转账的方式进行退款。 /// public IList refund_status_list { get; set; } /// /// 取当前退款单的退款入账方 ///1)退回银行卡: ///{银行名称 /// }{卡类型 ///}{卡尾号} ///2)退回支付用户零钱: ///支付用户零钱 /// public IList refund_recv_accout_list { get; set; } public RefundQueryResult(string resultXml) : base(resultXml) { if (base.IsResultCodeSuccess()) { transaction_id = GetXmlValue("transaction_id") ?? ""; out_trade_no = GetXmlValue("out_trade_no") ?? ""; total_fee = GetXmlValue("total_fee") ?? ""; settlement_total_fee = GetXmlValue("settlement_total_fee") ?? ""; fee_type = GetXmlValue("fee_type") ?? ""; cash_fee = GetXmlValue("cash_fee") ?? ""; refund_count = GetXmlValue("refund_count") ?? ""; out_refund_no_list = GetXmlValues("out_refund_no_") ?? new List(); refund_id_list = GetXmlValues("refund_id_") ?? new List(); refund_channel_list = GetXmlValues("refund_channel_") ?? new List(); refund_fee_list = GetXmlValues("refund_fee_") ?? new List(); settlement_refund_fee_list = GetXmlValues("settlement_refund_fee_") ?? new List(); refund_account = GetXmlValue("refund_account") ?? ""; coupon_type_list = GetXmlValues("coupon_type_") ?? new List(); coupon_refund_fee_list = GetXmlValues("coupon_refund_fee_") ?? new List(); coupon_refund_count_list = GetXmlValues("coupon_refund_count_") ?? new List(); coupon_refund_id_list = GetXmlValues("coupon_refund_id_") ?? new List(); refund_status_list = GetXmlValues("refund_status_") ?? new List(); refund_recv_accout_list = GetXmlValues("refund_recv_accout_") ?? new List(); } } } /// /// 订单退款 /// public class OrderRefundResult : Result { /// /// 微信订单号 /// public string transaction_id { get; set; } /// /// 商户订单号 /// public string out_trade_no { get; set; } /// /// 商户退款单号 /// public string out_refund_no { get; set; } /// /// 微信退款单号 /// public string refund_id { get; set; } /// /// 退款金额 /// public string refund_fee { get; set; } /// /// 应结退款金额 /// public string settlement_refund_fee { get; set; } /// /// 订单总金额,单位为分,只能为整数 /// public string total_fee { get; set; } /// /// 应结订单金额=订单金额-非充值代金券金额,应结订单金额<=订单金额 /// public string settlement_total_fee { get; set; } /// /// 订单金额货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string fee_type { get; set; } /// /// 符合ISO 4217标准的三位字母代码,默认人民币:CNY /// public string cash_fee_type { get; set; } /// /// 订单现金支付金额 /// public string cash_fee { get; set; } /// /// 现金退款金额 /// public string cash_refund_fee { get; set; } /// /// 代金券类型 /// public IList coupon_type_list { get; set; } /// /// 代金券退款总金额 /// public string coupon_refund_fee { get; set; } /// /// 单个代金券退款金额 /// public IList coupon_refund_fee_list { get; set; } /// /// 退款代金券使用数量 /// public string coupon_refund_count { get; set; } /// /// 退款代金券ID /// public IList coupon_refund_id_list { get; set; } public OrderRefundResult(string resultXml) : base(resultXml) { if (base.IsReturnCodeSuccess()) { if (base.IsResultCodeSuccess()) { out_refund_no = GetXmlValue("out_refund_no") ?? ""; refund_id = GetXmlValue("refund_id") ?? ""; refund_fee = GetXmlValue("refund_fee") ?? ""; settlement_refund_fee = GetXmlValue("settlement_refund_fee") ?? ""; fee_type = GetXmlValue("fee_type") ?? ""; total_fee = GetXmlValue("total_fee") ?? ""; settlement_total_fee = GetXmlValue("settlement_total_fee") ?? ""; cash_refund_fee = GetXmlValue("cash_refund_fee") ?? ""; cash_fee_type = GetXmlValue("cash_fee_type") ?? ""; cash_fee = GetXmlValue("cash_fee") ?? ""; transaction_id = GetXmlValue("transaction_id") ?? ""; out_trade_no = GetXmlValue("out_trade_no") ?? ""; coupon_type_list = GetXmlValues("coupon_type_") ?? new List(); coupon_refund_fee = GetXmlValue("coupon_refund_fee") ?? ""; coupon_refund_fee_list = GetXmlValues("coupon_refund_fee_") ?? new List(); coupon_refund_id_list = GetXmlValues("coupon_refund_id_list") ?? new List(); coupon_refund_count = GetXmlValue("coupon_refund_count"); } } } } }