コード例 #1
0
        /// <summary>
        /// JS-SDK里面的wx.chooseWXPay()发起一个微信支付请求参数
        /// </summary>
        /// <param name="prepay_id">统一支付接口返回的prepay_id参数值</param>
        /// <param name="time">当前时间</param>
        /// <returns></returns>
        public static chooseWXPayModel chooseWXPay(string prepay_id, DateTime time)
        {
            /*
             * paySign 采用统一的微信支付 Sign 签名生成方法,注意这里 appId 也要参与签名,appId 与 config 中传入的 appId 一致。
             * 即最后参与签名的参数有appId, timeStamp, nonceStr, package, signType。
             */
            chooseWXPayModel chooseWXPay = new chooseWXPayModel();

            chooseWXPay.timestamp = Common.ConvertTime(time);
            chooseWXPay.nonceStr  = RandomCode.createRandomCode(16, true);
            chooseWXPay.package   = "prepay_id=" + prepay_id;
            chooseWXPay.signType  = "MD5";
            chooseWXPay.paySign   = "";

            Dictionary <string, string> chooseWXPayParams = new Dictionary <string, string>();

            chooseWXPayParams.Add("appId", Privacy.AppId);
            chooseWXPayParams.Add("timeStamp", chooseWXPay.timestamp.ToString());
            chooseWXPayParams.Add("nonceStr", chooseWXPay.nonceStr);
            chooseWXPayParams.Add("package", chooseWXPay.package);
            chooseWXPayParams.Add("signType", chooseWXPay.signType);

            string sign    = "";
            string package = GetPackage(chooseWXPayParams, out sign);

            chooseWXPay.paySign = sign;
            return(chooseWXPay);
        }
コード例 #2
0
        /// <summary>
        /// 微信红包支付
        /// </summary>
        /// <returns></returns>
        public static string GetBonus(string CurrentWXID, string act_id, string act_name, int money, string wishing, string remark, string nick_name, string send_name)
        {
            //构建微信红包接口参数对象
            BonusModel bonusModel = new BonusModel()
            {
                sign         = "",
                mch_billno   = Privacy.PartnerID + DateTime.Now.ToString("yyyyMMdd") + GetTenRandomNum(),
                mch_id       = Privacy.PartnerID,
                wxappid      = Privacy.AppId,
                nick_name    = nick_name,
                send_name    = send_name,
                re_openid    = CurrentWXID,
                total_amount = money,
                min_value    = money,
                max_value    = money,
                total_num    = 1,
                wishing      = wishing,
                client_ip    = System.Configuration.ConfigurationManager.AppSettings["serverIP"].ToString(),              //"219.234.83.88",
                act_name     = act_name,
                act_id       = act_id,
                remark       = remark,
                nonce_str    = RandomCode.createRandomCode(16, true),
            };
            Dictionary <string, string> singWXBonusParams = new Dictionary <string, string>();

            singWXBonusParams.Add("nonce_str", bonusModel.nonce_str);
            singWXBonusParams.Add("mch_billno", bonusModel.mch_billno);
            singWXBonusParams.Add("mch_id", bonusModel.mch_id);
            singWXBonusParams.Add("wxappid", bonusModel.wxappid);
            singWXBonusParams.Add("nick_name", bonusModel.nick_name);
            singWXBonusParams.Add("send_name", bonusModel.send_name);
            singWXBonusParams.Add("re_openid", bonusModel.re_openid);
            singWXBonusParams.Add("total_amount", bonusModel.total_amount.ToString());
            singWXBonusParams.Add("min_value", bonusModel.min_value.ToString());
            singWXBonusParams.Add("max_value", bonusModel.max_value.ToString());
            singWXBonusParams.Add("total_num", bonusModel.total_num.ToString());
            singWXBonusParams.Add("wishing", bonusModel.wishing);
            singWXBonusParams.Add("client_ip", bonusModel.client_ip);
            singWXBonusParams.Add("act_name", bonusModel.act_name);
            singWXBonusParams.Add("remark", bonusModel.remark);
            singWXBonusParams.Add("act_id", bonusModel.act_id);
            string sign    = "";
            string package = GetPackage(singWXBonusParams, out sign);

            bonusModel.sign = sign;
            string tempxmlstr = WeChatHelper4Net.XmlHelper.Serialize(bonusModel);
            string xml        = tempxmlstr.Substring(tempxmlstr.IndexOf("<sign>"));

            xml = "<xml>" + xml;
            //调用微信红包接口,支付用户红包奖金
            string resultXml = Send(xml, "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");

            //处理接口返回数据
            resultXml = resultXml.Replace("<xml>", "<BonusResultModel>").Replace("</xml>", "</BonusResultModel>");
            return(resultXml);
        }
コード例 #3
0
        /// <summary>
        /// 获取JS-SDK配置信息
        /// </summary>
        /// <param name="AppId">AppId为空时默认取配置文件appSettings节点key=WeChatAppId</param>
        /// <param name="jsapi_ticket">jsapi_ticket</param>
        /// <param name="url">url(当前网页的URL,不包含#及其后面部分,要用Request.Url.AbsoluteUri,否则会存在编码问题)</param>
        /// <returns></returns>
        public static Models.JSSDK.JSSDKConfig GetConfig(string AppId, string jsapi_ticket, string url)
        {
            if (string.IsNullOrWhiteSpace(jsapi_ticket))
            {
                throw new ArgumentNullException(nameof(jsapi_ticket));
            }
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException(nameof(url));
            }
            AppId = string.IsNullOrWhiteSpace(AppId) ? Privacy.AppId : AppId;

            Models.JSSDK.JSSDKConfig config = new Models.JSSDK.JSSDKConfig();
            config.appId     = AppId;
            url              = Common.CleanUrl(url);                  //移除url里面#及#后面的部分
            config.timestamp = Common.ConvertTime(DateTime.Now);
            config.nonceStr  = RandomCode.createRandomCode(16, true); //生成16位随机字符串,数字,大写英文字母,小写英文字母

            /*
             * 关于URL编码导致签名错误的问题:
             * 微信建议:确认url是页面完整的url(请在当前页面alert(location.href.split('#')[0])确认),包括'http(s)://'部分,以及'?'后面的GET参数部分,但不包括'#'hash后面的部分。
             * 注意必须以http://或https://开头,分别支持80端口和443端口。其它协议或端口都不行!
             */

            /*
             * 对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。
             * 这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。
             */
            Dictionary <string, string> WeiXinUrlParameters = new Dictionary <string, string>();

            WeiXinUrlParameters.Add("jsapi_ticket", jsapi_ticket);
            WeiXinUrlParameters.Add("noncestr", config.nonceStr);
            WeiXinUrlParameters.Add("timestamp", config.timestamp.ToString());
            WeiXinUrlParameters.Add("url", url);
            string stringTemp = WeiXinUrlParameters.Sort().ToURLParameter();

            /*
             * 对string1进行sha1签名,得到signature
             */
            config.signature = Common.SHA1Encrypt(stringTemp);
            return(config);
        }
コード例 #4
0
        /// <summary>
        /// 微信支付统一下单接口
        /// </summary>
        /// <param name="unifiedorder">统一下单参数</param>
        /// <param name="time">当前时间</param>
        /// <returns></returns>
        public static unifiedorderResultModel unifiedorder(unifiedorderModel unifiedorder, DateTime time)
        {
            unifiedorder.out_trade_no = !string.IsNullOrWhiteSpace(unifiedorder.out_trade_no) ? unifiedorder.out_trade_no : (Privacy.PartnerID + time.ToString("yyyyMMddHHmmss") + RandomCode.createRandomCode(3, true));
            unifiedorder.fee_type     = !string.IsNullOrWhiteSpace(unifiedorder.fee_type) ? unifiedorder.fee_type : "CNY";
            unifiedorder.time_start   = !string.IsNullOrWhiteSpace(unifiedorder.time_start) ? unifiedorder.time_start : time.ToString("yyyyMMddHHmmss");
            unifiedorder.trade_type   = !string.IsNullOrWhiteSpace(unifiedorder.trade_type) ? unifiedorder.trade_type : "JSAPI";

            unifiedorder.appid     = Privacy.AppId;
            unifiedorder.mch_id    = Privacy.PartnerID;
            unifiedorder.nonce_str = RandomCode.createRandomCode(16, true);
            unifiedorder.sign      = "";

            Dictionary <string, string> unifiedorderParams = new Dictionary <string, string>();

            unifiedorderParams.Add("appid", unifiedorder.appid);
            unifiedorderParams.Add("mch_id", unifiedorder.mch_id);
            unifiedorderParams.Add("device_info", unifiedorder.device_info);
            unifiedorderParams.Add("nonce_str", unifiedorder.nonce_str);
            unifiedorderParams.Add("sign", unifiedorder.sign);
            unifiedorderParams.Add("body", unifiedorder.body);
            unifiedorderParams.Add("detail", unifiedorder.detail);
            unifiedorderParams.Add("attach", unifiedorder.attach);
            unifiedorderParams.Add("out_trade_no", unifiedorder.out_trade_no);
            unifiedorderParams.Add("fee_type", unifiedorder.fee_type);
            unifiedorderParams.Add("total_fee", unifiedorder.total_fee.ToString());
            unifiedorderParams.Add("spbill_create_ip", unifiedorder.spbill_create_ip);
            unifiedorderParams.Add("time_start", unifiedorder.time_start);
            unifiedorderParams.Add("time_expire", unifiedorder.time_expire);
            unifiedorderParams.Add("goods_tag", unifiedorder.goods_tag);
            unifiedorderParams.Add("notify_url", unifiedorder.notify_url);
            unifiedorderParams.Add("trade_type", unifiedorder.trade_type);
            unifiedorderParams.Add("product_id", unifiedorder.product_id);
            unifiedorderParams.Add("openid", unifiedorder.openid);

            string sign    = "";
            string package = GetPackage(unifiedorderParams, out sign);

            unifiedorder.sign          = sign;
            unifiedorderParams["sign"] = sign;

            /*
             * 注:参数值用XML转义即可,CDATA标签用于说明数据不被XML解析器解析。
             */
            string unifiedorderXml = unifiedorderParams.ToXml();

            unifiedorderXml = unifiedorderXml.Replace("<unifiedorderModel>", "<xml>").Replace("</unifiedorderModel>", "</xml>");
            string resultXml = string.Empty;

            try
            {
                /*
                 * 得到最终发送的数据:
                 */
                resultXml = HttpRequestHelper.Request("https://api.mch.weixin.qq.com/pay/unifiedorder", unifiedorderXml, HttpRequestHelper.Method.POST, System.Text.Encoding.UTF8);
                resultXml = resultXml.Replace("<xml>", "<unifiedorderResultModel>").Replace("</xml>", "</unifiedorderResultModel>");
                unifiedorderResultModel result = WeChatHelper4Net.XmlHelper.DeSerialize <unifiedorderResultModel>(resultXml);
                return(result);
            }
            catch (Exception Ex)
            {
                LogHelper.Save("Pay > unifiedorder:unifiedorderXml=" + unifiedorderXml + ",resultXml=" + resultXml, "Pay", LogType.Error, LogTime.day);
                throw Ex;
            }
        }