隐藏

C#微信开发之旅(十三):V2订单查询&退款(完结)

发布:2020/4/26 17:38:28作者:管理员 来源:本站 浏览次数:1258

订单查询

用处同V3订单查询,直接上代码:


/// <summary>
        /// V2订单查询
        /// </summary>
        public void QueryOrder()
        {
            string orderNo = string.Empty;
 
            WxPayModel model = WxPayModel.Create(orderNo);
            OrderQueryMessage message = WeiXinHelper.OrderQuery(model.CreateOrderQueryPackage());
        }

生成订单查询json方法:

/// <summary>
        /// 生成订单查询Json  (已测试通过)
        /// </summary>
        /// <returns></returns>
        public string CreateOrderQueryPackage()
        {
            Dictionary<string, string> nativeObj = new Dictionary<string, string>();
            if (CheckOrderQueryParameters() == false)
            {
                throw new SDKRuntimeException("生成package参数缺失!");
            }
            nativeObj.Add("appid", AppId);
            nativeObj.Add("package", GetCftPackage());
            nativeObj.Add("timestamp", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
            nativeObj.Add("app_signature", GetBizSign(nativeObj));
            nativeObj.Add("sign_method", SignType);

            var entries = nativeObj.Select(d => string.Format("\"{0}\": \"{1}\"", d.Key, d.Value));

            return "{" + string.Join(",", entries.ToArray()) + "}";
        }


退款

退款操作只是向财付通发出退款通知,成功与否需要查询:

/// <summary>
        /// V2退款
        /// </summary>
        /// <returns>提交退款结果</returns>
        public bool OrderRefund()
        {
            string orderNo = string.Empty;
            string transactionId = string.Empty;
            string totalFee = string.Empty;
            string refundNo = string.Empty;
            string refundFee = string.Empty;

            /*
             TenpayHelper.Refund()方法中  这两段代码是在  管理员账号的密码默认为111111时使用 ,请根据情况修改
            reqHandler.setParameter("op_user_id", partnerId); //todo:配置(op_user_id,op_user_passwd)
            reqHandler.setParameter("op_user_passwd", MD5Util.GetMD5("111111", "GBK"));
             */

            return TenpayHelper.Refund(WeiXinConst.PartnerId, WeiXinConst.PartnerKey,
               orderNo, transactionId, totalFee, refundNo, refundFee,
               WeiXinConst.CertPath, WeiXinConst.CertPwd);
        }


TenpayHelper.Refund()方法是对V2官方demo再次封装的,相关方法做了一点修改:

/// <summary>
    /// V2 接口 退款帮助类
    /// </summary>
    public class TenpayHelper
    {
        /// <summary>
        /// 申请退款
        /// </summary>
        /// <param name="partnerId">商户号</param>
        /// <param name="partnerKey">财付通密钥</param>
        /// <param name="orderNo">商户订单号</param>
        /// <param name="transanctionId">微信订单号</param>
        /// <param name="totalFee">总金额(单位:分)</param>
        /// <param name="refundNo">退款单号</param>
        /// <param name="refundFee">退款金额(单位:分)</param>
        /// <param name="Context">MVC页面请求上下文对象</param>
        /// <returns></returns>
        public static bool Refund(string partnerId, string partnerKey, string orderNo,
            string transanctionId, string totalFee, string refundNo, string refundFee,
            string certPath, string certPwd)
        {
            //商户号
            string partner = partnerId;
            //创建请求对象
            RequestHandler reqHandler = new RequestHandler();
            //通信对象
            TenpayHttpClient httpClient = new TenpayHttpClient();
            //应答对象
            ClientResponseHandler resHandler = new ClientResponseHandler();

            //-----------------------------
            //设置请求参数
            //-----------------------------
            reqHandler.init();
            reqHandler.setKey(partnerKey); //财付通密钥

            reqHandler.setGateUrl("https://mch.tenpay.com/refundapi/gateway/refund.xml");

            reqHandler.setParameter("partner", partner);
            //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先
            if (string.IsNullOrEmpty(transanctionId))
            {
                if (string.IsNullOrEmpty(orderNo))
                    throw new Exception("订单号不能为空!");

                reqHandler.setParameter("out_trade_no", orderNo);
            }
            else
            {
                reqHandler.setParameter("transaction_id", transanctionId);
            }

            reqHandler.setParameter("out_refund_no", refundNo); //退款单号
            reqHandler.setParameter("total_fee", totalFee); //总金额
            reqHandler.setParameter("refund_fee", refundFee); //退款金额
            reqHandler.setParameter("op_user_id", partnerId); //todo:配置(op_user_id,op_user_passwd)
            reqHandler.setParameter("op_user_passwd", MD5Util.GetMD5("111111", "GBK"));
            reqHandler.setParameter("service_version", "1.1");

            string requestUrl = reqHandler.getRequestURL();
            httpClient.setCertInfo(certPath, certPwd);
            //设置请求内容
            httpClient.setReqContent(requestUrl);
            //设置超时
            httpClient.setTimeOut(10);

            string rescontent = "";
            //后台调用
            if (httpClient.call())
            {
                //获取结果
                rescontent = httpClient.getResContent();

                resHandler.setKey(partnerKey);
                //设置结果参数
                resHandler.setContent(rescontent);
                //判断签名及结果
                if (resHandler.isTenpaySign() && resHandler.getParameter("retcode") == "0")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }


结束语:

C#微信开发之旅是我在博客园首篇随笔系列,写的虎头蛇尾,就先结束了。。。。

以后会继续坚持写博客,分享工作中总结的微小经验用以记录,也希望能帮到大家!