/// <summary> /// 查询红包记录 /// </summary> /// <param name="appId">公众账号AppID</param> /// <param name="mchId">商户MchID</param> /// <param name="tenPayKey">支付密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置</param> /// <param name="tenPayCertPath">证书地址(硬盘地址,形如E://cert//apiclient_cert.p12)</param> /// <param name="mchBillNo">商家订单号</param> /// <returns></returns> public static SearchRedPackResult SearchRedPack(string appId, string mchId, string tenPayKey, string tenPayCertPath, string mchBillNo) { string nonceStr = TenPayV3Util.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(); packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("appid", appId); //公众账号ID packageReqHandler.SetParameter("mch_id", mchId); //商户号 packageReqHandler.SetParameter("mch_billno", mchBillNo); //填入商家订单号 string sign = packageReqHandler.CreateMd5Sign("key", tenPayKey); packageReqHandler.SetParameter("sign", sign); //签名 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/queryworkwxredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = tenPayCertPath; //私钥(在安装证书时设置) string password = mchId; //调用证书 //X509Certificate cer = new X509Certificate(cert, password); X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); XmlDocument doc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed(); #region 发起post请求,载入到doc中 #if NET451 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string response = streamReader.ReadToEnd(); doc.LoadXml(response); #else HttpClientHandler handler = new HttpClientHandler(); handler.ClientCertificates.Add(cer); HttpClient client = new HttpClient(handler); HttpContent hc = new StringContent(data); var request = client.PostAsync(url, hc).Result; var response = request.Content.ReadAsStreamAsync().Result; doc.Load(response); #endif #endregion SearchRedPackResult searchReturn = new SearchRedPackResult { err_code = "", err_code_des = "" }; if (doc.SelectSingleNode("/xml/return_code") != null) { searchReturn.return_code = (doc.SelectSingleNode("/xml/return_code").InnerText.ToUpper() == "SUCCESS"); } if (doc.SelectSingleNode("/xml/return_msg") != null) { searchReturn.return_msg = doc.SelectSingleNode("/xml/return_msg").InnerText; } if (searchReturn.return_code == true) { //redReturn.sign = doc.SelectSingleNode("/xml/sign").InnerText; if (doc.SelectSingleNode("/xml/result_code") != null) { searchReturn.result_code = (doc.SelectSingleNode("/xml/result_code").InnerText.ToUpper() == "SUCCESS"); } if (searchReturn.result_code == true) { if (doc.SelectSingleNode("/xml/mch_billno") != null) { searchReturn.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { searchReturn.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/detail_id") != null) { searchReturn.detail_id = doc.SelectSingleNode("/xml/detail_id").InnerText; } if (doc.SelectSingleNode("/xml/status") != null) { searchReturn.status = doc.SelectSingleNode("/xml/status").InnerText; } if (doc.SelectSingleNode("/xml/send_type") != null) { searchReturn.send_type = doc.SelectSingleNode("/xml/send_type").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { searchReturn.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } if (doc.SelectSingleNode("/xml/reason") != null) { searchReturn.reason = doc.SelectSingleNode("/xml/reason").InnerText; } if (doc.SelectSingleNode("/xml/send_time") != null) { searchReturn.send_time = doc.SelectSingleNode("/xml/send_time").InnerText; } if (doc.SelectSingleNode("/xml/wishing") != null) { searchReturn.wishing = doc.SelectSingleNode("/xml/wishing").InnerText; } if (doc.SelectSingleNode("/xml/remark") != null) { searchReturn.remark = doc.SelectSingleNode("/xml/remark").InnerText; } if (doc.SelectSingleNode("/xml/act_name") != null) { searchReturn.act_name = doc.SelectSingleNode("/xml/act_name").InnerText; } } else { if (doc.SelectSingleNode("/xml/err_code") != null) { searchReturn.err_code = doc.SelectSingleNode("/xml/err_code").InnerText; } if (doc.SelectSingleNode("/xml/err_code_des") != null) { searchReturn.err_code_des = doc.SelectSingleNode("/xml/err_code_des").InnerText; } } } return(searchReturn); }
/* * 错误码 描述 解决方案 * NO_AUTH 发放失败,此请求可能存在风险,已被微信拦截 请提醒用户检查自身帐号是否异常。使用常用的活跃的微信号可避免这种情况。 * SENDNUM_LIMIT 该用户今日领取红包个数超过限制 如有需要、请在微信支付商户平台【api安全】中重新配置 【每日同一用户领取本商户红包不允许超过的个数】。 * CA_ERROR 请求未携带证书,或请求携带的证书出错 到商户平台下载证书,请求带上证书后重试。 * ILLEGAL_APPID 错误传入了app的appid 接口传入的所有appid应该为公众号的appid(在mp.weixin.qq.com申请的),不能为APP的appid(在open.weixin.qq.com申请的)。 * SIGN_ERROR 商户签名错误 按文档要求重新生成签名后再重试。 * FREQ_LIMIT 受频率限制 请对请求做频率控制 * XML_ERROR 请求的xml格式错误,或者post的数据为空 检查请求串,确认无误后重试 * PARAM_ERROR 参数错误 请查看err_code_des,修改设置错误的参数 * OPENID_ERROR Openid错误 根据用户在商家公众账号上的openid,获取用户在红包公众账号上的openid 错误。请核对商户自身公众号appid和用户在此公众号下的openid。 * NOTENOUGH 余额不足 商户账号余额不足,请登录微信支付商户平台充值 * FATAL_ERROR 重复请求时,参数与原单不一致 使用相同商户单号进行重复请求时,参数与第一次请求时不一致,请检查并修改参数后再重试。 * SECOND_OVER_LIMITED 企业红包的按分钟发放受限 每分钟发送红包数量不得超过1800个;(可联系微信支付[email protected]调高额度) * DAY_ OVER_LIMITED 企业红包的按天日发放受限 单个商户日发送红包数量不大于10000个;(可联系微信支付[email protected]调高额度) * MONEY_LIMIT 红包金额发放限制 每个红包金额必须大于1元,小于1000元(可联系微信支付[email protected]调高额度至4999元) * SEND_FAILED 红包发放失败,请更换单号再重试 原商户单号已经失败,如果还要对同一个用户发放红包, 需要更换新的商户单号再试。 * SYSTEMERROR 系统繁忙,请再试。 可用同一商户单号再次调用,只会发放一个红包 * PROCESSING 请求已受理,请稍后使用原单号查询发放结果 二十分钟后查询,按照查询结果成功失败进行处理 */ #endregion /// <summary> /// 发放企业红包接口 /// </summary> /// <param name="appId">公众账号AppID</param> /// <param name="mchId">商户MchID</param> /// <param name="tenPayKey">支付密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置</param> /// <param name="tenPayCertPath">证书地址(硬盘物理地址,形如E:\\cert\\apiclient_cert.p12)</param> /// <param name="senderName">红包发送者名称,会显示给接收红包的用户</param> /// <param name="iP">发送红包的服务器地址</param> /// <param name="redPackAmount">付款金额,单位分。红包金额大于200时,请求参数scene必传。</param> /// <param name="wishingWord">祝福语</param> /// <param name="actionName">活动名称(请注意活动名称长度,官方文档提示为32个字符,实际限制不足32个字符)</param> /// <param name="remark">活动描述,用于低版本微信显示</param> /// <param name="nonceStr">将nonceStr随机字符串返回,开发者可以存到数据库用于校验</param> /// <param name="paySign">将支付签名返回,开发者可以存到数据库用于校验</param> /// <param name="mchBillNo">商户订单号,新的订单号可以从RedPackApi.GetNewBillNo(mchId)方法获得,如果传入null,则系统自动生成</param> /// <para>示例:posttime%3d123123412%26clientversion%3d234134%26mobile%3d122344545%26deviceid%3dIOS</para> /// <para>mobile:业务系统账号的手机号,国家代码-手机号。不需要+号</para> /// <para>deviceid :mac 地址或者设备唯一标识</para> /// <para>clientversion :用户操作的客户端版本</para> /// <para>把值为非空的信息用key = value进行拼接,再进行urlencode</para> /// <para>urlencode(posttime= xx & mobile = xx & deviceid = xx)</para> /// <param name="consumeMchId">资金授权商户号,服务商替特约商户发放时使用(非必填),String(32)。示例:1222000096</param> /// <returns></returns> public static NormalRedPackResult SendWorkRedPack(string appId, string mchId, string tenPayKey, string tenPayCertPath, string senderName, int redPackAmount, string wishingWord, string actionName, string remark, int agentId, out string nonceStr, out string paySign, out string WorkpaySign, string openId, string amtType, string SenderHeader, string sceneId, string mchBillNo) { mchBillNo = mchBillNo ?? GetNewBillNo(mchId); nonceStr = TenPayV3Util.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(); //设置package订单参数 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", appId); //公众账号ID packageReqHandler.SetParameter("mch_id", mchId); //商户号 packageReqHandler.SetParameter("mch_billno", mchBillNo); //填入商家订单号 packageReqHandler.SetParameter("sender_name", senderName); //红包发送者名称 packageReqHandler.SetParameter("agentid", agentId.ToString()); //发送红包的应用id packageReqHandler.SetParameter("sender_header_media_id", SenderHeader); //发送者头像 packageReqHandler.SetParameter("re_openid", openId); //用户openid packageReqHandler.SetParameter("total_amount", redPackAmount.ToString()); //付款金额,单位分 packageReqHandler.SetParameter("wishing", wishingWord); //红包祝福语 packageReqHandler.SetParameter("act_name", actionName); //活动名称 packageReqHandler.SetParameter("remark", remark); //备注信息 packageReqHandler.SetParameter("scene_id", sceneId); //场景 WorkpaySign = packageReqHandler.CreateMd5Sign("key", tenPayKey); packageReqHandler.SetParameter("workwx_sign", WorkpaySign); //企业微信签名 paySign = packageReqHandler.CreateMd5Sign("key", tenPayKey); packageReqHandler.SetParameter("sign", paySign); //签名 //最新的官方文档中将以下三个字段去除了 //packageReqHandler.SetParameter("nick_name", "提供方名称"); //提供方名称 //packageReqHandler.SetParameter("max_value", "100"); //最大红包金额,单位分 //packageReqHandler.SetParameter("min_value", "100"); //最小红包金额,单位分 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendworkwxredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = tenPayCertPath; //私钥(在安装证书时设置) string password = mchId; //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); XmlDocument doc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed(); #if NET451 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //X509Certificate cer = new X509Certificate(cert, password); #region 发起post请求 HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string response = streamReader.ReadToEnd(); #endregion doc.LoadXml(response); #else #region 发起post请求 HttpClientHandler handler = new HttpClientHandler(); handler.ClientCertificates.Add(cer); HttpClient client = new HttpClient(handler); HttpContent hc = new StringContent(data); var request = client.PostAsync(url, hc).Result; var response = request.Content.ReadAsStreamAsync().Result; #endregion doc.Load(response); #endif //XDocument xDoc = XDocument.Load(responseContent); NormalRedPackResult normalReturn = new NormalRedPackResult { err_code = "", err_code_des = "" }; if (doc.SelectSingleNode("/xml/return_code") != null) { normalReturn.return_code = doc.SelectSingleNode("/xml/return_code").InnerText; } if (doc.SelectSingleNode("/xml/return_msg") != null) { normalReturn.return_msg = doc.SelectSingleNode("/xml/return_msg").InnerText; } if (normalReturn.ReturnCodeSuccess) { //redReturn.sign = doc.SelectSingleNode("/xml/sign").InnerText; if (doc.SelectSingleNode("/xml/result_code") != null) { normalReturn.result_code = doc.SelectSingleNode("/xml/result_code").InnerText; } if (normalReturn.ResultCodeSuccess) { if (doc.SelectSingleNode("/xml/mch_billno") != null) { normalReturn.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { normalReturn.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/wxappid") != null) { normalReturn.wxappid = doc.SelectSingleNode("/xml/wxappid").InnerText; } if (doc.SelectSingleNode("/xml/re_openid") != null) { normalReturn.re_openid = doc.SelectSingleNode("/xml/re_openid").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { normalReturn.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } } else { if (doc.SelectSingleNode("/xml/err_code") != null) { normalReturn.err_code = doc.SelectSingleNode("/xml/err_code").InnerText; } if (doc.SelectSingleNode("/xml/err_code_des") != null) { normalReturn.err_code_des = doc.SelectSingleNode("/xml/err_code_des").InnerText; } if (doc.SelectSingleNode("/xml/mch_billno") != null) { normalReturn.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { normalReturn.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/wxappid") != null) { normalReturn.wxappid = doc.SelectSingleNode("/xml/wxappid").InnerText; } if (doc.SelectSingleNode("/xml/re_openid") != null) { normalReturn.re_openid = doc.SelectSingleNode("/xml/re_openid").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { normalReturn.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } if (doc.SelectSingleNode("/xml/send_listid") != null) { normalReturn.send_listid = doc.SelectSingleNode("/xml/send_listid").InnerText; } } } return(normalReturn); }
/// <summary> /// 服务商 /// </summary> /// <param name="appId"></param> /// <param name="mchId"></param> /// <param name="subappid">子商户公众账号ID</param> /// <param name="submchid">子商户号</param> /// <param name="body"></param> /// <param name="outTradeNo"></param> /// <param name="totalFee">单位:分</param> /// <param name="spbillCreateIp"></param> /// <param name="notifyUrl"></param> /// <param name="tradeType"></param> /// <param name="openid">trade_type=NATIVE时,OpenId应该为null</param> /// <param name="subOpenid">用户子标识,不需要则填写null</param> /// <param name="key"></param> /// <param name="nonceStr"></param> /// <param name="deviceInfo">自定义参数,可以为终端设备号(门店号或收银设备ID),PC网页或公众号内支付可以传"WEB",String(32)如:013467007045764</param> /// <param name="timeStart">订单生成时间,如果为空,则默认为当前服务器时间</param> /// <param name="timeExpire">订单失效时间,留空则不设置失效时间</param> /// <param name="detail">商品详细列表</param> /// <param name="attach">附加数据</param> /// <param name="feeType">符合ISO 4217标准的三位字母代码,默认人民币:CNY</param> /// <param name="goodsTag">商品标记,使用代金券或立减优惠功能时需要的参数,说明详见代金券或立减优惠。String(32),如:WXG</param> /// <param name="productId">trade_type=NATIVE时(即扫码支付),此参数必传。此参数为二维码中包含的商品ID,商户自行定义。String(32),如:12235413214070356458058</param> /// <param name="limitPay">是否限制用户不能使用信用卡支付</param> /// <param name="sceneInfo">场景信息。该字段用于上报场景信息,目前支持上报实际门店信息。</param> /// <param name="profitSharing"> /// 是否需要分账 /// https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=24_3&index=3 /// https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=26_3 /// "Y" -- 需要分账, null 或者 "N"-不需要分账, /// 服务商需要在 产品中心--特约商户授权产品 申请特约商户授权, /// 并且特约商户需要在 产品中心-我授权的商品中给服务商授权才可以使用分账功能; /// 普通商户需要 产品中心-我的产品 中开通分账功能; /// </param> /// <param name="version">统一下单接口参数,参考:https://pay.weixin.qq.com/wiki/doc/api/danpin.php?chapter=9_203&index=6</param> public TenPayV3UnifiedorderRequestData( string appId, string mchId, string subappid, string submchid, string body, string outTradeNo, int totalFee, string spbillCreateIp, string notifyUrl, TenPayV3Type tradeType, string openid, string subOpenid, string key, string nonceStr, string deviceInfo = null, DateTimeOffset?timeStart = null, DateTime?timeExpire = null, string detail = null, string attach = null, string feeType = "CNY", string goodsTag = null, string productId = null, bool limitPay = false, TenPayV3UnifiedorderRequestData_SceneInfo sceneInfo = null, string profitSharing = null, string version = null ) { AppId = appId; MchId = mchId; DeviceInfo = deviceInfo; NonceStr = nonceStr; SignType = "MD5"; Body = body ?? "Senparc TenpayV3"; Detail = detail; Attach = attach; OutTradeNo = outTradeNo; FeeType = feeType; TotalFee = totalFee; SpbillCreateIP = spbillCreateIp; TimeStart = (timeStart ?? SystemTime.Now).ToString("yyyyMMddHHmmss"); TimeExpire = timeExpire.HasValue ? timeExpire.Value.ToString("yyyyMMddHHmmss") : null; GoodsTag = goodsTag; NotifyUrl = notifyUrl; TradeType = tradeType; ProductId = productId; LimitPay = limitPay ? "no_credit" : null; OpenId = openid; Key = key; SubAppId = subappid; SubMchId = submchid; SubOpenid = subOpenid; SceneInfo = sceneInfo; ProfitSharing = profitSharing; Version = version; #region 设置RequestHandler //创建支付应答对象 PackageRequestHandler = new RequestHandler(null); //初始化 PackageRequestHandler.Init(); //设置package订单参数 //以下设置顺序按照官方文档排序,方便维护:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 PackageRequestHandler.SetParameterWhenNotNull("version", Version); PackageRequestHandler.SetParameter("appid", this.AppId); //公众账号ID PackageRequestHandler.SetParameter("mch_id", this.MchId); //商户号 PackageRequestHandler.SetParameterWhenNotNull("sub_appid", this.SubAppId); //子商户公众账号ID PackageRequestHandler.SetParameterWhenNotNull("sub_mch_id", this.SubMchId); //子商户号 PackageRequestHandler.SetParameterWhenNotNull("device_info", this.DeviceInfo); //自定义参数 PackageRequestHandler.SetParameter("nonce_str", this.NonceStr); //随机字符串 PackageRequestHandler.SetParameterWhenNotNull("sign_type", this.SignType); //签名类型,默认为MD5 PackageRequestHandler.SetParameter("body", this.Body); //商品信息 PackageRequestHandler.SetParameterWhenNotNull("detail", this.Detail); //商品详细列表 PackageRequestHandler.SetParameterWhenNotNull("attach", this.Attach); //附加数据 PackageRequestHandler.SetParameter("out_trade_no", this.OutTradeNo); //商家订单号 PackageRequestHandler.SetParameterWhenNotNull("fee_type", this.FeeType); //符合ISO 4217标准的三位字母代码,默认人民币:CNY PackageRequestHandler.SetParameter("total_fee", this.TotalFee.ToString()); //商品金额,以分为单位(money * 100).ToString() PackageRequestHandler.SetParameter("spbill_create_ip", this.SpbillCreateIP); //用户的公网ip,不是商户服务器IP PackageRequestHandler.SetParameterWhenNotNull("time_start", this.TimeStart); //订单生成时间 PackageRequestHandler.SetParameterWhenNotNull("time_expire", this.TimeExpire); //订单失效时间 PackageRequestHandler.SetParameterWhenNotNull("goods_tag", this.GoodsTag); //商品标记 PackageRequestHandler.SetParameter("notify_url", this.NotifyUrl); //接收财付通通知的URL PackageRequestHandler.SetParameter("trade_type", this.TradeType.ToString()); //交易类型 PackageRequestHandler.SetParameterWhenNotNull("product_id", this.ProductId); //trade_type=NATIVE时(即扫码支付),此参数必传。 PackageRequestHandler.SetParameterWhenNotNull("limit_pay", this.LimitPay); //上传此参数no_credit--可限制用户不能使用信用卡支付 PackageRequestHandler.SetParameterWhenNotNull("openid", this.OpenId); //用户的openId,trade_type=JSAPI时(即公众号支付),此参数必传 PackageRequestHandler.SetParameterWhenNotNull("sub_openid", this.SubOpenid); //用户子标识 PackageRequestHandler.SetParameterWhenNotNull("profit_sharing", this.ProfitSharing); //是否需要分账标识 if (SceneInfo != null) { PackageRequestHandler.SetParameter("scene_info", SceneInfo.ToString()); //场景信息 } Sign = PackageRequestHandler.CreateMd5Sign("key", this.Key); PackageRequestHandler.SetParameter("sign", Sign); //签名 #endregion }
/// <summary> /// 企业付款 /// </summary> /// <param name="appId">微信分配的公众账号ID(企业号corpid即为此appId)</param> /// <param name="mchId">商户号</param> /// <param name="nonceStr">随机字符串</param> /// <param name="deviceInfo">微信支付分配的终端设备号</param> /// <param name="partnerTradeNo">商户订单号</param> /// <param name="openId">用户openid</param> /// <param name="key"></param> /// <param name="checkName">校验用户姓名选项(NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名) </param> /// <param name="reUserName">收款用户姓名</param> /// <param name="amount">金额(单位:元,小数点后不要超过2位,否则会被四舍五入到分)</param> /// <param name="desc">企业付款描述信息</param> /// <param name="spbillCreateIP">Ip地址</param> /// <param name="wwMsgType">付款消息类型(NORMAL_MSG:普通付款消息 APPROVAL _MSG:审批付款消息)</param> /// <param name="approvalNumber">审批单号</param> /// <param name="approvalType">审批类型</param> /// <param name="actName">项目名称,最长50个utf8字符</param> /// <param name="agentId"></param> public TenPayV3PayToWorkerRequestData(string appId, string mchId, string secret, string nonceStr, string partnerTradeNo, string openId, string key, string checkName, string reUserName, decimal amount, string desc, string spbillCreateIP, string deviceInfo, string actName, string wwMsgType = null, string approvalNumber = null, string approvalType = null, uint?agentId = null, string subAppId = null, string subMchId = null) { AppId = appId; SubAppId = subAppId; MchId = mchId; SubMchId = subMchId; Secret = secret; NonceStr = nonceStr; PartnerTradeNo = partnerTradeNo; OpenId = openId; CheckName = checkName; ReUserName = reUserName; Amount = amount; Desc = desc; SpbillCreateIP = spbillCreateIP; DeviceInfo = deviceInfo; ActName = actName; WwMsgType = wwMsgType; ApprovalNumber = approvalNumber; ApprovalType = approvalType; AgentId = agentId; Key = key; #region 设置RequestHandler //创建支付应答对象 PackageRequestHandler = new RequestHandler(null); //初始化 PackageRequestHandler.Init(); //设置package订单参数 PackageRequestHandler.SetParameter("appid", this.AppId); //微信分配的公众账号ID(企业微信corpid即为此appid) //https://work.weixin.qq.com/api/doc#90000/90135/90278 PackageRequestHandler.SetParameterWhenNotNull("sub_appid", this.SubAppId); //子商户公众账号ID PackageRequestHandler.SetParameter("mch_id", this.MchId); //商户号 PackageRequestHandler.SetParameterWhenNotNull("sub_mch_id", this.SubMchId); //子商户号 PackageRequestHandler.SetParameter("nonce_str", this.NonceStr); //随机字符串 PackageRequestHandler.SetParameterWhenNotNull("device_info", this.DeviceInfo); //微信支付分配的终端设备号 PackageRequestHandler.SetParameter("partner_trade_no", this.PartnerTradeNo); //商户订单号 PackageRequestHandler.SetParameter("openid", this.OpenId); //用户openid PackageRequestHandler.SetParameter("check_name", this.CheckName); //校验用户姓名选项 PackageRequestHandler.SetParameter("re_user_name", this.ReUserName); //收款用户姓名 PackageRequestHandler.SetParameter("amount", ((int)(this.Amount * 100 + 0.5m)).ToString()); //金额 PackageRequestHandler.SetParameter("desc", this.Desc); //企业付款描述信息 PackageRequestHandler.SetParameter("spbill_create_ip", this.SpbillCreateIP); //Ip地址 PackageRequestHandler.SetParameterWhenNotNull("ww_msg_type", this.WwMsgType); //付款消息类型 PackageRequestHandler.SetParameterWhenNotNull("approval_number", this.ApprovalNumber); //审批单号 PackageRequestHandler.SetParameterWhenNotNull("approval_type", this.ApprovalType); //审批类型 PackageRequestHandler.SetParameter("act_name", this.ActName); //项目名称 PackageRequestHandler.SetParameterWhenNotNull("agentid", this.AgentId.HasValue ? this.AgentId.ToString() : null); //项目名称 //企业微信签名:https://work.weixin.qq.com/api/doc#90000/90135/90281 var workWxSign = PackageRequestHandler.CreateMd5Sign("secret", this.Secret, WorkPaySignType.WorkPayApi); PackageRequestHandler.SetParameter("workwx_sign", workWxSign); //企业微信签名 Sign = PackageRequestHandler.CreateMd5Sign("key", this.Key); PackageRequestHandler.SetParameter("sign", Sign); //签名 #endregion }
/// <summary> /// 发送小程序红包 /// </summary> /// <param name="appId">公众账号AppID</param> /// <param name="mchId">商户MchID</param> /// <param name="tenPayKey">支付密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置</param> /// <param name="tenPayCertPath">证书地址(硬盘物理地址,形如E:\\cert\\apiclient_cert.p12)</param> /// <param name="openId">要发红包的用户的OpenID</param> /// <param name="senderName">红包发送者名称,会显示给接收红包的用户</param> /// <param name="redPackAmount">付款金额,单位分。红包金额大于200时,请求参数scene必传。</param> /// <param name="wishingWord">祝福语</param> /// <param name="actionName">活动名称(请注意活动名称长度,官方文档提示为32个字符,实际限制不足32个字符)</param> /// <param name="remark">活动描述,用于低版本微信显示</param> /// <param name="nonceStr">将nonceStr随机字符串返回,开发者可以存到数据库用于校验</param> /// <param name="paySign">将支付签名返回,开发者可以存到数据库用于校验</param> /// <param name="mchBillNo">商户订单号,新的订单号可以从RedPackApi.GetNewBillNo(mchId)方法获得,如果传入null,则系统自动生成</param> /// <param name="scene">场景id(非必填),红包金额大于200时,请求参数scene必传</param> /// <param name="consumeMchId">资金授权商户号,服务商替特约商户发放时使用(非必填),String(32)。示例:1222000096</param> /// <returns></returns> public static MiniAppRedPackResult SendMiniAppRedPack(string appId, string mchId, string tenPayKey, string tenPayCertPath, string openId, string senderName, int redPackAmount, string wishingWord, string actionName, string remark, out string nonceStr, out string paySign, string mchBillNo, RedPack_Scene?scene = null, string consumeMchId = null) { mchBillNo = mchBillNo ?? GetNewBillNo(mchId); nonceStr = TenPayV3Util.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(); //设置package订单参数 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", appId); //公众账号ID packageReqHandler.SetParameter("mch_id", mchId); //商户号 packageReqHandler.SetParameter("mch_billno", mchBillNo); //填入商家订单号 packageReqHandler.SetParameter("send_name", senderName); //红包发送者名称 packageReqHandler.SetParameter("re_openid", openId); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", redPackAmount.ToString()); //付款金额,单位分 packageReqHandler.SetParameter("total_num", "1"); //红包发放总人数 packageReqHandler.SetParameter("wishing", wishingWord); //红包祝福语 packageReqHandler.SetParameter("act_name", actionName); //活动名称 packageReqHandler.SetParameter("remark", remark); //备注信息 packageReqHandler.SetParameter("notify_way", "MINI_PROGRAM_JSAPI"); //通知用户形式,通过JSAPI方式领取红包,小程序红包固定传"MINI_PROGRAM_JSAPI" if (scene.HasValue) { packageReqHandler.SetParameter("scene_id", scene.Value.ToString()); //场景id } if (consumeMchId != null) { packageReqHandler.SetParameter("consume_mch_id", consumeMchId); //活动信息 } paySign = packageReqHandler.CreateMd5Sign("key", tenPayKey); packageReqHandler.SetParameter("sign", paySign); //签名 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = Senparc.Weixin.Config.TenPayV3Host + "/mmpaymkttransfers/sendminiprogramhb"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = tenPayCertPath; //私钥(在安装证书时设置) string password = mchId; //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); XmlDocument doc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed(); #if NET462 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //X509Certificate cer = new X509Certificate(cert, password); #region 发起post请求 HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string response = streamReader.ReadToEnd(); #endregion doc.LoadXml(response); #else #region 发起post请求 HttpClientHandler handler = new HttpClientHandler(); handler.ClientCertificates.Add(cer); HttpClient client = new HttpClient(handler); HttpContent hc = new StringContent(data); var request = client.PostAsync(url, hc).Result; var response = request.Content.ReadAsStreamAsync().Result; #endregion doc.Load(response); #endif //XDocument xDoc = XDocument.Load(responseContent); MiniAppRedPackResult miniAppRedPackResult = new MiniAppRedPackResult { err_code = "", err_code_des = "" }; if (doc.SelectSingleNode("/xml/return_code") != null) { miniAppRedPackResult.return_code = doc.SelectSingleNode("/xml/return_code").InnerText; } if (doc.SelectSingleNode("/xml/return_msg") != null) { miniAppRedPackResult.return_msg = doc.SelectSingleNode("/xml/return_msg").InnerText; } if (miniAppRedPackResult.ReturnCodeSuccess) { //redReturn.sign = doc.SelectSingleNode("/xml/sign").InnerText; if (doc.SelectSingleNode("/xml/result_code") != null) { miniAppRedPackResult.result_code = doc.SelectSingleNode("/xml/result_code").InnerText; } if (miniAppRedPackResult.ResultCodeSuccess) { if (doc.SelectSingleNode("/xml/mch_billno") != null) { miniAppRedPackResult.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { miniAppRedPackResult.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/wxappid") != null) { miniAppRedPackResult.wxappid = doc.SelectSingleNode("/xml/wxappid").InnerText; } if (doc.SelectSingleNode("/xml/re_openid") != null) { miniAppRedPackResult.re_openid = doc.SelectSingleNode("/xml/re_openid").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { miniAppRedPackResult.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } //小程序红包才有 if (doc.SelectSingleNode("/xml/send_listid") != null) { miniAppRedPackResult.package = doc.SelectSingleNode("/xml/package").InnerText; } } else { if (doc.SelectSingleNode("/xml/err_code") != null) { miniAppRedPackResult.err_code = doc.SelectSingleNode("/xml/err_code").InnerText; } if (doc.SelectSingleNode("/xml/err_code_des") != null) { miniAppRedPackResult.err_code_des = doc.SelectSingleNode("/xml/err_code_des").InnerText; } if (doc.SelectSingleNode("/xml/mch_billno") != null) { miniAppRedPackResult.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { miniAppRedPackResult.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/wxappid") != null) { miniAppRedPackResult.wxappid = doc.SelectSingleNode("/xml/wxappid").InnerText; } if (doc.SelectSingleNode("/xml/re_openid") != null) { miniAppRedPackResult.re_openid = doc.SelectSingleNode("/xml/re_openid").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { miniAppRedPackResult.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } } } return(miniAppRedPackResult); }
/// <summary> /// 裂变红包发送 /// <para>裂变红包:一次可以发放一组红包。首先领取的用户为种子用户,种子用户领取一组红包当中的一个,并可以通过社交分享将剩下的红包给其他用户。裂变红包充分利用了人际传播的优势。</para> /// </summary> /// <param name="appId">公众账号AppID</param> /// <param name="mchId">商户MchID</param> /// <param name="tenPayKey">支付密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置</param> /// <param name="tenPayCertPath">证书地址(硬盘物理地址,形如E:\\cert\\apiclient_cert.p12)</param> /// <param name="openId">要发红包的用户的OpenID</param> /// <param name="senderName">红包发送者名称,会显示给接收红包的用户</param> /// <param name="iP">发送红包的服务器地址</param> /// <param name="redPackAmount">付款金额,单位分。红包金额大于200时,请求参数scene必传。</param> /// <param name="wishingWord">祝福语</param> /// <param name="actionName">活动名称(请注意活动名称长度,官方文档提示为32个字符,实际限制不足32个字符)</param> /// <param name="remark">活动描述,用于低版本微信显示</param> /// <param name="nonceStr">将nonceStr随机字符串返回,开发者可以存到数据库用于校验</param> /// <param name="paySign">将支付签名返回,开发者可以存到数据库用于校验</param> /// <param name="mchBillNo">商户订单号,新的订单号可以从RedPackApi.GetNewBillNo(mchId)方法获得,如果传入null,则系统自动生成</param> /// <param name="scene">场景id(非必填),红包金额大于200时,请求参数scene必传</param> /// <param name="riskInfo">活动信息(非必填),String(128)posttime:用户操作的时间戳。 /// <para>示例:posttime%3d123123412%26clientversion%3d234134%26mobile%3d122344545%26deviceid%3dIOS</para> /// <para>mobile:业务系统账号的手机号,国家代码-手机号。不需要+号</para> /// <para>deviceid :mac 地址或者设备唯一标识</para> /// <para>clientversion :用户操作的客户端版本</para> /// <para>把值为非空的信息用key = value进行拼接,再进行urlencode</para> /// <para>urlencode(posttime= xx & mobile = xx & deviceid = xx)</para> /// </param> /// <param name="consumeMchId">资金授权商户号,服务商替特约商户发放时使用(非必填),String(32)。示例:1222000096</param> /// <param name="amtType">红包金额设置方式,默认填写“ALL_RAND”,ALL_RAND—全部随机,商户指定总金额和红包发放总人数,由微信支付随机计算出各红包金额</param> /// <returns></returns> public static NormalRedPackResult SendNGroupRedPack(string appId, string mchId, string tenPayKey, string tenPayCertPath, string openId, string senderName, string iP, int redPackAmount, string wishingWord, string actionName, string remark, out string nonceStr, out string paySign, string mchBillNo, RedPack_Scene?scene = null, string riskInfo = null, string consumeMchId = null, string amtType = "ALL_RAND", int total_num = 3) { mchBillNo = mchBillNo ?? GetNewBillNo(mchId); nonceStr = TenPayV3Util.GetNoncestr(); //RequestHandler packageReqHandler = new RequestHandler(null); //string accessToken = AccessTokenContainer.GetAccessToken(ConstantClass.AppID); //UserInfoJson userInforResult = UserApi.Info(accessToken, openID); RequestHandler packageReqHandler = new RequestHandler(); //设置package订单参数 packageReqHandler.SetParameter("mch_billno", mchBillNo); //填入商家订单号 packageReqHandler.SetParameter("mch_id", mchId); //商户号 packageReqHandler.SetParameter("wxappid", appId); //公众账号ID packageReqHandler.SetParameter("send_name", senderName); //红包发送者名称 packageReqHandler.SetParameter("re_openid", openId); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", redPackAmount.ToString()); //付款金额,单位分 packageReqHandler.SetParameter("amt_type", amtType); //签名 packageReqHandler.SetParameter("total_num", total_num.ToString()); //红包发放总人数 packageReqHandler.SetParameter("wishing", wishingWord); //红包祝福语 packageReqHandler.SetParameter("act_name", actionName); //活动名称 packageReqHandler.SetParameter("remark", remark); //备注信息 //比普通红包多的部分 if (scene.HasValue) { packageReqHandler.SetParameter("scene_id", scene.Value.ToString()); //场景id } packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 if (riskInfo != null) { packageReqHandler.SetParameter("risk_info", riskInfo);//活动信息 } if (consumeMchId != null) { packageReqHandler.SetParameter("consume_mch_id", consumeMchId);//活动信息 } paySign = packageReqHandler.CreateMd5Sign("key", tenPayKey); packageReqHandler.SetParameter("sign", paySign); //签名 //最新的官方文档中将以下三个字段去除了 //packageReqHandler.SetParameter("nick_name", "提供方名称"); //提供方名称 //packageReqHandler.SetParameter("max_value", "100"); //最大红包金额,单位分 //packageReqHandler.SetParameter("min_value", "100"); //最小红包金额,单位分 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 //string url = Senparc.Weixin.Config.TenPayV3Host + "/mmpaymkttransfers/sendredpack"; string url = Senparc.Weixin.Config.TenPayV3Host + "/mmpaymkttransfers/sendgroupredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = tenPayCertPath; //私钥(在安装证书时设置) string password = mchId; //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); XmlDocument doc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed(); #region 发起post请求,载入到doc中 #if NET462 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //X509Certificate cer = new X509Certificate(cert, password); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string response = streamReader.ReadToEnd(); doc.LoadXml(response); #else HttpClientHandler handler = new HttpClientHandler(); handler.ClientCertificates.Add(cer); HttpClient client = new HttpClient(handler); HttpContent hc = new StringContent(data); var request = client.PostAsync(url, hc).Result; var response = request.Content.ReadAsStreamAsync().Result; doc.Load(response); #endif #endregion //XDocument xDoc = XDocument.Load(responseContent); //if (xDoc==null) //{ // throw new WeixinException("微信支付XML响应格式错误"); //} NormalRedPackResult normalReturn = new NormalRedPackResult { err_code = "", err_code_des = "" }; if (doc.SelectSingleNode("/xml/return_code") != null) { normalReturn.return_code = doc.SelectSingleNode("/xml/return_code").InnerText; } if (doc.SelectSingleNode("/xml/return_msg") != null) { normalReturn.return_msg = doc.SelectSingleNode("/xml/return_msg").InnerText; } if (normalReturn.ReturnCodeSuccess) { //redReturn.sign = doc.SelectSingleNode("/xml/sign").InnerText; if (doc.SelectSingleNode("/xml/result_code") != null) { normalReturn.result_code = doc.SelectSingleNode("/xml/result_code").InnerText; } if (normalReturn.ResultCodeSuccess) { if (doc.SelectSingleNode("/xml/mch_billno") != null) { normalReturn.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { normalReturn.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/wxappid") != null) { normalReturn.wxappid = doc.SelectSingleNode("/xml/wxappid").InnerText; } if (doc.SelectSingleNode("/xml/re_openid") != null) { normalReturn.re_openid = doc.SelectSingleNode("/xml/re_openid").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { normalReturn.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } //裂变红包才有 if (doc.SelectSingleNode("/xml/send_time") != null) { normalReturn.send_time = doc.SelectSingleNode("/xml/send_time").InnerText; } //裂变红包才有 if (doc.SelectSingleNode("/xml/send_listid") != null) { normalReturn.send_listid = doc.SelectSingleNode("/xml/send_listid").InnerText; } } else { if (doc.SelectSingleNode("/xml/err_code") != null) { normalReturn.err_code = doc.SelectSingleNode("/xml/err_code").InnerText; } if (doc.SelectSingleNode("/xml/err_code_des") != null) { normalReturn.err_code_des = doc.SelectSingleNode("/xml/err_code_des").InnerText; } if (doc.SelectSingleNode("/xml/mch_billno") != null) { normalReturn.mch_billno = doc.SelectSingleNode("/xml/mch_billno").InnerText; } if (doc.SelectSingleNode("/xml/mch_id") != null) { normalReturn.mch_id = doc.SelectSingleNode("/xml/mch_id").InnerText; } if (doc.SelectSingleNode("/xml/wxappid") != null) { normalReturn.wxappid = doc.SelectSingleNode("/xml/wxappid").InnerText; } if (doc.SelectSingleNode("/xml/re_openid") != null) { normalReturn.re_openid = doc.SelectSingleNode("/xml/re_openid").InnerText; } if (doc.SelectSingleNode("/xml/total_amount") != null) { normalReturn.total_amount = doc.SelectSingleNode("/xml/total_amount").InnerText; } } } return(normalReturn); }