Beispiel #1
0
        public string Post()
        {
            //通过code获取access_token
            string code = Request.Query["code"];
            string url  = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd001485d83e3628b&secret={WeChatConfig.APP_SECRET}&code={code}&grant_type=authorization_code";

            _logger.Info("unifiedorder->请求access_token地址为:" + url);
            var client  = new RestClient(url);
            var request = new RestRequest(Method.GET);

            request.Timeout = 10000;
            IRestResponse response = client.Execute(request);

            _logger.Info("unifiedorder->请求access_token返回内容为:" + url);
            string openid = JsonConvert.DeserializeObject <Data>(response.Content)?.openid;

            _logger.Info("获取到openid为:" + openid);
            _logger.Info("开始支付:" + url);
            url             = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            client          = new RestClient(url);
            request         = new RestRequest(Method.POST);
            request.Timeout = 10000;
            WeChatPayData payData = new WeChatPayData();

            payData.SetValue("appid", WeChatConfig.APP_ID);
            payData.SetValue("mch_id", WeChatConfig.MCH_ID);
            payData.SetValue("nonce_str", payData.GenerateNonceStr());
            payData.SetValue("body", "test");
            payData.SetValue("out_trade_no", DateTime.Now.ToString("yyyyMMddHHmmss"));
            payData.SetValue("total_fee", 1);
            payData.SetValue("spbill_create_ip", "127.0.0.1");
            payData.SetValue("notify_url", "http://hexiaodong.top/api/pay/notify_url");
            payData.SetValue("trade_type", "JSAPI");
            payData.SetValue("openid", openid);                                 //"oTSBW5wX09Qwiidfk1sarDeXq-hY"
            payData.SetValue("sign_type", WeChatPayData.SIGN_TYPE_HMAC_SHA256); //签名类型
            payData.SetValue("sign", payData.MakeSign());
            string xml = payData.ToXml();

            request.AddJsonBody(xml);
            response = client.Execute(request);
            payData.FromXml(response.Content);
            WeChatPayData jsApiParam = new WeChatPayData();

            jsApiParam.SetValue("appId", WeChatConfig.APP_ID);
            jsApiParam.SetValue("timeStamp", jsApiParam.GenerateTimeStamp());
            jsApiParam.SetValue("nonceStr", jsApiParam.GenerateNonceStr());
            jsApiParam.SetValue("package", "prepay_id=" + payData.GetValue("prepay_id"));
            jsApiParam.SetValue("signType", WeChatPayData.SIGN_TYPE_HMAC_SHA256);
            jsApiParam.SetValue("paySign", jsApiParam.MakeSign());
            return(jsApiParam.ToJson());
        }
Beispiel #2
0
        private string GetJsApiParameters(string keyvalue, string appid, string prepayid)
        {
            WeChatPayData jsApiParam = new WeChatPayData();

            jsApiParam.SetValue("appId", appid);
            jsApiParam.SetValue("timeStamp", GenerateTimeStamp());
            jsApiParam.SetValue("nonceStr", GenerateNonceStr());
            jsApiParam.SetValue("package", "prepay_id=" + prepayid);
            jsApiParam.SetValue("signType", "MD5");
            jsApiParam.SetValue("paySign", jsApiParam.MakeSign(keyvalue));

            string parameters = jsApiParam.ToJson();

            return(parameters);
        }
Beispiel #3
0
        public void GetNotifyUrl()
        {
            //接收从微信后台POST过来的数据
            StringBuilder builder = new StringBuilder();

            using (Stream s = Request.Body)
            {
                using (StreamReader sr = new StreamReader(s))
                    builder.Append(sr.ReadToEnd());
            }
            WeChatPayData data = new WeChatPayData();

            data.FromXml(builder.ToString());
            _logger.Info("支付成功回调参数:" + data.ToJson());
            ControllerContext.HttpContext.Response.Redirect("http://hexiaodong.top/product/callback?data=" + data.ToJson());
        }
Beispiel #4
0
    /// <summary>
    ///  获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="url"></param>
    /// <returns>共享收货地址js函数需要的参数,json格式可以直接做参数使用</returns>
    public static string MakeEditAddressJsParam(string accessToken, string url)
    {
        string editAddrParam = string.Empty;

        try
        {
            //参与加密的参数key全部小写
            WeChatPayData signData = new WeChatPayData();
            signData.SetValue("appid", Config.APPID);
            signData.SetValue("url", url);
            signData.SetValue("timestamp", WeChatPayData.MakeTimeStamp());
            signData.SetValue("noncestr", WeChatPayData.MakeNonceStr());
            signData.SetValue("accesstoken", accessToken);
            string param = signData.ToSignStr();

            Log.Debug("MakeEditAddressJsParam", "SHA1 encrypt param : " + param);
            //SHA1加密
            string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1");
            Log.Debug("MakeEditAddressJsParam", "SHA1 encrypt result : " + addrSign);

            //构造收货地址js函数入口参数
            WeChatPayData paramsData = new WeChatPayData();
            paramsData.SetValue("appId", Config.APPID);
            paramsData.SetValue("scope", "jsapi_address");
            paramsData.SetValue("signType", "sha1");
            paramsData.SetValue("addrSign", addrSign);
            paramsData.SetValue("timeStamp", signData.GetValue("timestamp"));
            paramsData.SetValue("nonceStr", signData.GetValue("noncestr"));

            //转为json格式
            editAddrParam = paramsData.ToJson();
            Log.Debug("MakeEditAddressJsParam", editAddrParam);
        }
        catch (Exception ex)
        {
            Log.Error("MakeEditAddressJsParam", ex.ToString());
            throw ex;
        }

        return(editAddrParam);
    }
Beispiel #5
0
    /// <summary>
    /// 根据预支付回话标示prepay_id构造JS参数
    /// </summary>
    /// <param name="prepayID"></param>
    /// <returns></returns>
    public static string MakeWXPayJsParam(string prepayID)
    {
        //返回微信客户端调用JS支付所需的参数
        string wxJsApiParam = string.Empty;

        try
        {
            //* 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数,
            //* 微信浏览器调起JSAPI时的输入参数格式如下:
            //* {
            //*   "appId" : "wx2421b1c4370ec43b",     //公众号名称,由商户传入
            //*   "timeStamp":" 1395712654",         //时间戳,自1970年以来的秒数
            //*   "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串
            //*   "package" : "prepay_id=u802345jgfjsdfgsdg888",
            //*   "signType" : "MD5",         //微信签名方式:
            //*   "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
            //* }
            //* @return string 微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用
            //* 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7

            WeChatPayData jsPayData = new WeChatPayData();
            jsPayData.SetValue("appId", Config.APPID);
            jsPayData.SetValue("timeStamp", WeChatPayData.MakeTimeStamp());
            jsPayData.SetValue("nonceStr", WeChatPayData.MakeNonceStr());
            jsPayData.SetValue("package", "prepay_id=" + prepayID);
            jsPayData.SetValue("signType", "MD5");
            jsPayData.SetValue("paySign", jsPayData.MakeSign());

            wxJsApiParam = jsPayData.ToJson();

            Log.Debug("jsApiParam", wxJsApiParam);
        }
        catch (Exception ex)
        {
            Log.Error("WxPayAPI", ex.Message);
            throw ex;
        }

        return(wxJsApiParam);
    }
Beispiel #6
0
    /// <summary>
    /// 微信支付统一下单,接口参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
    /// 除被扫支付场景以外,商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识prepay_id,有效期2小时,再按扫码、JSAPI、APP等不同场景生成交易串调起支付。
    /// </summary>
    /// <param name="po"></param>
    /// <param name="jStateCode"></param>
    /// <returns>正常返回prepay_id,jStateCode为空;如果有错误发生,则prepay_id为空,jStateCode有值</returns>
    public static string CallUnifiedOrderAPI(ProductOrder po, out WeChatPayData stateCode)
    {
        string prepayID = string.Empty;

        //统一下单API返回的状态码
        stateCode = new WeChatPayData();

        try
        {
            WeChatPayData sendPayData;
            WeChatPayData recvPayData;

            //生成统一下单接口报文
            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            sendPayData = new WeChatPayData();
            sendPayData.SetValue("appid", Config.APPID);                     //必填
            sendPayData.SetValue("mch_id", Config.MCHID);                    //必填
            sendPayData.SetValue("device_info", "WEB");
            sendPayData.SetValue("nonce_str", WeChatPayData.MakeNonceStr()); //必填
            sendPayData.SetValue("body", po.ProductNames);                   //必填
            sendPayData.SetValue("detail", po.OrderDetails);
            sendPayData.SetValue("attach", "订单自定义数据");
            sendPayData.SetValue("out_trade_no", po.OrderID);                                                                  //必填
            sendPayData.SetValue("fee_type", "CNY");
            sendPayData.SetValue("total_fee", (po.OrderPrice * 100).ToString("F0"));                                           //必填,单位为【分】
            sendPayData.SetValue("spbill_create_ip", po.ClientIP);                                                             //必填
            sendPayData.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
            sendPayData.SetValue("time_expire", DateTime.Now.AddMinutes(Config.WeChatOrderExpire).ToString("yyyyMMddHHmmss")); //最短失效时间间隔必须大于5分钟,可为空,默认2小时,也即prepay_id有效期
            sendPayData.SetValue("goods_tag", "商品标记,代金券或立减优惠功能的参数");
            sendPayData.SetValue("notify_url", Config.PayNotifyUrl);                                                           //必填,微信支付成功后异步通知url
            sendPayData.SetValue("trade_type", "JSAPI");                                                                       //必填
            sendPayData.SetValue("openid", po.Purchaser.OpenID);                                                               //trade_type = JSAPI,此参数必传

            //生成报文签名
            sendPayData.SetValue("sign", sendPayData.MakeSign());   //必填

            //生成接口所需的XML报文格式
            string sendMsg = sendPayData.ToXml();

            var start = DateTime.Now;

            Log.Debug("JsApiPay", "UnfiedOrder request" + sendMsg);
            //调用微信支付统一下单接口,并获取返回报文
            string recvMsg = HttpService.Post(sendMsg, url, false, Config.WeChatAPITimeout);
            Log.Debug("JsApiPay", "UnfiedOrder response" + recvMsg);

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

            Log.Debug("JsApiPay", "UnfiedOrder time cost" + timeCost.ToString());

            //recvMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wxa1ce5cd8d4bc5b3f]]></appid><mch_id><![CDATA[1296478401]]></mch_id><device_info><![CDATA[WEB]]></device_info><nonce_str><![CDATA[vjqH6K12oBv9iEyW]]></nonce_str><sign><![CDATA[42916A4F50AAD1AA1B9A3D222371EBAE]]></sign><result_code><![CDATA[SUCCESS]]></result_code><prepay_id><![CDATA[wx20151223230752785abc1a610183113624]]></prepay_id><trade_type><![CDATA[JSAPI]]></trade_type></xml>";

            if (string.IsNullOrEmpty(recvMsg))
            {
                Log.Error("JsApiPay", "UnfiedOrder接口没有返回数据");

                throw new Exception("UnfiedOrder接口没有返回数据");
            }

            recvPayData = new WeChatPayData();
            //根据XML返回报文生成返回报文对象
            recvPayData.FromXml(recvMsg);

            //校验通信标示
            if (recvPayData.IsSet("return_code") && recvPayData.GetValue("return_code").ToString().ToUpper() == "SUCCESS")
            {
                //当return_code为SUCCESS,才有sign参数返回,校验对方报文签名
                if (!recvPayData.CheckSign())
                {
                    stateCode.SetValue("return_code", "FAIL");
                    stateCode.SetValue("return_msg", "微信支付返回的签名错误,请在安全的网络环境中进行支付!");
                    Log.Error("CallUnifiedOrderAPI::SIGN签名错误", recvPayData.GetValue("sign").ToString());
                }
                else
                {
                    //校验业务结果result_code也是SUCCESS,才有prepay_id返回
                    if (recvPayData.IsSet("result_code") && recvPayData.GetValue("result_code").ToString().ToUpper() == "SUCCESS" && recvPayData.IsSet("prepay_id"))
                    {
                        //获取prepay_id
                        prepayID = recvPayData.GetValue("prepay_id").ToString();
                    }
                    else  //可能是订单已支付、已关闭、订单号重复等错误原因
                    {
                        stateCode.SetValue("result_code", "FAIL");
                        stateCode.SetValue("err_code", recvPayData.GetValue("err_code").ToString());
                        stateCode.SetValue("err_code_des", recvPayData.GetValue("err_code_des").ToString());
                        Log.Error("CallUnifiedOrderAPI", stateCode.ToJson());
                    }
                }
            }
            else //可能为我方报文sign签名错误或参数格式错误
            {
                stateCode.SetValue("return_code", "FAIL");
                stateCode.SetValue("return_msg", recvPayData.GetValue("return_msg").ToString());
                Log.Error("CallUnifiedOrderAPI", stateCode.ToJson());
            }
        }
        catch (Exception ex)
        {
            Log.Error("WxPayAPI", ex.Message);
            throw ex;
        }

        return(prepayID);
    }