You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
11 KiB

пре 3 година
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Runtime.Serialization;
  7. using System.IO;
  8. using System.Text;
  9. using System.Net;
  10. using System.Web.Security;
  11. using LitJson;
  12. namespace WxPayAPI
  13. {
  14. public class JsApiPay
  15. {
  16. /// <summary>
  17. /// 保存页面对象,因为要在类的方法中使用Page的Request对象
  18. /// </summary>
  19. private Page page { get; set; }
  20. /// <summary>
  21. /// openid用于调用统一下单接口
  22. /// </summary>
  23. public string openid { get; set; }
  24. /// <summary>
  25. /// access_token用于获取收货地址js函数入口参数
  26. /// </summary>
  27. public string access_token { get; set; }
  28. /// <summary>
  29. /// out_trade_no 商家订单号
  30. /// </summary>
  31. public string out_trade_no { get; set; }
  32. /// <summary>
  33. /// 商品金额,用于统一下单
  34. /// </summary>
  35. public int total_fee { get; set; }
  36. /// <summary>
  37. /// 统一下单接口返回结果
  38. /// </summary>
  39. public WxPayData unifiedOrderResult { get; set; }
  40. public JsApiPay(Page page)
  41. {
  42. this.page = page;
  43. }
  44. /**
  45. *
  46. *
  47. * http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  48. * url跳转获取code
  49. * code去获取openid和access_token
  50. *
  51. */
  52. public void GetOpenidAndAccessToken()
  53. {
  54. if (!string.IsNullOrEmpty(page.Request.QueryString["code"]))
  55. {
  56. //获取code码,以获取openid和access_token
  57. string code = page.Request.QueryString["code"];
  58. Log.Debug(this.GetType().ToString(), "Get code : " + code);
  59. GetOpenidAndAccessTokenFromCode(code);
  60. }
  61. else
  62. {
  63. //构造网页授权获取code的URL
  64. string host = page.Request.Url.Host;
  65. string path = page.Request.Path;
  66. string redirect_uri = page.Server.UrlEncode(HttpContext.Current.Request.Url.ToString());
  67. WxPayData data = new WxPayData();
  68. data.SetValue("appid", WxPayConfig.APPID);
  69. data.SetValue("redirect_uri", redirect_uri);
  70. data.SetValue("response_type", "code");
  71. data.SetValue("scope", "snsapi_base");
  72. data.SetValue("state", "STATE" + "#wechat_redirect");
  73. string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
  74. Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
  75. try
  76. {
  77. //触发微信返回code码
  78. page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
  79. }
  80. catch (System.Threading.ThreadAbortException ex)
  81. {
  82. }
  83. //page.Response.Write("Page:" + url + "<br>--------<br>" + redirect_uri + "<br>--------<br>");
  84. }
  85. }
  86. /**
  87. *
  88. * code换取网页授权access_token和openid的返回数据JSON数据包如下
  89. * {
  90. * "access_token":"ACCESS_TOKEN",
  91. * "expires_in":7200,
  92. * "refresh_token":"REFRESH_TOKEN",
  93. * "openid":"OPENID",
  94. * "scope":"SCOPE",
  95. * "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
  96. * }
  97. * access_token可用于获取共享收货地址
  98. * openid是微信支付jsapi支付接口统一下单时必须的参数
  99. * http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  100. * @WxPayException
  101. */
  102. public void GetOpenidAndAccessTokenFromCode(string code)
  103. {
  104. try
  105. {
  106. //构造获取openid及access_token的url
  107. WxPayData data = new WxPayData();
  108. data.SetValue("appid", WxPayConfig.APPID);
  109. data.SetValue("secret", WxPayConfig.APPSECRET);
  110. data.SetValue("code", code);
  111. data.SetValue("grant_type", "authorization_code");
  112. string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();
  113. //请求url以获取数据
  114. string result = HttpService.Get(url);
  115. Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);
  116. //保存access_token,用于收货地址获取
  117. JsonData jd = JsonMapper.ToObject(result);
  118. access_token = (string)jd["access_token"];
  119. //获取用户openid
  120. openid = (string)jd["openid"];
  121. Log.Debug(this.GetType().ToString(), "Get openid : " + openid);
  122. Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token);
  123. }
  124. catch (Exception ex)
  125. {
  126. Log.Error(this.GetType().ToString(), ex.ToString());
  127. throw new WxPayException(ex.ToString());
  128. }
  129. }
  130. /**
  131. *
  132. * @return
  133. * @WxPayException
  134. */
  135. public WxPayData GetUnifiedOrderResult()
  136. {
  137. //统一下单
  138. WxPayData data = new WxPayData();
  139. data.SetValue("body", "百花网购物/" + out_trade_no);
  140. data.SetValue("attach", "百花网购物");
  141. data.SetValue("out_trade_no", out_trade_no);
  142. data.SetValue("total_fee", total_fee);
  143. data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
  144. data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
  145. data.SetValue("goods_tag", "百花网购物");
  146. data.SetValue("trade_type", "JSAPI");
  147. data.SetValue("openid", openid);
  148. WxPayData result = WxPayApi.UnifiedOrder(data);
  149. if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
  150. {
  151. Log.Error(this.GetType().ToString(), "UnifiedOrder response error!");
  152. throw new WxPayException("UnifiedOrder response error!");
  153. }
  154. unifiedOrderResult = result;
  155. return result;
  156. }
  157. /**
  158. *
  159. * jsapi支付所需的参数
  160. * JSAPI时的输入参数格式如下
  161. * {
  162. * "appId" : "wx2421b1c4370ec43b", //公众号名称,由商户传入
  163. * "timeStamp":" 1395712654", //时间戳,自1970年以来的秒数
  164. * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串
  165. * "package" : "prepay_id=u802345jgfjsdfgsdg888",
  166. * "signType" : "MD5", //微信签名方式:
  167. * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
  168. * }
  169. * @return string JSAPI时的输入参数json格式可以直接做参数用
  170. * APIhttp://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7
  171. *
  172. */
  173. public string GetJsApiParameters()
  174. {
  175. Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing...");
  176. WxPayData jsApiParam = new WxPayData();
  177. jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid"));
  178. jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
  179. jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
  180. jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id"));
  181. jsApiParam.SetValue("signType", "MD5");
  182. jsApiParam.SetValue("paySign", jsApiParam.MakeSign());
  183. string parameters = jsApiParam.ToJson();
  184. Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters);
  185. return parameters;
  186. }
  187. /**
  188. *
  189. * js函数入口参数,http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
  190. * @return string js函数需要的参数json格式可以直接做参数使用
  191. */
  192. public string GetEditAddressParameters()
  193. {
  194. string parameter = "";
  195. try
  196. {
  197. string host = page.Request.Url.Host;
  198. string path = page.Request.Path;
  199. string queryString = page.Request.Url.Query;
  200. //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
  201. string url = "http://" + host + path + queryString;
  202. //构造需要用SHA1算法加密的数据
  203. WxPayData signData = new WxPayData();
  204. signData.SetValue("appid", WxPayConfig.APPID);
  205. signData.SetValue("url", url);
  206. signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp());
  207. signData.SetValue("noncestr", WxPayApi.GenerateNonceStr());
  208. signData.SetValue("accesstoken", access_token);
  209. string param = signData.ToUrl();
  210. Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param);
  211. //SHA1加密
  212. string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1");
  213. Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign);
  214. //获取收货地址js函数入口参数
  215. WxPayData afterData = new WxPayData();
  216. afterData.SetValue("appId", WxPayConfig.APPID);
  217. afterData.SetValue("scope", "jsapi_address");
  218. afterData.SetValue("signType", "sha1");
  219. afterData.SetValue("addrSign", addrSign);
  220. afterData.SetValue("timeStamp", signData.GetValue("timestamp"));
  221. afterData.SetValue("nonceStr", signData.GetValue("noncestr"));
  222. //转为json格式
  223. parameter = afterData.ToJson();
  224. Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter);
  225. }
  226. catch (Exception ex)
  227. {
  228. Log.Error(this.GetType().ToString(), ex.ToString());
  229. throw new WxPayException(ex.ToString());
  230. }
  231. return parameter;
  232. }
  233. }
  234. }