订单查询
用处同V3订单查询,直接上代码:
1 /// <summary> 2 /// V2订单查询 3 /// </summary> 4 public void QueryOrder() 5 { 6 string orderNo = string.Empty; 7 8 WxPayModel model = WxPayModel.Create(orderNo); 9 OrderQueryMessage message = WeiXinHelper.OrderQuery(model.CreateOrderQueryPackage()); 10 }
生成订单查询json方法:
1 /// <summary> 2 /// 生成订单查询Json (已测试通过) 3 /// </summary> 4 /// <returns></returns> 5 public string CreateOrderQueryPackage() 6 { 7 Dictionary<string, string> nativeObj = new Dictionary<string, string>(); 8 if (CheckOrderQueryParameters() == false) 9 { 10 throw new SDKRuntimeException("生成package参数缺失!"); 11 } 12 nativeObj.Add("appid", AppId); 13 nativeObj.Add("package", GetCftPackage()); 14 nativeObj.Add("timestamp", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString()); 15 nativeObj.Add("app_signature", GetBizSign(nativeObj)); 16 nativeObj.Add("sign_method", SignType); 17 18 var entries = nativeObj.Select(d => string.Format(""{0}": "{1}"", d.Key, d.Value)); 19 20 return "{" + string.Join(",", entries.ToArray()) + "}"; 21 }
退款
退款操作只是向财付通发出退款通知,成功与否需要查询:
1 /// <summary> 2 /// V2退款 3 /// </summary> 4 /// <returns>提交退款结果</returns> 5 public bool OrderRefund() 6 { 7 string orderNo = string.Empty; 8 string transactionId = string.Empty; 9 string totalFee = string.Empty; 10 string refundNo = string.Empty; 11 string refundFee = string.Empty; 12 13 /* 14 TenpayHelper.Refund()方法中 这两段代码是在 管理员账号的密码默认为111111时使用 ,请根据情况修改 15 reqHandler.setParameter("op_user_id", partnerId); //todo:配置(op_user_id,op_user_passwd) 16 reqHandler.setParameter("op_user_passwd", MD5Util.GetMD5("111111", "GBK")); 17 */ 18 19 return TenpayHelper.Refund(WeiXinConst.PartnerId, WeiXinConst.PartnerKey, 20 orderNo, transactionId, totalFee, refundNo, refundFee, 21 WeiXinConst.CertPath, WeiXinConst.CertPwd); 22 }
TenpayHelper.Refund()方法是对V2官方demo再次封装的,相关方法做了一点修改:
1 /// <summary> 2 /// V2 接口 退款帮助类 3 /// </summary> 4 public class TenpayHelper 5 { 6 /// <summary> 7 /// 申请退款 8 /// </summary> 9 /// <param name="partnerId">商户号</param> 10 /// <param name="partnerKey">财付通密钥</param> 11 /// <param name="orderNo">商户订单号</param> 12 /// <param name="transanctionId">微信订单号</param> 13 /// <param name="totalFee">总金额(单位:分)</param> 14 /// <param name="refundNo">退款单号</param> 15 /// <param name="refundFee">退款金额(单位:分)</param> 16 /// <param name="Context">MVC页面请求上下文对象</param> 17 /// <returns></returns> 18 public static bool Refund(string partnerId, string partnerKey, string orderNo, 19 string transanctionId, string totalFee, string refundNo, string refundFee, 20 string certPath, string certPwd) 21 { 22 //商户号 23 string partner = partnerId; 24 //创建请求对象 25 RequestHandler reqHandler = new RequestHandler(); 26 //通信对象 27 TenpayHttpClient httpClient = new TenpayHttpClient(); 28 //应答对象 29 ClientResponseHandler resHandler = new ClientResponseHandler(); 30 31 //----------------------------- 32 //设置请求参数 33 //----------------------------- 34 reqHandler.init(); 35 reqHandler.setKey(partnerKey); //财付通密钥 36 37 reqHandler.setGateUrl("https://mch.tenpay.com/refundapi/gateway/refund.xml"); 38 39 reqHandler.setParameter("partner", partner); 40 //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先 41 if (string.IsNullOrEmpty(transanctionId)) 42 { 43 if (string.IsNullOrEmpty(orderNo)) 44 throw new Exception("订单号不能为空!"); 45 46 reqHandler.setParameter("out_trade_no", orderNo); 47 } 48 else 49 { 50 reqHandler.setParameter("transaction_id", transanctionId); 51 } 52 53 reqHandler.setParameter("out_refund_no", refundNo); //退款单号 54 reqHandler.setParameter("total_fee", totalFee); //总金额 55 reqHandler.setParameter("refund_fee", refundFee); //退款金额 56 reqHandler.setParameter("op_user_id", partnerId); //todo:配置(op_user_id,op_user_passwd) 57 reqHandler.setParameter("op_user_passwd", MD5Util.GetMD5("111111", "GBK")); 58 reqHandler.setParameter("service_version", "1.1"); 59 60 string requestUrl = reqHandler.getRequestURL(); 61 httpClient.setCertInfo(certPath, certPwd); 62 //设置请求内容 63 httpClient.setReqContent(requestUrl); 64 //设置超时 65 httpClient.setTimeOut(10); 66 67 string rescontent = ""; 68 //后台调用 69 if (httpClient.call()) 70 { 71 //获取结果 72 rescontent = httpClient.getResContent(); 73 74 resHandler.setKey(partnerKey); 75 //设置结果参数 76 resHandler.setContent(rescontent); 77 //判断签名及结果 78 if (resHandler.isTenpaySign() && resHandler.getParameter("retcode") == "0") 79 { 80 return true; 81 } 82 else 83 { 84 return false; 85 } 86 } 87 else 88 { 89 return false; 90 } 91 } 92 }
结束语:
C#微信开发之旅是我在博客园首篇随笔系列,写的虎头蛇尾,就先结束了。。。。
以后会继续坚持写博客,分享工作中总结的微小经验用以记录,也希望能帮到大家!