Esempio n. 1
0
        //post数据
        public static WxPayDataTool GetResult(string url, WxPayDataTool inputobj, bool isUseCert = true)
        {
            string xml = inputobj.ToXml();
            //LogDB.DebugTest("发送的数据:" + xml);
            string response = UtilTool.Post(url, xml, isUseCert);
            //LogDB.DebugTest("接收的结果:"+response);
            WxPayDataTool result = new WxPayDataTool();

            result.FromXml(response);
            return(result);
        }
Esempio n. 2
0
 //检测返回结果
 public static bool CheckReturn(WxPayDataTool result)
 {
     if (result != null)
     {
         bool flag1 = (result.IsSet("return_code") && result.GetValue("return_code").ToString().ToUpper() == "SUCCESS");
         bool flag2 = (result.IsSet("result_code") && result.GetValue("result_code").ToString().ToUpper() == "SUCCESS");
         return(flag1 && flag2);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 3
0
        public static SortedDictionary <string, object> AppPay(string body, string NotifyUrl, string orderid, string openid, decimal paymoney, string clientIp)
        {
            //string logStr=@"D:\ppp\log\WXPay\wx_AppPay_" + DateTime.Today.ToString("yyyyMMdd") + ".txt";
            UnifiedOrderInfo info = new UnifiedOrderInfo()
            {
                Body           = body,
                OutTradeNo     = orderid,
                OpenId         = "",
                TotalFee       = (int)(Math.Round(paymoney, 2, MidpointRounding.AwayFromZero) * 100),
                SpbillCreateIp = clientIp,
                NotifyUrl      = NotifyUrl,
                Trade_type     = "APP"
            };
            string NonceStr = UtilTool.GenerateCode(32);

            info.NonceStr = NonceStr;
            //Util.WriteFile(logStr, @"WX AppPay Call======================" + DateTime.Now.ToString());
            //Util.WriteFile(logStr, @"Client IP:" + info.SpbillCreateIp);
            WxPayDataTool paytool = WxPayAction.UnifiedOrder(info);

            // Util.WriteFile(logStr,@"微信返回信息:"+paytool.ToJson());
            if (paytool.GetValue("return_code").ToString() == "SUCCESS")
            {
                if (paytool.GetValue("result_code").ToString() == "SUCCESS")
                {
                    WxPayDataTool  paytool2Sign = new WxPayDataTool();
                    PayAccountInfo payaccount   = new PayAccountInfo();
                    paytool2Sign.SetValue("appid", payaccount.AppId);
                    paytool2Sign.SetValue("noncestr", info.NonceStr);//此参数虽然是随机码  但是必须和之前请求微信时的随机码一致
                    paytool2Sign.SetValue("package", "Sign=WXPay");
                    paytool2Sign.SetValue("partnerid", payaccount.PartnerId);
                    paytool2Sign.SetValue("prepayid", paytool.GetValue("prepay_id"));
                    paytool2Sign.SetValue("timestamp", UtilTool.GenerateUnixTime());


                    string sign = paytool2Sign.MakeSign(payaccount.PartnerKey);

                    paytool2Sign.SetValue("sign", sign);

                    return(paytool2Sign.GetValues());
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Esempio n. 4
0
        public new WxPayDataTool  ProcessNotify()
        {
            WxPayDataTool notifyData = GetNotifyData(_InputStream);

            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在,则立即返回结果给微信支付后台
                WxPayDataTool res = new WxPayDataTool();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                // Util.WriteFile(@"D:\ppp\log\wx.txt", "统一下单支付结果回调出错:" + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();
            string out_trade_no   = notifyData.GetValue("out_trade_no").ToString();
            string total          = notifyData.GetValue("total_fee").ToString();
            //查询订单,判断订单真实性
            OrderqueryInfo info = new OrderqueryInfo()
            {
                Transaction_id = transaction_id
            };

            if (!QueryOrder(info))
            {
                //若订单查询失败,则立即返回结果给微信支付后台
                WxPayDataTool res = new WxPayDataTool();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                //Util.WriteFile(@"D:\ppp\log\wx.txt", "订单查询失败:" + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
            WxPayDataTool result = new WxPayDataTool();

            result.SetValue("transaction_id", transaction_id);
            result.SetValue("out_trade_no", out_trade_no);
            result.SetValue("total_fee", total);
            if (notifyData.IsSet("result_code"))
            {
                result.SetValue("result_code", notifyData.GetValue("result_code").ToString());
            }
            if (notifyData.IsSet("return_code"))
            {
                result.SetValue("return_code", notifyData.GetValue("return_code").ToString());
            }
            return(result);
        }
Esempio n. 5
0
        //微信支付(公众号支付)
        public static string WxJsApiPay(string body, string NotifyUrl, string orderid, string openid, decimal paymoney)
        {
            UnifiedOrderInfo info = new UnifiedOrderInfo()
            {
                Body       = body,
                OutTradeNo = orderid,
                OpenId     = openid,
                TotalFee   = (int)(Math.Round(paymoney, 2, MidpointRounding.AwayFromZero) * 100)
            };

            info.NotifyUrl = NotifyUrl;

            WxPayDataTool paytool  = WxPayAction.GetJsApiParameters(info);
            string        pay_json = paytool.ToJson();

            return(pay_json);
        }
Esempio n. 6
0
        //查询订单
        private bool QueryOrder(OrderqueryInfo info)
        {
            // string logStr = @"D:\ppp\log\WXPay\wx_OrderQuery_" + DateTime.Today.ToString("yyyyMMdd") + ".txt";
            WxPayDataTool result = new WxPayDataTool();

            result = WxPayAction.Orderquery(info);

            if (!WxPayAction.CheckReturn(result))
            {
                //Util.WriteFile(logStr, @"订单查询信息失败=============" + DateTime.Now.ToString());
                //Util.WriteFile(logStr, result.ToJson());
                return(false);
            }
            else
            {
                //Util.WriteFile(logStr, @"订单查询信息成功=============" + DateTime.Now.ToString());
                //Util.WriteFile(logStr, result.ToJson());
                return(true);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 订单查询
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static WxPayDataTool Orderquery(OrderqueryInfo info)
        {
            string        url     = "https://api.mch.weixin.qq.com/pay/orderquery";
            WxPayDataTool paytool = new WxPayDataTool();

            paytool.SetValue("appid", info.AppId);
            paytool.SetValue("mch_id", info.PartnerId);
            if (!string.IsNullOrEmpty(info.Transaction_id))
            {
                paytool.SetValue("transaction_id", info.Transaction_id);
            }
            if (!string.IsNullOrEmpty(info.Out_trade_no))
            {
                paytool.SetValue("out_trade_no", info.Out_trade_no);
            }
            paytool.SetValue("nonce_str", UtilTool.GenerateCode(32));    //随机字符串    不超过32位
            paytool.SetValue("sign", paytool.MakeSign(info.PartnerKey)); //签名
            WxPayDataTool result = GetResult(url, paytool, false);

            return(result);
        }
Esempio n. 8
0
        //微信(返回二维码链接扫码付)===原生支付
        public static string GetQRCode(string body, string NotifyUrl, string orderid, string openid, decimal paymoney)
        {
            UnifiedOrderInfo info = new UnifiedOrderInfo()
            {
                Body       = body,
                OutTradeNo = orderid,
                OpenId     = "",
                TotalFee   = (int)(Math.Round(paymoney, 2, MidpointRounding.AwayFromZero) * 100),
                NotifyUrl  = NotifyUrl,
                Trade_type = "NATIVE"
            };
            WxPayDataTool paytool = WxPayAction.UnifiedOrder(info);
            bool          flag    = WxPayAction.CheckReturn(paytool);

            if (flag)
            {
                return(paytool.GetValue("code_url").ToString());//获得统一下单接口返回的二维码链接
            }
            else
            {
                return("");
            }
        }
Esempio n. 9
0
        //H5调起js支付
        public static WxPayDataTool GetJsApiParameters(UnifiedOrderInfo info)
        {
            string        prepay_id = "";
            WxPayDataTool payresult = UnifiedOrder(info);

            if (CheckReturn(payresult))
            //   LogDB.DebugTest("统一下单信息出错:" + payresult.ToJson());
            // else
            {
                if (payresult.IsSet("prepay_id") && (prepay_id = payresult.GetValue("prepay_id").ToString()) != "")
                {
                    WxPayDataTool jsApiParam = new WxPayDataTool();
                    jsApiParam.SetValue("appId", info.AppId);
                    jsApiParam.SetValue("timeStamp", UtilTool.GenerateUnixTime());
                    jsApiParam.SetValue("nonceStr", UtilTool.GenerateCode(15));
                    jsApiParam.SetValue("package", "prepay_id=" + prepay_id);
                    jsApiParam.SetValue("signType", "MD5");
                    jsApiParam.SetValue("paySign", jsApiParam.MakeSign(info.PartnerKey));
                    return(jsApiParam);
                }
            }
            return(new WxPayDataTool());
        }
Esempio n. 10
0
        /// <summary>
        /// 接收从微信支付后台发送过来的数据并验证签名
        /// </summary>
        /// <returns>微信支付后台返回的数据</returns>
        public WxPayDataTool GetNotifyData(Stream InputStream)
        {
            //接收从微信后台POST过来的数据
            System.IO.Stream s = InputStream;
            int count          = 0;

            byte[]        buffer  = new byte[1024];
            StringBuilder builder = new StringBuilder();

            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();

            //转换数据格式并验证签名
            WxPayDataTool data = new WxPayDataTool();

            try
            {
                //LogDB.DebugTest("支付结果回调信息:" + builder.ToString());
                data.FromXml(builder.ToString());
            }
            catch (Exception ex)
            {
                //若签名错误,则立即返回结果给微信支付后台
                WxPayDataTool res = new WxPayDataTool();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", ex.Message);
                //LogDB.DebugTest("异步回调信息出错:" + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
            return(data);
        }
Esempio n. 11
0
        //统一订单
        public static WxPayDataTool UnifiedOrder(UnifiedOrderInfo info)
        {
            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

            if (string.IsNullOrWhiteSpace(info.NonceStr))
            {
                info.NonceStr = UtilTool.GenerateCode(32);
            }
            WxPayDataTool paytool = new WxPayDataTool();

            paytool.SetValue("appid", info.AppId);
            paytool.SetValue("mch_id", info.PartnerId);
            paytool.SetValue("device_info", "");
            paytool.SetValue("nonce_str", info.NonceStr);
            paytool.SetValue("body", info.Body);
            paytool.SetValue("attach", "");
            paytool.SetValue("out_trade_no", info.OutTradeNo);
            paytool.SetValue("total_fee", (int)info.TotalFee);
            paytool.SetValue("spbill_create_ip", info.SpbillCreateIp);
            paytool.SetValue("time_start", "");
            paytool.SetValue("time_expire", "");
            paytool.SetValue("goods_tag", info.GoodsTag);
            paytool.SetValue("notify_url", info.NotifyUrl);
            paytool.SetValue("trade_type", info.Trade_type);
            paytool.SetValue("openid", info.OpenId);
            string signstr = paytool.MakeSign(info.PartnerKey);

            paytool.SetValue("sign", signstr);
            WxPayDataTool result = GetResult(url, paytool, false);

            if (!CheckReturn(result))
            {
                //LogDB.DebugTest("统一下单信息失败:" + result.ToJson());
            }
            return(result);
        }
Esempio n. 12
0
 //检测返回结果
 public static bool CheckReturn(WxPayDataTool result)
 {
     return(WxPayAction.CheckReturn(result));
 }