Beispiel #1
0
        /// <summary>
        /// 发送红包
        /// </summary>
        /// <param name="appId">重要,企业号请使用转openid返回的appid</param>
        /// <param name="openId">接受红包用户OpenId</param>
        /// <param name="amount">红包金额(单位:分)</param>
        /// <param name="ip">发送红包服务器IP</param>
        /// <param name="act_name">活动名称</param>
        /// <param name="wishing">红包祝福语</param>
        /// <param name="remark">备注信息</param>
        /// <param name="send_name">发红包公司</param>
        public static PackResult SendPack(string appId, string openId, int amount, string ip, string act_name, string wishing, string remark, string send_name)
        {
            var data = new WxPayData();

            data.SetValue("mch_billno", WechatHelper.GenerateOutTradeNo(PubPayConf.MchId));
            data.SetValue("mch_id", PubPayConf.MchId);
            data.SetValue("wxappid", appId);
            data.SetValue("send_name", send_name);
            data.SetValue("re_openid", openId);
            data.SetValue("total_amount", 100);
            data.SetValue("total_num", 1);
            data.SetValue("wishing", wishing);
            data.SetValue("client_ip", ip);
            data.SetValue("act_name", act_name);
            data.SetValue("remark", remark);
            data.SetValue("nonce_str", WechatHelper.GetNonceStrGuid());
            data.SetValue("sign", data.MakeSign(WxPayData.SIGN_TYPE_MD5));

            // 证书
            var cert = WechatHelper.GetRequestCert(PubPayConf.CertPath, PubPayConf.CertPass, PubPayConf.MchId.ToString());

            using (var client = new HttpClient(cert))
            {
                var result = client.PostAsync(SEND_REDPACK_URL, new StringContent(data.ToXml())).Result.Content.ReadAsStringAsync().Result;
                WechatHelper.Log.Debug("数据" + data.ToXml() + "  结果:" + SerializeHelper.JsonSerialize(result));

                // 解析返回结果
                //WxPayData res = new WxPayData();
                //res.FromXml(result);

                //return new PackResult()
                //{
                //    err_code = res.GetAsStringValue("err_code"),
                //    err_code_des = res.GetAsStringValue("err_code_des").ToString(),
                //    result_code = res.GetAsStringValue("result_code").ToString(),
                //    return_code = res.GetAsStringValue("return_code").ToString(),
                //    return_msg = res.GetAsStringValue("return_msg").ToString()
                //};

                // 红包不返回签名,验证签名必定报错
                var res = SerializeHelper.XmlDeserialize <PackResult>(result);
                return(res);
            }
        }
        /// <summary>
        /// 统一下单
        /// </summary>
        /// <param name="inputObj">提交给统一下单API的参数</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns>成功时返回,其他抛异常</returns>
        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 Exception("缺少统一支付接口必填参数out_trade_no!");
            }
            else if (!inputObj.IsSet("body"))
            {
                throw new Exception("缺少统一支付接口必填参数body!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new Exception("缺少统一支付接口必填参数total_fee!");
            }
            else if (!inputObj.IsSet("trade_type"))
            {
                throw new Exception("缺少统一支付接口必填参数trade_type!");
            }

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

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

            inputObj.SetValue("appid", PubConf.AppId);                         //公众账号ID
            inputObj.SetValue("mch_id", PubPayConf.MchId);                     //商户号
            inputObj.SetValue("spbill_create_ip", WechatHelper.GetServerIP()); //终端ip
            inputObj.SetValue("nonce_str", GenerateNonceStr());                //随机字符串
            inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256);   //签名类型

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

            var start = DateTime.Now;

            WechatHelper.Log.Debug("WxPayApi:UnfiedOrder request : " + xml);
            string response = HttpService.Post(xml, url, false, timeOut);

            WechatHelper.Log.Debug("WxPayApi:UnfiedOrder response : " + response);

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

            WxPayData result = new WxPayData();

            result.FromXml(response);

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

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// BJPay
        /// </summary>
        /// <param name="unifiedOrder"></param>
        /// <returns></returns>
        public static WxPayData GetXMResult(WxPayUnifiedOrder unifiedOrder)
        {
            //统一下单
            WxPayData data = new WxPayData();

            data.SetValue("body", unifiedOrder.body);
            data.SetValue("attach", unifiedOrder.attach);
            data.SetValue("out_trade_no", unifiedOrder.out_trade_no);
            data.SetValue("total_fee", unifiedOrder.total_fee);
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
            data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
            data.SetValue("goods_tag", unifiedOrder.goods_tag);
            data.SetValue("trade_type", "NATIVE");
            data.SetValue("spbill_create_ip", unifiedOrder.spbill_create_ip);
            data.SetValue("product_id", "12235413214070356458058");
            data.SetValue("notify_url", unifiedOrder.notify_url);

            WxPayData result = WxPayApi.UnifiedOrder(data);

            if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
            {
                throw new Exception("微信支付下单失败!");
            }

            return(result);
        }