コード例 #1
0
        /**
         *
         * 测速上报接口实现
         * @param WxPayData inputObj 提交给测速上报接口的参数
         * @param int timeOut 测速上报接口超时时间
         * @throws WxPayException
         * @return 成功时返回测速上报接口返回的结果,其他抛异常
         */

        public static WxPayData Report(WxPayData inputObj, int timeOut = 1)
        {
            string url = "https://api.mch.weixin.qq.com/payitil/report";

            //检测必填参数
            if (!inputObj.IsSet("interface_url"))
            {
                throw new WxPayException("接口URL,缺少必填参数interface_url!");
            }
            if (!inputObj.IsSet("return_code"))
            {
                throw new WxPayException("返回状态码,缺少必填参数return_code!");
            }
            if (!inputObj.IsSet("result_code"))
            {
                throw new WxPayException("业务结果,缺少必填参数result_code!");
            }
            if (!inputObj.IsSet("user_ip"))
            {
                throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
            }
            if (!inputObj.IsSet("execute_time_"))
            {
                throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
            }

            JsApiConfig jsApiConfig = new JsApiConfig();

            inputObj.SetValue("appid", jsApiConfig.AppId);                      //公众账号ID
            inputObj.SetValue("mch_id", jsApiConfig.Partner);                   //商户号
            inputObj.SetValue("user_ip", ToolHelper.GetIP());                   //终端ip
            inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss")); //商户上报时间
            inputObj.SetValue("nonce_str", GenerateNonceStr());                 //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign());                     //签名
            string xml = inputObj.ToXml();

            string response = HttpService.Post(xml, url, false, timeOut);

            WxPayData result = new WxPayData();

            result.FromXml(response);
            return(result);
        }
コード例 #2
0
        /**
         *
         * 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
         * @return string 共享收货地址js函数需要的参数,json格式可以直接做参数使用
         */

        public string GetEditAddressParameters()
        {
            string parameter = "";

            try
            {
                string host        = HttpContext.Current.Request.Url.Host;
                string path        = HttpContext.Current.Request.Path;
                string queryString = HttpContext.Current.Request.Url.Query;
                //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
                string url = "http://" + host + path + queryString;

                //构造需要用SHA1算法加密的数据
                WxPayData signData = new WxPayData();
                signData.SetValue("appid", jsApiConfig.AppId);
                signData.SetValue("url", url);
                signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp());
                signData.SetValue("noncestr", WxPayApi.GenerateNonceStr());
                signData.SetValue("accesstoken", access_token);
                string param = signData.ToUrl();

                //SHA1加密
                string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1");

                //获取收货地址js函数入口参数
                WxPayData afterData = new WxPayData();
                afterData.SetValue("appId", jsApiConfig.AppId);
                afterData.SetValue("scope", "jsapi_address");
                afterData.SetValue("signType", "sha1");
                afterData.SetValue("addrSign", addrSign);
                afterData.SetValue("timeStamp", signData.GetValue("timestamp"));
                afterData.SetValue("nonceStr", signData.GetValue("noncestr"));

                //转为json格式
                parameter = afterData.ToJson();
            }
            catch (Exception ex)
            {
                throw new WxPayException(ex.ToString());
            }

            return(parameter);
        }
コード例 #3
0
        public static async Task <WxPayData> GetJSSDKConfig()
        {
            JsApiConfig jsApiConfig = new JsApiConfig();

            string appid = jsApiConfig.AppId;

            string secret = jsApiConfig.AppSecret;

            string timestamp = WxPayApi.GenerateTimeStamp();

            string noncestr = WxPayApi.GenerateNonceStr();

            string signature = "";

            //签名算法  https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

            //1. 获取AccessToken(有效期7200秒,开发者必须在自己的服务全局缓存access_token)

            WxTicketHelper helper = new WxTicketHelper();

            WxTicket wxticket = await helper.GetWxTicketAsync(WxTicketType.ACCESS_TOKEN);

            string access_token = string.Empty;

            if (wxticket == null || wxticket.ExpireTime < ConvertDateTimeInt(DateTime.Now))
            {
                string url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;

                string result = HttpService.Get(url1);

                JsonData jd = JsonMapper.ToObject(result);
                if (!((System.Collections.IDictionary)jd).Contains("access_token"))
                {
                    LogHelper.Error("access_token is null:" + result);
                    return(null);
                }
                access_token = (string)jd["access_token"];

                await helper.SaveAsync(WxTicketType.ACCESS_TOKEN, new WxTicket
                {
                    Ticket     = access_token,
                    ExpireTime = ConvertDateTimeInt(DateTime.Now) + 7180
                });
            }
            else
            {
                access_token = wxticket.Ticket;
            }

            //2. 用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket)
            wxticket = await helper.GetWxTicketAsync(WxTicketType.TICKET);

            string ticket = string.Empty;

            if (wxticket == null || wxticket.ExpireTime < ConvertDateTimeInt(DateTime.Now))
            {
                string url2 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";

                string result2 = HttpService.Get(url2);

                JsonData jd2 = JsonMapper.ToObject(result2);

                if (!((System.Collections.IDictionary)jd2).Contains("ticket"))
                {
                    return(null);
                }

                ticket = (string)jd2["ticket"];

                await helper.SaveAsync(WxTicketType.TICKET, new WxTicket
                {
                    Ticket     = ticket,
                    ExpireTime = ConvertDateTimeInt(DateTime.Now) + 7180
                });
            }
            else
            {
                ticket = wxticket.Ticket;
            }

            //3. 开始签名

            Uri    uri     = HttpContext.Current.Request.Url;
            string now_url = (uri.Scheme + "://" + uri.Host + HttpContext.Current.Request.RawUrl).Split('#')[0];

            string no_jiami = $"jsapi_ticket={ticket}&noncestr={noncestr}&timestamp={timestamp}&url={now_url}";

            //SHA1加密

            signature = SHA1_Hash(no_jiami);

            WxPayData data = new WxPayData();

            data.SetValue("appId", appid);

            data.SetValue("timestamp", timestamp);

            data.SetValue("nonceStr", noncestr);

            data.SetValue("signature", signature);

            return(data);
        }
コード例 #4
0
        /**
         *
         * 统一下单
         * @param WxPaydata inputObj 提交给统一下单API的参数
         * @param int timeOut 超时时间
         * @throws WxPayException
         * @return 成功时返回,其他抛异常
         */

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

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no"))
            {
                throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
            }
            else if (!inputObj.IsSet("body"))
            {
                throw new WxPayException("缺少统一支付接口必填参数body!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new WxPayException("缺少统一支付接口必填参数total_fee!");
            }
            else if (!inputObj.IsSet("trade_type"))
            {
                throw new WxPayException("缺少统一支付接口必填参数trade_type!");
            }

            //关联参数
            if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
            {
                throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
            }
            if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
            {
                throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
            }

            JsApiConfig jsApiConfig = new JsApiConfig();

            //异步通知url未设置,则使用配置文件中的url
            if (!inputObj.IsSet("notify_url"))
            {
                inputObj.SetValue("notify_url", jsApiConfig.Notify_url);//异步通知url
            }

            inputObj.SetValue("appid", jsApiConfig.AppId);             //公众账号ID
            inputObj.SetValue("mch_id", jsApiConfig.Partner);          //商户号
            inputObj.SetValue("spbill_create_ip", ToolHelper.GetIP()); //终端ip
            inputObj.SetValue("nonce_str", GenerateNonceStr());        //随机字符串

            //签名
            inputObj.SetValue("sign", inputObj.MakeSign());
            string xml = inputObj.ToXml();

            var start = DateTime.Now;

            string response = HttpService.Post(xml, url, false, timeOut);

            var end      = DateTime.Now;
            int timeCost = (int)((end - start).TotalMilliseconds);

            WxPayData result = new WxPayData();

            result.FromXml(response);

            ReportCostTime(url, timeCost, result);//测速上报

            return(result);
        }