/// <summary> /// 提交被扫支付API /// http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=5_5 /// 1.应用场景:收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,由商户收银台或者商户后台调用该接口发起支付。 ///是否需要证书:不需要。 /// <param name="appid">(必填) String(32) 微信分配的公众账号ID</param> /// <param name="mch_id">(必填) String(32) 微信支付分配的商户号</param> /// <param name="device_info"> String(32) 微信支付分配的终端设备号,商户自定义</param> /// <param name="nonce_str">(必填) String(32) 随机字符串,不长于32位</param> /// <param name="body">(必填) String(32) 商品描述 商品或支付单简要描</param> /// <param name="detail"> String(8192) 商品详情 商品名称明细列表</param> /// <param name="attach"> String(127) 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据</param> /// <param name="out_trade_no">(必填) String(32) 商家订单ID,32个字符内、可包含字母, 其他说明见第4.2节商户订单号:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_2 </param> /// <param name="fee_type">符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见第4.2节货币类型: http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_2 </param> /// <param name="total_fee">(必填) Int 订单总金额,只能为整数,详见第4.2节支付金额:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_2 </param> /// <param name="spbill_create_ip">(必填) String(32)终端IP APP和网页支付提交用户端IP,Native支付填调用微信支付API的机器IP。</param> /// <param name="time_start">String(14) 订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。</param> /// <param name="time_expire">String(14) 订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。</param> /// <param name="goods_tag">String(32) 商品标记,代金券或立减优惠功能的参数,说明详见第10节代金券或立减优惠:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=10_1 </param> /// <param name="auth_code">String(128) 授权码 扫码支付授权码,设备读取用户微信中的条码或者二维码信息 </param> /// <param name="partnerKey">API密钥</param> /// <returns>返回json字符串,格式参见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=5_5 </returns> public static dynamic Submit(string appid, string mch_id, string device_info, string nonce_str, string body, string detail, string attach, string out_trade_no, string fee_type, int total_fee, string spbill_create_ip, string time_start, string time_expire, string goods_tag, string auth_code, string partnerKey) { var stringADict = new Dictionary <string, string>(); stringADict.Add("appid", appid); stringADict.Add("mch_id", mch_id); stringADict.Add("device_info", device_info); stringADict.Add("nonce_str", nonce_str); stringADict.Add("body", body); stringADict.Add("attach", attach); stringADict.Add("out_trade_no", out_trade_no); stringADict.Add("fee_type", fee_type); stringADict.Add("total_fee", total_fee.ToString()); stringADict.Add("spbill_create_ip", spbill_create_ip); stringADict.Add("time_start", time_start); stringADict.Add("time_expire", time_expire); stringADict.Add("goods_tag", goods_tag); stringADict.Add("auth_code", auth_code); var sign = WxPayAPI.Sign(stringADict, partnerKey);//生成签名字符串 var postdata = PayUtil.GeneralPostdata(stringADict, sign); var url = "https://api.mch.weixin.qq.com/pay/micropay"; var client = new HttpClient(); var result = client.PostAsync(url, new StringContent(postdata)).Result; if (!result.IsSuccessStatusCode) { return(string.Empty); } return(new DynamicXml(result.Content.ReadAsStringAsync().Result)); }
/// <summary> /// 提交被扫支付API /// http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=6_4 /// 应用场景: 用户扫描商户展示在各种场景的二维码进行支付。 /// <param name="appid">(必填) String(32) 微信分配的公众账号ID</param> /// <param name="mch_id">(必填) String(32) 微信支付分配的商户号</param> /// <param name="time_stamp"> String(32) 微信支付分配的终端设备号,商户自定义</param> /// <param name="nonce_str">(必填) String(32) 随机字符串,不长于32位</param> /// <param name="product_id">(必填) String(32) 商品描述 商品或支付单简要描</param> /// <param name="partnerKey">API密钥</param> /// <returns>返回支付链接,开发者需要自己生成相应的二维码展示给用户 </returns> public static string Mode1(string appid, string mch_id, string time_stamp, string nonce_str, string product_id, string partnerKey) { var stringADict = new Dictionary <string, string>(); stringADict.Add("appid", appid); stringADict.Add("mch_id", mch_id); stringADict.Add("time_stamp", time_stamp); stringADict.Add("nonce_str", nonce_str); stringADict.Add("product_id", product_id); var sign = WxPayAPI.Sign(stringADict, partnerKey);//生成签名字符串 return(string.Format("weixin://wxpay/bizpayurl?sign={0}&appid={1}&mch_id={2}&product_id={3}&time_stamp={4}&nonce_str={5}" , sign, appid, mch_id, product_id, time_stamp, nonce_str )); }
/// <summary> /// 撤销支付API /// http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=5_6 /// 应用场景:支付交易返回失败或支付系统超时,调用该接口撤销交易。如果此订单用户支付失败,微信支付系统会将此订单关闭;如果用户支付成功,微信支付系统会将此订单资金退还给用户。 /// 注意:7天以内的交易单可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。 /// 请求需要双向证书 /// </summary> /// <param name="appid">(必填) String(32) 微信分配的公众账号ID</param> /// <param name="mch_id">(必填) String(32) 微信支付分配的商户号</param> /// <param name="transaction_id"> String(32) 微信订单号 优先使用</param> /// <param name="out_trade_no">String(32) 商户订单号 </param> /// <param name="nonce_str">(必填) String(32) 随机字符串,不长于32位</param> /// <param name="partnerKey">API密钥</param> /// <returns></returns> public static dynamic ReverseSubmit(string appid, string mch_id, string transaction_id, string out_trade_no, string nonce_str, string partnerKey) { var stringADict = new Dictionary <string, string>(); stringADict.Add("appid", appid); stringADict.Add("mch_id", mch_id); stringADict.Add("transaction_id", transaction_id); stringADict.Add("out_trade_no", out_trade_no); stringADict.Add("nonce_str", nonce_str); var sign = WxPayAPI.Sign(stringADict, partnerKey);//生成签名字符串 var postdata = PayUtil.GeneralPostdata(stringADict, sign); var url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; var client = new HttpClient(); var result = client.PostAsync(url, new StringContent(postdata)).Result; if (!result.IsSuccessStatusCode) { return(string.Empty); } return(new DynamicXml(result.Content.ReadAsStringAsync().Result)); }
/// <summary> /// 获取微信现金红包信息接口 /// 是否需要证书:需要。 /// http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_6 /// 使用说明 /// 用于商户对已发放的红包进行查询红包的具体信息,可支持普通红包和裂变包。 /// </summary> /// <param name="nonce_str">(必填) String(32) 随机字符串,不长于32位</param> /// <param name="mch_billno">(必填) String(28) 商户发放红包的商户订单号</param> /// <param name="mch_id">(必填) String(32) 微信支付分配的商户号</param> /// <param name="appid">(必填) String(32) 微信分配的公众账号ID</param> /// <param name="bill_type">(必填) String(32) 订单类型 例子:MCHT ,通过商户订单号获取红包信息。</param> /// <param name="partnerKey">API密钥</param> /// <param name="cert_path">秘钥路径</param> /// <param name="cert_password">秘钥密码</param> /// <returns>返回xml字符串,格式参见:http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_6 </returns> public static string GetHbInfo(string nonce_str, string mch_billno, string mch_id, string appid, string bill_type, string partnerKey, string cert_path, string cert_password) { try { var stringADict = new Dictionary <string, string>(); stringADict.Add("nonce_str", nonce_str); stringADict.Add("mch_billno", mch_billno); stringADict.Add("mch_id", mch_id); stringADict.Add("appid", appid); stringADict.Add("bill_type", bill_type); var sign = WxPayAPI.Sign(stringADict, partnerKey);//生成签名字符串 var postdata = PayUtil.GeneralPostdata(stringADict, sign); var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo"; // 要注意的这是这个编码方式,还有内容的Xml内容的编码方式 Encoding encoding = Encoding.GetEncoding("UTF-8"); byte[] data = encoding.GetBytes(postdata); //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //X509Certificate cer = new X509Certificate(cert_path, cert_password); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cer = new X509Certificate(cert_path, cert_password); #region 该部分是关键,若没有该部分则在IIS下会报 CA证书出错 X509Certificate2 certificate = new X509Certificate2(cert_path, cert_password); X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); store.Remove(certificate); //可省略 store.Add(certificate); store.Close(); #endregion HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; webrequest.ContentLength = data.Length; Stream outstream = webrequest.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Flush(); outstream.Close(); HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse(); Stream instream = webreponse.GetResponseStream(); string resp = string.Empty; using (StreamReader reader = new StreamReader(instream)) { resp = reader.ReadToEnd(); } return(resp); } catch (Exception ex) { throw ex; } }
/// <summary> /// http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5 /// 1.应用场景:向指定微信用户的openid发放指定金额红包 ///是否需要证书:需要。 ///<param name="nonce_str">(必填) String(32) 随机字符串,不长于32位</param> ///<param name="mch_billno">(必填) String(32) 商户订单号(每个订单号必须唯一)组成: mch_id+yyyymmdd+10位一天内不能重复的数字。</param> ///<param name="mch_id">(必填) String(32) 微信支付分配的商户号</param> ///<param name="sub_mch_id"> String(32) 微信支付分配的子商户号,受理模式下必填</param> /// <param name="wxappid">(必填) String(32) 商户appid</param> /// <param name="nick_name">(必填) String(32) 提供方名称</param> /// <param name="send_name">(必填) String(32) 红包发送者名称</param> /// <param name="re_openid">(必填) String(32) 接受收红包的用户 用户在wxappid下的openid</param> /// <param name="total_amount">(必填) int 付款金额,单位分</param> /// <param name="min_value">(必填) int 最小红包金额,单位分</param> /// <param name="max_value">(必填) int 最大红包金额,单位分</param> /// <param name="total_num">(必填) int 红包发放总人数</param> /// <param name="wishing">(必填) String(128) 红包祝福语</param> /// <param name="client_ip">(必填) String(15) 调用接口的机器Ip地址</param> /// <param name="act_name">(必填) String(32) 活动名称</param> /// <param name="remark">(必填) String(256) 备注信息</param> /// <param name="logo_imgurl"> String(128) 商户logo的url</param> /// <param name="share_content"> String(256) 分享文案</param> /// <param name="share_url"> String(128) 分享链接</param> /// <param name="share_imgurl"> String(128) 分享的图片url</param> /// <param name="partnerKey">API密钥</param> /// <param name="cert_path">秘钥路径</param> /// <param name="cert_password">秘钥密码</param> /// <returns>返回xml字符串,格式参见:http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5 </returns> public static string SendRedPack(string nonce_str, string mch_billno, string mch_id, string sub_mch_id, string wxappid, string nick_name, string send_name, string re_openid, int total_amount, int min_value, int max_value, int total_num, string wishing, string client_ip, string act_name, string remark, string logo_imgurl, string share_content, string share_url, string share_imgurl, string partnerKey, string cert_path, string cert_password) { try { var stringADict = new Dictionary <string, string>(); stringADict.Add("nonce_str", nonce_str); stringADict.Add("mch_billno", mch_billno); stringADict.Add("mch_id", mch_id); stringADict.Add("sub_mch_id", sub_mch_id); stringADict.Add("wxappid", wxappid); stringADict.Add("nick_name", nick_name); stringADict.Add("send_name", send_name); stringADict.Add("re_openid", re_openid); stringADict.Add("total_amount", total_amount.ToString()); stringADict.Add("min_value", min_value.ToString()); stringADict.Add("max_value", max_value.ToString()); stringADict.Add("total_num", total_num.ToString()); stringADict.Add("wishing", wishing.ToString()); stringADict.Add("client_ip", client_ip); stringADict.Add("act_name", act_name); stringADict.Add("remark", remark); stringADict.Add("logo_imgurl", logo_imgurl); stringADict.Add("share_content", share_content); stringADict.Add("share_url", share_url); stringADict.Add("share_imgurl", share_imgurl); var sign = WxPayAPI.Sign(stringADict, partnerKey);//生成签名字符串 var postdata = PayUtil.GeneralPostdata(stringADict, sign); var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; // 要注意的这是这个编码方式,还有内容的Xml内容的编码方式 Encoding encoding = Encoding.GetEncoding("UTF-8"); byte[] data = encoding.GetBytes(postdata); //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //X509Certificate cer = new X509Certificate(cert_path, cert_password); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cer = new X509Certificate(cert_path, cert_password); #region 该部分是关键,若没有该部分则在IIS下会报 CA证书出错 X509Certificate2 certificate = new X509Certificate2(cert_path, cert_password); X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); store.Remove(certificate); //可省略 store.Add(certificate); store.Close(); #endregion HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; webrequest.ContentLength = data.Length; Stream outstream = webrequest.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Flush(); outstream.Close(); HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse(); Stream instream = webreponse.GetResponseStream(); string resp = string.Empty; using (StreamReader reader = new StreamReader(instream)) { resp = reader.ReadToEnd(); } return(resp); } catch (Exception ex) { throw ex; } }