Exemple #1
0
        public static void Main(string[] args)
        {
            try
            {
                LogHelp.WriteLog("begin::团购定时任务");
                //判断已经结束的团购
                OrderBC bc      = new OrderBC();
                var     entitys = bc.GetGroupBuyConfig();
                foreach (var item in entitys)
                {
                    var needCount = bc.GetGroupBuyConfigByClassId(item.ClassId).NeedCount;
                    LogHelp.WriteLog("needCount:::" + needCount.ToString());
                    var group = bc.GetGroupBuyByClassId(item.ClassId);
                    if (group != null)
                    {
                        if (needCount != group.NowCount)//人数不满足,退款
                        {
                            LogHelp.WriteLog("人数不满足,退款");
                            //找到付款订单
                            var members = bc.GetGroupBuyMember(group.GroupBuyId);
                            if (members != null && members.Count > 0)
                            {
                                foreach (var i in members)
                                {
                                    var order = bc.GetOrderByOpenIdandClassId(i.openId, group.ClassId);
                                    //根据订单退款
                                    AppSetting  setting = new AppSetting();
                                    WxPayClient client  = new WxPayClient();
                                    WxPayData   data    = new WxPayData();


                                    string RefundNumber = string.Format("{0}{1}", order.OrderNo.ToString(), DateTime.Now.ToString("fff"));
                                    LogHelp.WriteLog("RefundNumber:::" + RefundNumber);


                                    RefundOrderRequest req = new RefundOrderRequest();
                                    data.SetValue("out_trade_no", order.WXPayOutTradeNumber);
                                    data.SetValue("total_fee", 1);                                 //订单总金额
                                    data.SetValue("refund_fee", 1);                                //退款金额

                                    data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo()); //随机生成商户退款单号
                                    //data.SetValue("total_fee", Convert.ToInt32(order.PayPrice * 100));//订单总金额
                                    //data.SetValue("refund_fee",  Convert.ToInt32(order.PayPrice * 100));//退款金额

                                    var resp = client.Refund(data);

                                    //WxPayData jsApiParam = new WxPayData();
                                    //jsApiParam.SetValue("appId", resp.AppId);
                                    //jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
                                    //jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
                                    //jsApiParam.SetValue("package", "prepay_id=" + resp.PrepayId);
                                    //jsApiParam.SetValue("signType", "MD5");
                                    //jsApiParam.SetValue("paySign", jsApiParam.MakeSign());
                                }
                            }
                        }
                        bc.UpdateGroupBuyStatus(group.GroupBuyId, 2);
                    }
                }
                LogHelp.WriteLog("end::团购定时任务");
            }
            catch (Exception ex)
            {
                LogHelp.WriteLog("end::团购定时任务出错" + ex.Message);
            }

            //
        }
Exemple #2
0
        //private ILogger _logger;
        //public UnifiedOrderResponse UnifiedOrder(UnifiedOrderRequest request)
        //{

        //    //_logger = new LoggerFactory().SetCurrent(new Log4NetLoggerFactory("Wechat_Verify")).CreateLogger();
        //    UnifiedOrderResponse resp = new UnifiedOrderResponse();
        //    //统一下单
        //    try
        //    {
        //        //_logger.Debug("exPay:in");
        //        WxPayData data = new WxPayData();
        //        data.SetValue("body", request.Body);
        //        data.SetValue("attach", request.Attach);
        //        data.SetValue("out_trade_no", request.OutTradeNo);
        //        data.SetValue("total_fee", request.TotalFee);
        //        data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
        //        data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
        //        data.SetValue("goods_tag", request.GoodTag);
        //        data.SetValue("trade_type", request.TradeType);
        //        data.SetValue("openid", request.OpenId);


        //        data.SetValue("notify_url", request.NotifyUrl);//异步通知url
        //        data.SetValue("appid", WxPayConfig.APPID);//公众账号ID
        //        data.SetValue("mch_id", WxPayConfig.MCHID);//商户号
        //        data.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip
        //        data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串

        //        //签名
        //        data.SetValue("sign", data.MakeSign());

        //        string xml = data.ToXml();
        //        LogHelp.WriteLog(DateTime.Now + "payInxml:" + xml);
        //        RestClient client = new RestClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
        //        RestRequest req = new RestRequest(Method.POST);
        //        req.RequestFormat = DataFormat.Xml;
        //        req.AddParameter("text/xml", xml, ParameterType.RequestBody);

        //        var content = client.Execute(req).Content;
        //        LogHelp.WriteLog(DateTime.Now + "payIn:" + content);
        //        WxPayData result = new WxPayData();
        //        result.FromXml(content);


        //        if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
        //        {
        //            resp.Success = false;

        //            if (result.IsSet("return_msg"))
        //            {
        //                resp.ErrMsg = (string)result.GetValue("return_msg");
        //            }

        //            return resp;
        //        }

        //        resp.Success = (string)result.GetValue("return_code") == "SUCCESS";
        //        if (!resp.Success)
        //        {
        //            resp.ErrMsg = (string)result.GetValue("return_msg");
        //            return resp;
        //        }
        //        else
        //            resp.ErrMsg = string.Empty;

        //        resp.AppId = (string)result.GetValue("appid");
        //        resp.MchId = (string)result.GetValue("mch_id");
        //        resp.NonceStr = (string)result.GetValue("nonce_str");
        //        resp.Sign = (string)result.GetValue("sign");
        //        resp.PrepayId = (string)result.GetValue("prepay_id");
        //        resp.TradeType = (string)result.GetValue("trade_type");
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelp.WriteLog(DateTime.Now + "UnifiedOrderError:" + ex.Message);
        //    }

        //    return resp;
        //}

        //public RefundOrderResponse RefundOrder(RefundOrderRequest request)
        //{
        //    RefundOrderResponse resp = new RefundOrderResponse();

        //    RestClient client = new RestClient("https://api.mch.weixin.qq.com/secapi/pay/refund");

        //    X509Certificate2[] certs = new X509Certificate2[] {
        //        new X509Certificate2(request.SSLCertRootPath + WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD)
        //        };

        //    client.ClientCertificates = new X509CertificateCollection(certs);

        //    WxPayData data = new WxPayData();
        //    data.SetValue("transaction_id", request.TransactionId);
        //    data.SetValue("out_refund_no", request.OutRefundNo);
        //    data.SetValue("total_fee", request.TotalFee);
        //    data.SetValue("refund_fee", request.RefundFee);
        //    data.SetValue("op_user_id", WxPayConfig.MCHID);

        //    data.SetValue("appid", WxPayConfig.APPID);
        //    data.SetValue("mch_id", WxPayConfig.MCHID);
        //    data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
        //    data.SetValue("sign", data.MakeSign());

        //    string xml = data.ToXml();

        //    RestRequest req = new RestRequest(Method.POST);
        //    req.RequestFormat = DataFormat.Xml;
        //    req.AddParameter("text/xml", xml, ParameterType.RequestBody);

        //    var content = client.Execute(req).Content;

        //    WxPayData result = new WxPayData();
        //    result.FromXml(content);

        //    resp.Success = (string)result.GetValue("return_code") == "SUCCESS";
        //    if (!resp.Success)
        //    {
        //        resp.ErrCode = (string)result.GetValue("err_code");
        //        resp.ErrMsg = (string)result.GetValue("err_code_des");
        //        return resp;
        //    }
        //    else
        //    {
        //        resp.ErrCode = string.Empty;
        //        resp.ErrMsg = string.Empty;
        //    }


        //    resp.AppId = (string)result.GetValue("appid");
        //    resp.MchId = (string)result.GetValue("mch_id");

        //    resp.TransactionId = (string)result.GetValue("transaction_id");
        //    resp.OutTradeNo = (string)result.GetValue("out_trade_no");
        //    resp.OutRefundNo = (string)result.GetValue("out_refund_no");
        //    resp.RefundId = (string)result.GetValue("refund_id");
        //    resp.RefundChannel = (string)result.GetValue("refund_channel");
        //    resp.RefundFee = int.Parse((string)result.GetValue("refund_fee"));
        //    resp.TotalFee = int.Parse((string)result.GetValue("total_fee"));

        //    return resp;
        //}


        public WxPayData Refund(WxPayData inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
            {
                throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
            }
            else if (!inputObj.IsSet("out_refund_no"))
            {
                throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
            }
            else if (!inputObj.IsSet("refund_fee"))
            {
                throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
            }

            inputObj.SetValue("op_user_id", WxPayConfig.MCHID);
            inputObj.SetValue("appid", WxPayConfig.APPID);                              //公众账号ID
            inputObj.SetValue("mch_id", WxPayConfig.MCHID);                             //商户号
            inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串

            inputObj.SetValue("sign", inputObj.MakeSign());                             //签名

            string xml   = inputObj.ToXml();
            var    start = DateTime.Now;

            LogHelp.WriteLog(DateTime.Now + "Refund request:" + xml);

            byte[]         data       = System.Text.Encoding.UTF8.GetBytes(xml);
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

            webrequest.Method        = "POST";
            webrequest.ContentType   = "application/x-www-form-urlencoded";
            webrequest.Timeout       = 6 * 1000;;
            webrequest.ContentLength = data.Length;

            //string path = "/cert/apiclient_cert.p12";
            X509Certificate2 cert = new X509Certificate2(WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD);

            webrequest.ClientCertificates.Add(cert);

            webrequest.KeepAlive       = false;
            webrequest.ProtocolVersion = HttpVersion.Version10;
            Stream stream = webrequest.GetRequestStream();

            stream.Write(data, 0, data.Length);
            stream.Close();
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
            StreamReader    sr          = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8);
            string          response    = sr.ReadToEnd();

            //string response = HttpService.Post(xml, url, true, timeOut);//调用HTTP通信接口提交数据到API
            LogHelp.WriteLog(DateTime.Now + "Refund response:" + response);

            var end      = DateTime.Now;
            int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时

            //将xml格式的结果转换为对象以返回
            WxPayData result = new WxPayData();

            result.FromXml(response);



            return(result);
        }