资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

微信支付----PC扫码支付(查询微信支付订单判断是否支付成功)

    提示------------------几个状态没有加判断--------如:微信返回订单号不存在,微信系统异常。。。。
  
  
  
  c#webapi建议前端轮番查询订单状态
    #region  查询扫码订单状态
        /// 
        /// 查询扫码支付状态
        /// 
        /// 支付订单号
        /// 
        [HttpGet]
        public ResponseMessage PayMent_Result(string out_trade_no)
        {
            if (out_trade_no != null)
            {
                PayMent pay = new PayMent();
                OrderDetail detail = pay.geidingdan(out_trade_no);
                if (detail.trade_state == "SUCCESS")//支付成功
                {
                    try
                    {
                    自己的业务逻辑(数据库等等操作)
                    }
                    catch (Exception ex)
                    {
                        return new ResponseMessage { Code = -1};//系统异常
                    }


                    return new ResponseMessage { Code = 1 };
                }
                else if (detail.trade_state == "USERPAYING")//用户支付中 
                {
                    return new ResponseMessage { Code = 3 };
                    //return "3";

                }
                else if (detail.trade_state == "NOTPAY")//未支付 
                {
                    return new ResponseMessage { Code = 2 };
                    // return "2";

                }
                else if (detail.trade_state == "PAYERROR")//支付失败
                {
                    return new ResponseMessage { Code = 4 };
                    //return "4"; ;

                } 
                else if (detail.err_code == "ORDERNOTEXIST")
                {
                    return new ResponseMessage { Code = -1, Message = detail.err_code_des }; //

                }
                else if (detail.err_code == "SYSTEMERROR")
                {

                    return new ResponseMessage { Code = -1, Message = detail.err_code_des }; //
                }
                else {

                    return new ResponseMessage { Code = 0 }; //其他状态
                }
            }
            return new ResponseMessage { Code = -2 }; //支付订单号不能为空

        }
        #endregion
  
  #region 返回前端使用的code_url
  
  -----自己定义API接口
  -----调用NATIVEPayMent 方法传入方法的参数,判断返回结果如果为""说明微信统一下单失败了,可以在统一下单返回结果写入日志文件,查看失败原因。
  
  #endregion
  
  
  
  ----------ENDAPI---------------------------------------------------------------------------------------------
  
  /// 
        /// 微信扫码支付
        /// 
        /// 公众号ID
        /// 商品描述
        /// 商户号
        /// 终端IP
        /// 金额
        /// 商户订单号
        /// 证书密匙
        /// 二维码中包含的商品ID
        /// 
        public string NATIVEPayMent (string boby, string spbill_create_ip, Double total_fee, string out_trade_no, string product_id)
       {
           UnifiedOrder order = new UnifiedOrder();
           order.appid = WXconfig.appid;
           order.attach = "服务费";
           order.body = boby;
           order.device_info = "";
           order.mch_id = WXconfig.mch_id;
           order.nonce_str = TenpayUtil.getNoncestr();
           order.notify_url = Wxconfig.url;
           order.out_trade_no = out_trade_no;
           order.product_id = product_id;
           order.trade_type = "NATIVE";
           order.spbill_create_ip = spbill_create_ip;
           order.total_fee = Convert.ToInt32((total_fee) * 100);
           TenpayUtil tenpay = new TenpayUtil();
           string paySignKey = WXconfig.paysignkey;       
           string code_url = tenpay.getcode_url(order, paySignKey);
           return code_url;
       }

        /// 
        /// 查询扫码订单情况
        /// 
        /// 商户订单号
        /// 
        public OrderDetail geidingdan(string out_trade_no)
        {
            TenpayUtil tenpay = new TenpayUtil();
            OrderDetail detail = tenpay.getOrderDetail(out_trade_no);

            return detail;
        }
        
        
         public    class TenpayUtil
    {
        #region url
        /// 
        /// 统一支付接口
        /// 
        const string UnifiedPayUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        /// 
        /// 网页授权接口
        /// 
        const string access_tokenUrl = "https://api.weixin.qq.com/sns/oauth3/access_token";

        /// 
        /// 微信订单查询接口
        /// 
        const string OrderQueryUrl = "https://api.mch.weixin.qq.com/pay/orderquery";

        #endregion

        #region 随机串,时间截
        /// 
        /// 随机串
        /// 
        public static string getNoncestr()
        {
            Random random = new Random();
            return MD5Util.GetMD5(random.Next(1000).ToString(), "GBK").ToLower().Replace("s", "S");
        }


        /// 
        /// 时间截,自1970年以来的秒数
        /// 
        public static string getTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        #endregion

        /// 
        /// 网页授权接口
        /// 
        public static string getAccess_tokenUrl()
        {
            return access_tokenUrl;
        }


        #region 把微信扫码支付的回调XML转换集合
        public string GetXml(string xmlstring) {

            SortedDictionary < string, object> sd= GetInfoFromXml(xmlstring);
            string reslut = "";
            foreach (KeyValuePair s in sd) {
                if (s.Key == "result_code") {
                    reslut= s.Value.ToString();
                }
            }
            return reslut;
        }
        #endregion

        #region 获取微信签名
        /// 
        /// 获取微信签名
        /// 
        /// 
        /// 
        public string getsign(SortedDictionary sParams, string key)
        {
            int i = 0;
            string sign = string.Empty;
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair temp in sParams)
            {
                if (temp.Value.ToString() == "" || temp.Value == null || temp.Key.ToLower() == "sign")
                {
                    continue;
                }
                i++;
                sb.Append(temp.Key.Trim() + "=" + temp.Value.ToString() + "&");
            }
            sb.Append("key=" + key.Trim() + "");
            string signkey = sb.ToString();
            sign = MD5Util.GetMD5(signkey, "utf-8");
            //utf-8

            return sign;
        }

        #endregion

        #region post数据到指定接口并返回数据

        /// 
        /// post数据到指定接口并返回数据
        /// 
        public string PostXmlToUrl(string url, string postData,string type="")
        {
            string returnmsg = "";
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {                      
                if (type != "") {
                     Encoding encoding = Encoding.UTF8;
                    wc.Encoding = System.Text.Encoding.GetEncoding("GB2312");
                    byte[] data = encoding.GetBytes(postData);
                    returnmsg = Encoding.UTF8.GetString(wc.UploadData(url, "POST", data));
                }
                else 
                 returnmsg = wc.UploadString(url, "POST", postData);
            }
            return returnmsg;
        }

        #endregion

        #region 获取code_url
        /// 
        /// 获取code_url
        /// 
        public string getcode_url(UnifiedOrder order, string key)
        {
            string code_url = "";

            string post_data = getUnifiedOrderXml(order, key);
            string request_data = PostXmlToUrl(UnifiedPayUrl, post_data);
            //string request_data = HttpRequestutil.RequestUrl(UnifiedPayUrl, post_data, "post");     

            SortedDictionary requestXML = GetInfoFromXml(request_data);

            foreach (KeyValuePair k in requestXML)
            {


                if (k.Key == "code_url")
                {
                    code_url = k.Value.ToString();
                    break;
                }
            }
            return code_url;
        }

        #endregion

        #region 获取prepay_id

        /// 
        /// 获取prepay_id
        /// 
        public string getPrepay_id(UnifiedOrder order, string key)
        {
            string prepay_id = "";
            
                string post_data = getUnifiedOrderXml(order, key);
                string request_data = PostXmlToUrl(UnifiedPayUrl, post_data);              

                SortedDictionary requestXML = GetInfoFromXml(request_data);

                foreach (KeyValuePair k in requestXML)
                {
               
                    
                    if (k.Key == "prepay_id")
                    {
                        prepay_id = k.Value.ToString();
                        break;
                    }
                }          
            return prepay_id;
        }

        #endregion

        #region 获取微信订单明细
        /// 
        /// 获取微信订单明细
        /// 
        public OrderDetail getOrderDetail(string out_trade_no)
        {
            string post_data = getQueryOrderXml(out_trade_no);
            string request_data = PostXmlToUrl(OrderQueryUrl, post_data,"1");
            OrderDetail orderdetail = new OrderDetail();
            SortedDictionary requestXML = GetInfoFromXml(request_data);
            foreach (KeyValuePair k in requestXML)
            {
                switch (k.Key)
                {
                    case "retuen_code":
                        orderdetail.result_code = k.Value.ToString();
                        break;
                    case "return_msg":
                        orderdetail.return_msg = k.Value.ToString();
                        break;
                    case "appid":
                        orderdetail.appid = k.Value.ToString();
                        break;
                    case "mch_id":
                        orderdetail.mch_id = k.Value.ToString();
                        break;
                    case "nonce_str":
                        orderdetail.nonce_str = k.Value.ToString();
                        break;
                    case "sign":
                        orderdetail.sign = k.Value.ToString();
                        break;
                    case "result_code":
                        orderdetail.result_code = k.Value.ToString();
                        break;
                    case "err_code":
                        orderdetail.err_code = k.Value.ToString();
                        break;
                    case "err_code_des":
                        orderdetail.err_code_des = k.Value.ToString();
                        break;
                    case "trade_state":
                        orderdetail.trade_state = k.Value.ToString();
                        break;
                    case "device_info":
                        orderdetail.device_info = k.Value.ToString();
                        break;
                    case "openid":
                        orderdetail.openid = k.Value.ToString();
                        break;
                    case "is_subscribe":
                        orderdetail.is_subscribe = k.Value.ToString();
                        break;
                    case "trade_type":
                        orderdetail.trade_type = k.Value.ToString();
                        break;
                    case "bank_type":
                        orderdetail.bank_type = k.Value.ToString();
                        break;
                    case "total_fee":
                        orderdetail.total_fee = k.Value.ToString();
                        break;
                    case "coupon_fee":
                        orderdetail.coupon_fee = k.Value.ToString();
                        break;
                    case "fee_type":
                        orderdetail.fee_type = k.Value.ToString();
                        break;
                    case "transaction_id":
                        orderdetail.transaction_id = k.Value.ToString();
                        break;
                    case "out_trade_no":
                        orderdetail.out_trade_no = k.Value.ToString();
                        break;
                    case "attach":
                        orderdetail.attach = k.Value.ToString();
                        break;
                    case "time_end":
                        orderdetail.time_end = k.Value.ToString();
                        break;
                    default:
                        break;
                }
            }
            return orderdetail;
        }
        #endregion

        #region 

        #region 把XML数据转换为SortedDictionary

        /// 
        /// 把XML数据转换为SortedDictionary集合
        /// 
        /// 
        /// 
        protected SortedDictionary GetInfoFromXml(string xmlstring)
        {
            SortedDictionary sParams = new SortedDictionary();
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlstring);
                XmlElement root = doc.DocumentElement;
                int len = root.ChildNodes.Count;
                for (int i = 0; i < len; i++)
                {
                    string name = root.ChildNodes[i].Name;
                    if (!sParams.ContainsKey(name))
                    {
                        sParams.Add(name.Trim(), root.ChildNodes[i].InnerText.Trim());
                    }
                }
            }
            catch(Exception ex ) {

            }
            return sParams;
        }

        #endregion

        #region 微信统一下单接口xml参数整理
        /// 
        /// 微信统一下单接口xml参数整理
        /// 
        /// 微信支付参数实例
        /// 密钥
        /// 
        protected string getUnifiedOrderXml(UnifiedOrder order, string key)
        {
            string return_string = string.Empty;
            SortedDictionary sParams = new SortedDictionary();
            sParams.Add("appid", order.appid);
            sParams.Add("attach", order.attach);
            sParams.Add("body", order.body);
            sParams.Add("device_info", order.device_info);
            sParams.Add("mch_id", order.mch_id);
            sParams.Add("nonce_str", order.nonce_str);
            sParams.Add("notify_url", order.notify_url);
              //当 trade_type=JSAPI 时必输入
            if (order.trade_type == "JSAPI")
            {
                sParams.Add("openid", order.openid);
            }
            else if (order.trade_type == "NATIVE")
            {
                //当 trade_type=NATIVE 时必输入
                sParams.Add("product_id", order.product_id);
            }
            sParams.Add("out_trade_no", order.out_trade_no);
            sParams.Add("spbill_create_ip", order.spbill_create_ip);
            sParams.Add("total_fee", order.total_fee);
            sParams.Add("trade_type", order.trade_type);
            order.sign = getsign(sParams, key);
            sParams.Add("sign", order.sign);
           
            //拼接成XML请求数据
            StringBuilder sbPay = new StringBuilder();
            foreach (KeyValuePair k in sParams)
            {
                if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
                {
                    sbPay.Append("<" + k.Key + ">");
                }
                else
                {
                    sbPay.Append("<" + k.Key + ">" + k.Value + "");
                }
            }
            return_string = string.Format("{0}", sbPay.ToString());
            byte[] byteArray = Encoding.UTF8.GetBytes(return_string);
            return_string = Encoding.GetEncoding("GBK").GetString(byteArray);
            Byte[] temp = Encoding.UTF8.GetBytes(return_string);
            string dataGBK = Encoding.GetEncoding("utf-8").GetString(temp);         
            return return_string;
            //  GBK
        }

        #endregion

        #region 微信订单查询接口XML参数整理
        /// 
        /// 微信订单查询接口XML参数整理
        /// 
        /// 微信订单查询参数实例
        /// 密钥
        /// 
        protected string getQueryOrderXml(string out_trade_no)
        {
            string return_string = string.Empty;
            SortedDictionary sParams = new SortedDictionary();
            sParams.Add("appid", WXconfig.appid);
            sParams.Add("mch_id", WXconfig.mch_id);
           // sParams.Add("transaction_id", queryorder.transaction_id);
            sParams.Add("out_trade_no", out_trade_no);
            sParams.Add("nonce_str", getNoncestr());
           string  sign = getsign(sParams, WXconfig.paysignkey);
            sParams.Add("sign", sign);

            //拼接成XML请求数据
            StringBuilder sbPay = new StringBuilder();
            foreach (KeyValuePair k in sParams)
            {
                if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
                {
                    sbPay.Append("<" + k.Key + ">");
                }
                else
                {
                    sbPay.Append("<" + k.Key + ">" + k.Value + "");
                }
            }
            return_string = string.Format("{0}", sbPay.ToString().TrimEnd(','));
            return return_string;
        }
        #endregion 

        #endregion
    }
        
        public class MD5Util
    {

        public MD5Util() { 
         //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// 
        /// /** 获取大写的MD5签名结果 */
        /// 
        /// 数据
        /// 编码格式
        /// 
 
        
        public static string GetMD5(string encypStr, string charset)
        {
            string retStr;
            MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();

            //创建md5对象
            byte[] inputBye;
            byte[] outputBye;

            //使用GB2312编码方式把字符串转化为字节数组.
            try
            {
                inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
            }
            catch (Exception ex)
            {
                inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
            }
            outputBye = m5.ComputeHash(inputBye);

            retStr = System.BitConverter.ToString(outputBye);
            retStr = retStr.Replace("-", "").ToUpper();
            return retStr;
        }
        /// 
        /// SHA1加密
        /// 
        /// 
        /// 
        public static string sha1(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "sha1").ToLower();
        }

       

    }

阿瓦提ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

1.1  获取扫码支付的结果接口(建议轮番获取状态)

请求方式:http get

 

接口参数说明

名称

类型

必填

说明

out_trade_no

String

支付订单ID

 

 

返回结果: code :1//支付成功

返回结果: code :2//未支付

返回结果: code :3//用户支付中

返回结果: code :4//支付失败

返回结果: code :0//其他状态

返回结果: code :-1,Message="错误信息"//系统异常

 返回结果: code :-2//订单号不能为空

备注:(code不为-1,1,-2的时候轮番调取接口,建议时间为1s~2s,为-1的时候,提示系统异常,联系管理员。为1的时候跳到完成页面。)

订单查询说明:微信支付虽然有回调(个人认为比较坑),这里我是用了自己支付订单号去查询微信支付的该订单状态。(微信文档有说明,有2个方式,一个回调,一个用支付订单查询订单状态)


当前名称:微信支付----PC扫码支付(查询微信支付订单判断是否支付成功)
链接URL:http://cdkjz.cn/article/gpsodc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220