一、微信小程序支付

1、微信小程序端请求支付接口

商户在小程序中先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易后调起支付。具体可以查看接口示例

接口传入参数示例:

  1. <xml>
  2. <appid>wx2421b1c4370ec43b</appid>
  3. <attach>支付测试</attach>
  4. <body>JSAPI支付测试</body>
  5. <mch_id>10000100</mch_id>
  6. <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
  7. <notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
  8. <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
  9. <out_trade_no>1415659990</out_trade_no>
  10. <spbill_create_ip>14.23.150.211</spbill_create_ip>
  11. <total_fee>1</total_fee>
  12. <trade_type>JSAPI</trade_type>
  13. <sign>0CB01533B8C1EF103065174F50BCA001</sign>
  14. </xml>

接口返回示例:

  1. <xml>
  2. <return_code><![CDATA[SUCCESS]]></return_code>
  3. <return_msg><![CDATA[OK]]></return_msg>
  4. <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
  5. <mch_id><![CDATA[]]></mch_id>
  6. <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
  7. <openid><![CDATA[oUpF8uMuAJO_M2pxb1Q9zNjWeS6o]]></openid>
  8. <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
  9. <result_code><![CDATA[SUCCESS]]></result_code>
  10. <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
  11. <trade_type><![CDATA[JSAPI]]></trade_type>
  12. </xml>

二、接口调用(C#)

1、支付接口方法

  1. //获取请求数据
  2. Dictionary<string, string> strParam = new Dictionary<string, string>();
  3. //小程序ID
  4. strParam.Add("appid", PayInfo.appid);
  5. //附加数据
  6. strParam.Add("attach", PayInfo.attach);
  7. //商品描述
  8. strParam.Add("body", PayInfo.body);
  9. //商户号
  10. strParam.Add("mch_id", PayInfo.mchid);
  11. //随机字符串
  12. strParam.Add("nonce_str", PayInfo.nonceStr);
  13. //通知地址 (异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。)
  14. strParam.Add("notify_url", PayInfo.notifyUrl);
  15. //用户标识
  16. strParam.Add("openid", openId);
  17. //商户订单号
  18. strParam.Add("out_trade_no", orderNum);
  19. //终端IP
  20. strParam.Add("spbill_create_ip", PayInfo.addrIp);
  21. //标价金额
  22. strParam.Add("total_fee", Convert.ToInt32(orderTotal * ).ToString());
  23. //交易类型
  24. strParam.Add("trade_type", PayInfo.tradeType);
  25. strParam.Add("sign", PayHelper.GetSignInfo(strParam, PayInfo.key));
  26. //获取预支付ID
  27. string preInfo = Utility.PostHttpResponse(PayInfo.orderUrl, PayHelper.CreateXmlParam(strParam));
  28. string strCode = PayHelper.GetXmlValue(preInfo, "return_code");
  29. string strMsg = PayHelper.GetXmlValue(preInfo, "return_msg");
  30. if (strCode == "SUCCESS")
  31. {
  32. //再次签名
  33. string nonecStr = PayInfo.nonceStr;
  34. string timeStamp = PayInfo.timeStamp;
  35. string package = "prepay_id=" + PayHelper.GetXmlValue(preInfo, "prepay_id");
  36. Dictionary<string, string> singInfo = new Dictionary<string, string>();
  37. singInfo.Add("appId", PayInfo.appid);
  38. singInfo.Add("nonceStr", nonecStr);
  39. singInfo.Add("package", package);
  40. singInfo.Add("signType", PayInfo.signType);
  41. singInfo.Add("timeStamp", timeStamp);
  42. //返回参数
  43. info.Msg = strMsg;
  44. info.Code = strCode;
  45. info.Id = orderId;
  46. info.appId = PayInfo.appid;
  47. info.orderGuId = orderNum;
  48. info.Package = package;
  49. info.TimeStamp = timeStamp;
  50. info.NonceStr = nonecStr;
  51. info.SignType = PayInfo.signType;
  52. info.PaySign = PayHelper.GetSignInfo(singInfo, PayInfo.key);
  53. listMsg.Add(info);
  54. result = u.SuccessMsg(listMsg, "", "");
  55. }
  56. else
  57. {
  58. info.Code = strCode;
  59. info.Msg = strMsg;
  60. listMsg.Add(info);
  61. result = u.FailMsg(listMsg);
  62. }

2、支付结果通知API

  1. /// <summary>
  2. /// 支付结果通知API
  3. /// </summary>
  4. /// <returns></returns>
  5. [HttpPost]
  6. public string OrderNotify()
  7. {
  8. string strResult = string.Empty;
  9. try
  10. {
  11. //1.获取微信通知的参数
  12. string strXML = Utility.GetPostStr();
  13. //判断是否请求成功
  14. if (PayHelper.GetXmlValue(strXML, "return_code") == "SUCCESS")
  15. {
  16. //判断是否支付成功
  17. if (PayHelper.GetXmlValue(strXML, "result_code") == "SUCCESS")
  18. {
  19. //获得签名
  20. string getSign = PayHelper.GetXmlValue(strXML, "sign");
  21. //进行签名
  22. string sign = PayHelper.GetSignInfo(PayHelper.GetFromXml(strXML), PayInfo.key);
  23. if (sign == getSign)
  24. {
  25. //校验订单信息
  26. string wxOrderNum = PayHelper.GetXmlValue(strXML, "transaction_id"); //微信订单号
  27. string orderNum = PayHelper.GetXmlValue(strXML, "out_trade_no"); //商户订单号
  28. string orderTotal = PayHelper.GetXmlValue(strXML, "total_fee");
  29. string openid = PayHelper.GetXmlValue(strXML, "openid");
  30. //校验订单是否存在
  31. if (true)
  32. {
  33. //2.更新订单的相关状态
  34.  
  35. //3.返回一个xml格式的结果给微信服务器
  36. if (obj > )
  37. {
  38. strResult = PayHelper.GetReturnXml("SUCCESS", "OK");
  39. }
  40. else
  41. {
  42. strResult = PayHelper.GetReturnXml("FAIL", "订单状态更新失败");
  43. }
  44. }
  45. else
  46. {
  47. strResult = PayHelper.GetReturnXml("FAIL", "支付结果中微信订单号数据库不存在!");
  48. }
  49. }
  50. else
  51. {
  52. strResult = PayHelper.GetReturnXml("FAIL", "签名不一致!");
  53. }
  54. }
  55. else
  56. {
  57. strResult = PayHelper.GetReturnXml("FAIL", "支付通知失败!");
  58. }
  59. }
  60. else
  61. {
  62. strResult = PayHelper.GetReturnXml("FAIL", "支付通知失败!");
  63. }
  64.  
  65. #endregion
  66. }
  67. catch (Exception ex)
  68. {
  69.  
  70. }
  71. return strResult;
  72. }

3、Utility类

  1. /// <summary>
  2. /// 获得Post过来的数据
  3. /// </summary>
  4. /// <returns></returns>
  5. public static string GetPostStr()
  6. {
  7. Int32 intLen = Convert.ToInt32(HttpContext.Current.Request.InputStream.Length);
  8. byte[] b = new byte[intLen];
  9. HttpContext.Current.Request.InputStream.Read(b, , intLen);
  10. return Encoding.UTF8.GetString(b);
  11. }
  12.  
  13. /// <summary>
  14. /// 模拟POST提交
  15. /// </summary>
  16. /// <param name="url">请求地址</param>
  17. /// <param name="xmlParam">xml参数</param>
  18. /// <returns>返回结果</returns>
  19. public static string PostHttpResponse(string url, string xmlParam)
  20. {
  21. HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  22. myHttpWebRequest.Method = "POST";
  23. myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
  24.  
  25. // Encode the data
  26. byte[] encodedBytes = Encoding.UTF8.GetBytes(xmlParam);
  27. myHttpWebRequest.ContentLength = encodedBytes.Length;
  28.  
  29. // Write encoded data into request stream
  30. Stream requestStream = myHttpWebRequest.GetRequestStream();
  31. requestStream.Write(encodedBytes, , encodedBytes.Length);
  32. requestStream.Close();
  33.  
  34. HttpWebResponse result;
  35.  
  36. try
  37. {
  38. result = (HttpWebResponse)myHttpWebRequest.GetResponse();
  39. }
  40. catch
  41. {
  42. return string.Empty;
  43. }
  44.  
  45. if (result.StatusCode == HttpStatusCode.OK)
  46. {
  47. using (Stream mystream = result.GetResponseStream())
  48. {
  49. using (StreamReader reader = new StreamReader(mystream))
  50. {
  51. return reader.ReadToEnd();
  52. }
  53. }
  54. }
  55. return null;
  56. }

4、PayHelper类

  1. #region 生成签名
  2. /// <summary>
  3. /// 获取签名数据
  4. ///</summary>
  5. /// <param name="strParam"></param>
  6. /// <param name="key"></param>
  7. /// <returns></returns>
  8. public static string GetSignInfo(Dictionary<string, string> strParam, string key)
  9. {
  10. int i = ;
  11. string sign = string.Empty;
  12. StringBuilder sb = new StringBuilder();
  13. try
  14. {
  15. foreach (KeyValuePair<string, string> temp in strParam)
  16. {
  17. if (temp.Value == "" || temp.Value == null || temp.Key.ToLower() == "sign")
  18. {
  19. continue;
  20. }
  21. i++;
  22. sb.Append(temp.Key.Trim() + "=" + temp.Value.Trim() + "&");
  23. }
  24. sb.Append("key=" + key.Trim() + "");
  25. sign = MD5Core.GetHashString(sb.ToString(), Encoding.UTF8).ToUpper();
  26. }
  27. catch (Exception ex)
  28. {
  29. Utility.AddLog("PayHelper", "GetSignInfo", ex.Message, ex);
  30. }
  31. return sign;
  32. }
  33. #endregion
  34.  
  35. #region XML 处理
  36. /// <summary>
  37. /// 获取XML值
  38. /// </summary>
  39. /// <param name="strXml">XML字符串</param>
  40. /// <param name="strData">字段值</param>
  41. /// <returns></returns>
  42. public static string GetXmlValue(string strXml, string strData)
  43. {
  44. string xmlValue = string.Empty;
  45. XmlDocument xmlDocument = new XmlDocument();
  46. xmlDocument.LoadXml(strXml);
  47. var selectSingleNode = xmlDocument.DocumentElement.SelectSingleNode(strData);
  48. if (selectSingleNode != null)
  49. {
  50. xmlValue = selectSingleNode.InnerText;
  51. }
  52. return xmlValue;
  53. }
  54.  
  55. /// <summary>
  56. /// 集合转换XML数据 (拼接成XML请求数据)
  57. /// </summary>
  58. /// <param name="strParam">参数</param>
  59. /// <returns></returns>
  60. public static string CreateXmlParam(Dictionary<string, string> strParam)
  61. {
  62. StringBuilder sb = new StringBuilder();
  63. try
  64. {
  65. sb.Append("<xml>");
  66. foreach (KeyValuePair<string, string> k in strParam)
  67. {
  68. if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
  69. {
  70. sb.Append("<" + k.Key + "><![CDATA[" + k.Value + "]]></" + k.Key + ">");
  71. }
  72. else
  73. {
  74. sb.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">");
  75. }
  76. }
  77. sb.Append("</xml>");
  78. }
  79. catch (Exception ex)
  80. {
  81. Utility.AddLog("PayHelper", "CreateXmlParam", ex.Message, ex);
  82. }
  83.  
  84. return sb.ToString();
  85. }
  86.  
  87. /// <summary>
  88. /// XML数据转换集合(XML数据拼接成字符串)
  89. /// </summary>
  90. /// <param name="xmlString"></param>
  91. /// <returns></returns>
  92. public static Dictionary<string, string> GetFromXml(string xmlString)
  93. {
  94. Dictionary<string, string> sParams = new Dictionary<string, string>();
  95. try
  96. {
  97. XmlDocument doc = new XmlDocument();
  98. doc.LoadXml(xmlString);
  99. XmlElement root = doc.DocumentElement;
  100. int len = root.ChildNodes.Count;
  101. for (int i = ; i < len; i++)
  102. {
  103. string name = root.ChildNodes[i].Name;
  104. if (!sParams.ContainsKey(name))
  105. {
  106. sParams.Add(name.Trim(), root.ChildNodes[i].InnerText.Trim());
  107. }
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. Utility.AddLog("PayHelper", "GetFromXml", ex.Message, ex);
  113. }
  114. return sParams;
  115. }
  116.  
  117. /// <summary>
  118. /// 返回通知 XML
  119. /// </summary>
  120. /// <param name="returnCode"></param>
  121. /// <param name="returnMsg"></param>
  122. /// <returns></returns>
  123. public static string GetReturnXml(string returnCode, string returnMsg)
  124. {
  125. StringBuilder sb = new StringBuilder();
  126. sb.Append("<xml>");
  127. sb.Append("<return_code><![CDATA[" + returnCode + "]]></return_code>");
  128. sb.Append("<return_msg><![CDATA[" + returnMsg + "]]></return_msg>");
  129. sb.Append("</xml>");
  130. return sb.ToString();
  131. }
  132. #endregion

5、PayInfo类

  1. public class PayInfo
  2. {
  3. /// <summary>
  4. /// 小程序登录API
  5. /// </summary>
  6. public static string loginUrl = ConfigurationManager.AppSettings["loginurl"].ToString();
  7.  
  8. /// <summary>
  9. /// 统一下单API
  10. /// </summary>
  11. public static string orderUrl = ConfigurationManager.AppSettings["orderurl"].ToString();
  12.  
  13. /// <summary>
  14. /// 支付结果通知API
  15. /// </summary>
  16. public static string notifyUrl = ConfigurationManager.AppSettings["notifyurl"].ToString();
  17.  
  18. /// <summary>
  19. /// 查询订单API
  20. /// </summary>
  21. public static string queryUrl = ConfigurationManager.AppSettings["queryurl"].ToString();
  22.  
  23. /// <summary>
  24. /// 申请退款API
  25. /// </summary>
  26. public static string refundUrl = ConfigurationManager.AppSettings["refundurl"].ToString();
  27.  
  28. /// <summary>
  29. /// 退款通知API
  30. /// </summary>
  31. public static string refundNotifyUrl = ConfigurationManager.AppSettings["refundnotifyurl"].ToString();
  32.  
  33. /// <summary>
  34. /// 退款通知API
  35. /// </summary>
  36. public static string refundQueryUrl = ConfigurationManager.AppSettings["refundqueryurl"].ToString();
  37.  
  38. /// <summary>
  39. /// 小程序唯一标识
  40. /// </summary>
  41. public static string appid = ConfigurationManager.AppSettings["appid"].ToString();
  42.  
  43. /// <summary>
  44. /// 小程序的 app secret
  45. /// </summary>
  46. public static string secret = ConfigurationManager.AppSettings["secret"].ToString();
  47.  
  48. /// <summary>
  49. /// 小程序的授权类型
  50. /// </summary>
  51. public static string grantType = ConfigurationManager.AppSettings["grant_type"].ToString();
  52.  
  53. /// <summary>
  54. /// 商户号(微信支付分配的商户号)
  55. /// </summary>
  56. public static string mchid = ConfigurationManager.AppSettings["mch_id"].ToString();
  57.  
  58. /// <summary>
  59. ///商户平台设置的密钥key
  60. /// </summary>
  61. public static string key = ConfigurationManager.AppSettings["key"].ToString();
  62.  
  63. /// <summary>
  64. /// 随机字符串不长于 32 位
  65. /// </summary>
  66. public static string nonceStr = PayHelper.GetNonceStr();
  67.  
  68. /// <summary>
  69. /// 时间戳 从1970年1月1日00:00:00至今的秒数,即当前的时间
  70. /// </summary>
  71. public static string timeStamp = PayHelper.GetTimeStamp();
  72.  
  73. /// <summary>
  74. /// 终端IP APP和网页支付提交用户端IP,
  75. /// </summary>
  76. public static string addrIp = PayHelper.GetIP;
  77.  
  78. /// <summary>
  79. /// 交易类型 小程序取值如下:JSAPI
  80. /// </summary>
  81. public static string tradeType = "JSAPI";
  82.  
  83. /// <summary>
  84. /// 签名类型 默认为MD5,支持HMAC-SHA256和MD5。
  85. /// </summary>
  86. public static string signType = "MD5";
  87.  
  88. /// <summary>
  89. /// 商品描述 商品简单描述,该字段请按照规范传递
  90. /// </summary>
  91. public static string body = "奇小爱的异想世界-支付";
  92.  
  93. /// <summary>
  94. /// 附加数据 在查询API和支付通知中原样返回
  95. /// </summary>
  96. public static string attach = "微信支付信息";
  97.  
  98. /// <summary>
  99. /// 签名,参与签名参数:appid,mch_id,transaction_id,out_trade_no,nonce_str,key
  100. /// </summary>
  101. public string sign = "";
  102.  
  103. /// <summary>
  104. /// 微信订单号,优先使用
  105. /// </summary>
  106. public static string transactionid = "";
  107.  
  108. /// <summary>
  109. /// 商户系统内部订单号
  110. /// </summary>
  111. public static string out_trade_no = "";
  112.  
  113. /// <summary>
  114. /// 商户退款单号
  115. /// </summary>
  116. public static string out_refund_no = "";
  117.  
  118. /// <summary>
  119. /// 退款金额
  120. /// </summary>
  121. public static decimal refundfee;
  122.  
  123. /// <summary>
  124. /// 订单金额
  125. /// </summary>
  126. public static decimal totalfee;
  127. }

三、微信小程序调用

1、小程序调用方法,发起支付,具体如下:

微信小程序支付方发起微信支付

2、示例代码:

  1. wx.requestPayment({
  2. 'timeStamp': '',
  3. 'nonceStr': '',
  4. 'package': '',
  5. 'signType': 'MD5',
  6. 'paySign': '',
  7. 'success':function(res){
  8. },
  9. 'fail':function(res){
  10. }
  11. })

3、测试完成支付

优秀是一种习惯,欢迎大家关注学习 

微信小程序之微信支付C#后台(统一下单)的更多相关文章

  1. 微信小程序腾讯云php后台解决方案

    微信小程序腾讯云php后台解决方案 微信小程序前段需要添加必要的文件以配合后端 (1)wafer2-client-sdk sdk提供了几种接口包括登陆,获取用户openid,图片上传等 (2)conf ...

  2. 微信小程序之微信登陆 —— 微信小程序教程系列(20)

    简介: 微信登陆,在新建一个微信小程序Hello World项目的时候,就可以看到项目中出现了我们的微信头像,其实这个Hello World项目,就有一个简化版的微信登陆.只不过是,还没有写入到咱们自 ...

  3. [转]微信小程序、微信公众号、H5之间相互跳转

    本文转自:https://www.cnblogs.com/colorful-paopao1/p/8608609.html 转自慕课网 一.小程序和公众号 答案是:可以相互关联. 在微信公众号里可以添加 ...

  4. 微信小程序、微信公众号、H5之间相互跳转

    转自慕课网 一.小程序和公众号 答案是:可以相互关联. 在微信公众号里可以添加小程序. 图片有点小,我把文字打出来吧: 可关联已有的小程序或快速创建小程序.已关联的小程序可被使用在自定义菜单和模版消息 ...

  5. 微信小程序与微信公众号同一用户登录问题

    微信小程序与微信公众号同一用户登录问题 最近在做微信小程序与微信公众号登录合并的接口.整理相关资料以及个人认识的心得写了这篇文章与大家一起分享. 首先,简单说下我遇到的问题是我们的程序调用微信小程序得 ...

  6. 微信小程序(微信应用号)开发ide安装解决方法

    这两天整个技术圈都炸锅了,微信小程序(微信应用号)发布内测,首批200家收到邀请,但是没受邀请的同学,也不用担心,下面介绍一下解决方法. 首先需要下载ide,昨天只需要下载0.9版本的编辑器并替换文件 ...

  7. 微信小程序(原名微信应用号)开发工具0.9版安装教程

    微信小程序全称微信公众平台·小程序,原名微信公众平台·应用号(简称微信应用号) 声明 微信小程序开发工具类似于一个轻量级的IDE集成开发环境,目前仅开放给了少部分受微信官方邀请的人士(据说仅200个名 ...

  8. 微信小程序(微信应用号)组件讲解[申明:来源于网络]

    微信小程序(微信应用号)组件讲解[申明:来源于网络] 地址:http://www.cnblogs.com/muyixiaoguang/p/5902008.html

  9. 微信小程序和微信公众号的id是一个吗

    首先,简单说下我遇到的问题是我们的程序调用微信小程序得到openid,然后通过openID得到用户的唯一标识,用户得以登录,然而,当我们调用微信公众号也同样的到openid,同一以用户两个不同的ope ...

随机推荐

  1. 【Python学习之四】递归与尾递归

    看完廖雪峰老师的教程,感觉尾递归函数是一个相对难点.于是复习一下,思考了一下,发表一些见解,记录一下. 1.递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数 ...

  2. java实现可安装的exe程序

    java实现可安装的exe程序 通过编写Java代码,实现可安装的exe文件的一般思路: 1.在eclipse中创建java项目,然后编写Java代码,将编写好的Java项目导出一个.jar格式的ja ...

  3. CodeForce--Benches

    A. Benches   There are nn benches in the Berland Central park. It is known that aiai people are curr ...

  4. PAT Basic 1079

    1079 延迟的回文数(20 分) 给定一个 k+1 位的正整数 N,写成 a​k​​⋯a​1​​a​0​​ 的形式,其中对所有 i 有 0≤a​i​​<10 且 a​k​​>0.N 被称 ...

  5. 爬取豆瓣Top250_Ajax动态页面

    爬取网址: 完整代码: import sys from urllib import request, parse import ssl ssl._create_default_https_contex ...

  6. 03008_ServletContext

    1.什么是ServletContext? (1)ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象    内部封装是该web应用的信息,Servle ...

  7. JSONP分享-- 在JavaScript中跨域请求

    如果你正在开发一个现代的基于web的应用程序,那么你: 在客户端使用JavaScript. 需要集成那些没有完全在你控制之下的服务(或者那些来自不同的域). 在你的浏览器控制台中遇到过这个错误信息: ...

  8. PHP define() 定义常量

    PHP define()函数定义了运行时的常量, 具体语法如下所示: (PHP 4, PHP 5, PHP 7) define — Defines a named constant bool defi ...

  9. maven项目打包jar,含有依赖jar

    在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...

  10. URAL 1099 Work scheduling 一般图的最大匹配 带花树算法(模板)

    R - Work scheduling Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...