/**
         *
         * 获取收货地址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);
        }
        /**
         * 调用统一下单,获得下单结果
         * @return 统一下单结果
         * @失败时抛异常WxPayException
         */

        public WxPayData GetUnifiedOrderResult(Enroll enroll, Course course)
        {
            //统一下单
            WxPayData data = new WxPayData();

            data.SetValue("body", "优杰学车-在线报名");
            data.SetValue("detail", $"客户于{enroll.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss")}在线报名{course.Name}{enroll.Amount}元."); //商品详情
            data.SetValue("out_trade_no", enroll.OrderNo);
            data.SetValue("total_fee", (Math.Round(enroll.Amount * 100, 0)).ToString());                                          //微信支付提交的金额是不能带小数点的,且是以分为单位,所以我们系统如果是以元为单位要处理下金额,即先乘以100,再去小数点
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
            data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
            data.SetValue("trade_type", "JSAPI");
            data.SetValue("openid", openid);

            WxPayData result = WxPayApi.UnifiedOrder(data);

            if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
            {
                throw new WxPayException("UnifiedOrder response error!");
            }

            unifiedOrderResult = result;
            return(result);
        }
示例#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);
        }