• 微信支付异步回调,带你解决微信支付的深坑


    1.首先我们先下载微信支付的服务器端demo





    2.个文件作用介绍

    index.jsp  下单  payRequest.jsp  获取微信支付prepay_id等。

    重点我说说这个payNotifyUrl.jsp

    1. <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
    2. <%@ page import="com.tenpay.RequestHandler" %>
    3. <%@ page import="com.tenpay.ResponseHandler" %>
    4. <%@ page import="com.tenpay.client.ClientResponseHandler" %>
    5. <%@ page import="com.tenpay.client.TenpayHttpClient" %>
    6. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    7. <%
    8. //---------------------------------------------------------
    9. //财付通支付通知(后台通知)示例,商户按照此文档进行开发即可
    10. //---------------------------------------------------------
    11. //商户号
    12. String partner = "1900000109";
    13. //密钥
    14. String key = "8934e7d15453e97507ef794cf7b0519d";
    15. //创建支付应答对象
    16. ResponseHandler resHandler = new ResponseHandler(request, response);
    17. resHandler.setKey(key);
    18. //判断签名
    19. if(resHandler.isTenpaySign()) {
    20. //通知id
    21. String notify_id = resHandler.getParameter("notify_id");
    22. //创建请求对象
    23. RequestHandler queryReq = new RequestHandler(null, null);
    24. //通信对象
    25. TenpayHttpClient httpClient = new TenpayHttpClient();
    26. //应答对象
    27. ClientResponseHandler queryRes = new ClientResponseHandler();
    28. //通过通知ID查询,确保通知来至财付通
    29. queryReq.init();
    30. queryReq.setKey(key);
    31. queryReq.setGateUrl("https://gw.tenpay.com/gateway/verifynotifyid.xml");
    32. queryReq.setParameter("partner", partner);
    33. queryReq.setParameter("notify_id", notify_id);
    34. //通信对象
    35. httpClient.setTimeOut(5);
    36. //设置请求内容
    37. httpClient.setReqContent(queryReq.getRequestURL());
    38. System.out.println("queryReq:" + queryReq.getRequestURL());
    39. //后台调用
    40. if(httpClient.call()) {
    41. //设置结果参数
    42. queryRes.setContent(httpClient.getResContent());
    43. System.out.println("queryRes:" + httpClient.getResContent());
    44. queryRes.setKey(key);
    45. //获取返回参数
    46. String retcode = queryRes.getParameter("retcode");
    47. String trade_state = queryRes.getParameter("trade_state");
    48. String trade_mode = queryRes.getParameter("trade_mode");
    49. //判断签名及结果
    50. if(queryRes.isTenpaySign()&& "0".equals(retcode) && "0".equals(trade_state) && "1".equals(trade_mode)) {
    51. System.out.println("订单查询成功");
    52. //取结果参数做业务处理
    53. System.out.println("out_trade_no:" + queryRes.getParameter("out_trade_no")+
    54. " transaction_id:" + queryRes.getParameter("transaction_id"));
    55. System.out.println("trade_state:" + queryRes.getParameter("trade_state")+
    56. " total_fee:" + queryRes.getParameter("total_fee"));
    57. //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee
    58. System.out.println("discount:" + queryRes.getParameter("discount")+
    59. " time_end:" + queryRes.getParameter("time_end"));
    60. //------------------------------
    61. //处理业务开始
    62. //------------------------------
    63. //处理数据库逻辑
    64. //注意交易单不要重复处理
    65. //注意判断返回金额
    66. //------------------------------
    67. //处理业务完毕
    68. //------------------------------
    69. resHandler.sendToCFT("Success");
    70. }
    71. else{
    72. //错误时,返回结果未签名,记录retcode、retmsg看失败详情。
    73. System.out.println("查询验证签名失败或业务错误");
    74. System.out.println("retcode:" + queryRes.getParameter("retcode")+
    75. " retmsg:" + queryRes.getParameter("retmsg"));
    76. }
    77. } else {
    78. System.out.println("后台调用通信失败");
    79. System.out.println(httpClient.getResponseCode());
    80. System.out.println(httpClient.getErrInfo());
    81. //有可能因为网络原因,请求已经处理,但未收到应答。
    82. }
    83. }
    84. else{
    85. System.out.println("通知签名验证失败");
    86. }
    87. %>
    就是上面的这代码。完全没有用。查看sdk源码才知道 这个异步回调是接收微信发送的所有参数,然后排序  加密 验签。  最坑的是  微信 的参数根本不是通过的参数返回的。而是通过的流。所以这个太坑了。下面我把我修改过的源码发出来 帮助大家解决回调问题。

    首先是控制器

    1. /**
    2. * 异步回调接口
    3. * @param request
    4. * @param response
    5. * @throws Exception
    6. */
    7. @RequestMapping(value="/weixin_parent_notify.do",produces="text/html;charset=utf-8")
    8. @ResponseBody
    9. public String WeixinParentNotifyPage(HttpServletRequest request,HttpServletResponse response) throws Exception{
    10. ServletInputStream instream = request.getInputStream();
    11. StringBuffer sb = new StringBuffer();
    12. int len = -1;
    13. byte[] buffer = new byte[1024];
    14. while((len = instream.read(buffer)) != -1){
    15. sb.append(new String(buffer,0,len));
    16. }
    17. instream.close();
    18. SortedMap<String,String> map = WXRequestUtil.doXMLParseWithSorted(sb.toString());//接受微信的通知参数
    19. Map<String,String> return_data = new HashMap<String,String>();
    20. //创建支付应答对象
    21. ResponseHandler resHandler = new ResponseHandler(request, response);
    22. resHandler.setAllparamenters(map);
    23. resHandler.setKey(ConstantUtil.PARTNER_KEY[0]);
    24. //判断签名
    25. if(resHandler.isTenpaySign()){
    26. if(!map.get("return_code").toString().equals("SUCCESS")){
    27. return_data.put("return_code", "FAIL");
    28. return_data.put("return_msg", "return_code不正确");
    29. }else{
    30. if(!map.get("result_code").toString().equals("SUCCESS")){
    31. return_data.put("return_code", "FAIL");
    32. return_data.put("return_msg", "result_code不正确");
    33. }
    34. String out_trade_no = map.get("out_trade_no").toString();
    35. String time_end = map.get("time_end").toString();
    36. BigDecimal total_fee = new BigDecimal(map.get("total_fee").toString());
    37. //付款完成后,支付宝系统发送该交易状态通知
    38. System.out.println("交易成功");
    39. Map order = orderdao.PaymentEndGetOrderInfo(out_trade_no);
    40. if(order == null){
    41. System.out.println("订单不存在");
    42. return_data.put("return_code", "FAIL");
    43. return_data.put("return_msg", "订单不存在");
    44. return WXRequestUtil.GetMapToXML(return_data);
    45. }
    46. int order_type = (int) order.get("order_type");
    47. boolean payment_status = (boolean) order.get("payment_status");
    48. int supplier_id = (int) order.get("supplier_id");
    49. BigDecimal p = new BigDecimal("100");
    50. BigDecimal amount = (BigDecimal) order.get("amount");
    51. amount = amount.multiply(p);
    52. //如果订单已经支付返回错误
    53. if(payment_status){
    54. System.out.println("订单已经支付");
    55. return_data.put("return_code", "SUCCESS");
    56. return_data.put("return_msg", "OK");
    57. return WXRequestUtil.GetMapToXML(return_data);
    58. }
    59. //如果支付金额不等于订单金额返回错误
    60. if(amount.compareTo(total_fee)!=0){
    61. System.out.println("资金异常");
    62. return_data.put("return_code", "FAIL");
    63. return_data.put("return_msg", "金额异常");
    64. return WXRequestUtil.GetMapToXML(return_data);
    65. }
    66. //更新订单信息
    67. if(orderdao.PaymentEndUpdateOrder(out_trade_no, time_end)){
    68. System.out.println("更新订单成功");
    69. //如果该订单是幼儿产品 并且 存在代理
    70. if(order_type == 2){
    71. if(supplier_id != 0){
    72. Map su = userdao.getSupplierInfo(supplier_id);
    73. String phone = (String) su.get("phone_number");
    74. String nickname = (String) su.get("nickname");
    75. String app_token = (String) su.get("app_token");
    76. String content = "【三盛科创】尊敬的"+ nickname +"您好。您在我们平台出售的商品有新用户下单。请您点击该链接查看发货信息。"+Config.WEB_SERVER+"/order/SupplierOrderInfo.do?order_number="+out_trade_no+"&sid="+app_token+".请您务必妥善包管。";
    77. MessageUtil.SendMessage(phone,content);
    78. }
    79. }else{
    80. orderdao.UpdateOrderStatus(out_trade_no, 3);//更新订单为已完成
    81. }
    82. return_data.put("return_code", "SUCCESS");
    83. return_data.put("return_msg", "OK");
    84. return WXRequestUtil.GetMapToXML(return_data);
    85. }else{
    86. return_data.put("return_code", "FAIL");
    87. return_data.put("return_msg", "更新订单失败");
    88. return WXRequestUtil.GetMapToXML(return_data);
    89. }
    90. }
    91. }else{
    92. return_data.put("return_code", "FAIL");
    93. return_data.put("return_msg", "签名错误");
    94. }
    95. String xml = WXRequestUtil.GetMapToXML(return_data);
    96. return xml;
    97. }

    微信工具类WXRequestUtil.java

    1. package com.tenpay.util;
    2. import java.io.BufferedReader;
    3. import java.io.ByteArrayInputStream;
    4. import java.io.File;
    5. import java.io.FileInputStream;
    6. import java.io.InputStream;
    7. import java.io.InputStreamReader;
    8. import java.io.OutputStream;
    9. import java.net.ConnectException;
    10. import java.net.HttpURLConnection;
    11. import java.net.InetAddress;
    12. import java.net.URL;
    13. import java.security.KeyStore;
    14. import java.util.Date;
    15. import java.util.HashMap;
    16. import java.util.Iterator;
    17. import java.util.List;
    18. import java.util.Map;
    19. import java.util.SortedMap;
    20. import java.util.TreeMap;
    21. import javax.net.ssl.SSLContext;
    22. import org.apache.http.Consts;
    23. import org.apache.http.HttpEntity;
    24. import org.apache.http.client.methods.CloseableHttpResponse;
    25. import org.apache.http.client.methods.HttpPost;
    26. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    27. import org.apache.http.conn.ssl.SSLContexts;
    28. import org.apache.http.entity.StringEntity;
    29. import org.apache.http.impl.client.CloseableHttpClient;
    30. import org.apache.http.impl.client.HttpClients;
    31. import org.apache.http.util.EntityUtils;
    32. import org.glassfish.jersey.internal.util.Base64;
    33. import org.jdom.Document;
    34. import org.jdom.Element;
    35. import org.jdom.input.SAXBuilder;
    36. import com.zhiweism.util.MD5;
    37. import com.zhiweism.util.Util;
    38. /*
    39. * 用户发起统一下单请求
    40. * 作者:董志平
    41. * 用于发起微信扫码支付接口
    42. */
    43. public class WXRequestUtil {
    44. private static String WXSign = null;
    45. // //测试
    46. public static void main(String[] args) {
    47. //Map<String,String> res = SendPayment("苹果","20170106113325",1,0);
    48. }
    49. /*
    50. * 发起支付请求
    51. * body 商品描述
    52. * out_trade_no 订单号
    53. * total_fee 订单金额 单位 元
    54. * product_id 商品ID
    55. */
    56. public static Map<String,String> SendPayment(String body,String out_trade_no,double total_fee,int app_type){
    57. String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    58. String xml = WXParamGenerate(body,out_trade_no,total_fee,app_type);
    59. String res = httpsRequest(url,"POST",xml);
    60. Map<String,String> data = null;
    61. try {
    62. data = doXMLParse(res);
    63. } catch (Exception e) {
    64. data = null;
    65. }
    66. return data;
    67. }
    68. /**
    69. * 获取签名
    70. * @return
    71. */
    72. public static String getWXSign() {
    73. return WXSign;
    74. }
    75. /**
    76. * 获得随机字符串
    77. *
    78. */
    79. public static String NonceStr(){
    80. String res = Base64.encodeAsString(Math.random()+"::"+new Date().toString()).substring(0, 30);
    81. return res;
    82. }
    83. /**
    84. * 获取时间戳
    85. */
    86. public static String GetTimeStamp(){
    87. int t = (int)(System.currentTimeMillis()/1000);
    88. return t+"";
    89. }
    90. /**
    91. * 获取用户的ip
    92. *
    93. */
    94. public static String GetIp() {
    95. InetAddress ia=null;
    96. try {
    97. ia=InetAddress.getLocalHost();
    98. String localip=ia.getHostAddress();
    99. return localip;
    100. } catch (Exception e) {
    101. return null;
    102. }
    103. }
    104. /**
    105. * 获取签名
    106. *
    107. */
    108. public static String GetSign(Map<String,String> param,int app_type){
    109. String StringA = Util.formatUrlMap(param, false, false);
    110. String stringSignTemp = MD5.md5(StringA+"&key="+ConstantUtil.PARTNER_KEY[app_type]).toUpperCase();
    111. return stringSignTemp;
    112. }
    113. /**
    114. *
    115. * Map转xml数据
    116. */
    117. public static String GetMapToXML(Map<String,String> param){
    118. StringBuffer sb = new StringBuffer();
    119. sb.append("<xml>");
    120. for (Map.Entry<String,String> entry : param.entrySet()) {
    121. sb.append("<"+ entry.getKey() +">");
    122. sb.append(entry.getValue());
    123. sb.append("</"+ entry.getKey() +">");
    124. }
    125. sb.append("</xml>");
    126. return sb.toString();
    127. }
    128. //微信统一下单参数设置
    129. public static String WXParamGenerate(String description,String out_trade_no,double total_fee,int app_type){
    130. int fee = (int)(total_fee * 100.00);
    131. Map<String,String> param = new HashMap<String,String>();
    132. param.put("appid", ConstantUtil.APP_ID[app_type]);
    133. param.put("mch_id", ConstantUtil.PARTNER[app_type]);
    134. param.put("nonce_str",NonceStr());
    135. param.put("body",description);
    136. param.put("out_trade_no",out_trade_no);
    137. param.put("total_fee", fee+"");
    138. param.put("spbill_create_ip", GetIp());
    139. param.put("notify_url", ConstantUtil.WEIXIN_NOTIFY[app_type]);
    140. param.put("trade_type", "APP");
    141. WXSign = GetSign(param,app_type);
    142. param.put("sign", WXSign);
    143. return GetMapToXML(param);
    144. }
    145. //发起微信支付请求
    146. public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
    147. try {
    148. URL url = new URL(requestUrl);
    149. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    150. conn.setDoOutput(true);
    151. conn.setDoInput(true);
    152. conn.setUseCaches(false);
    153. // 设置请求方式(GET/POST)
    154. conn.setRequestMethod(requestMethod);
    155. conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
    156. // 当outputStr不为null时向输出流写数据
    157. if (null != outputStr) {
    158. OutputStream outputStream = conn.getOutputStream();
    159. // 注意编码格式
    160. outputStream.write(outputStr.getBytes("UTF-8"));
    161. outputStream.close();
    162. }
    163. // 从输入流读取返回内容
    164. InputStream inputStream = conn.getInputStream();
    165. InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
    166. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    167. String str = null;
    168. StringBuffer buffer = new StringBuffer();
    169. while ((str = bufferedReader.readLine()) != null) {
    170. buffer.append(str);
    171. }
    172. // 释放资源
    173. bufferedReader.close();
    174. inputStreamReader.close();
    175. inputStream.close();
    176. inputStream = null;
    177. conn.disconnect();
    178. return buffer.toString();
    179. } catch (ConnectException ce) {
    180. System.out.println("连接超时:{}"+ ce);
    181. } catch (Exception e) {
    182. System.out.println("https请求异常:{}"+ e);
    183. }
    184. return null;
    185. }
    186. //退款的请求方法
    187. public static String httpsRequest2(String requestMethod, String outputStr) throws Exception {
    188. KeyStore keyStore = KeyStore.getInstance("PKCS12");
    189. StringBuilder res = new StringBuilder("");
    190. FileInputStream instream = new FileInputStream(new File("/home/apiclient_cert.p12"));
    191. try {
    192. keyStore.load(instream, "".toCharArray());
    193. } finally {
    194. instream.close();
    195. }
    196. // Trust own CA and all self-signed certs
    197. SSLContext sslcontext = SSLContexts.custom()
    198. .loadKeyMaterial(keyStore, "1313329201".toCharArray())
    199. .build();
    200. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
    201. sslcontext,
    202. new String[] { "TLSv1" },
    203. null,
    204. SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    205. CloseableHttpClient httpclient = HttpClients.custom()
    206. .setSSLSocketFactory(sslsf)
    207. .build();
    208. try {
    209. HttpPost httpost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
    210. httpost.addHeader("Connection", "keep-alive");
    211. httpost.addHeader("Accept", "*/*");
    212. httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    213. httpost.addHeader("Host", "api.mch.weixin.qq.com");
    214. httpost.addHeader("X-Requested-With", "XMLHttpRequest");
    215. httpost.addHeader("Cache-Control", "max-age=0");
    216. httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
    217. StringEntity entity2 = new StringEntity(outputStr ,Consts.UTF_8);
    218. httpost.setEntity(entity2);
    219. System.out.println("executing request" + httpost.getRequestLine());
    220. CloseableHttpResponse response = httpclient.execute(httpost);
    221. try {
    222. HttpEntity entity = response.getEntity();
    223. System.out.println("----------------------------------------");
    224. System.out.println(response.getStatusLine());
    225. if (entity != null) {
    226. System.out.println("Response content length: " + entity.getContentLength());
    227. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
    228. String text = "";
    229. res.append(text);
    230. while ((text = bufferedReader.readLine()) != null) {
    231. res.append(text);
    232. System.out.println(text);
    233. }
    234. }
    235. EntityUtils.consume(entity);
    236. } finally {
    237. response.close();
    238. }
    239. } finally {
    240. httpclient.close();
    241. }
    242. return res.toString();
    243. }
    244. //xml解析
    245. public static Map<String, String> doXMLParse(String strxml) throws Exception {
    246. strxml = strxml.replaceFirst("encoding=".*"", "encoding="UTF-8"");
    247. if(null == strxml || "".equals(strxml)) {
    248. return null;
    249. }
    250. Map<String,String> m = new HashMap<String,String>();
    251. InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
    252. SAXBuilder builder = new SAXBuilder();
    253. Document doc = builder.build(in);
    254. Element root = doc.getRootElement();
    255. List list = root.getChildren();
    256. Iterator it = list.iterator();
    257. while(it.hasNext()) {
    258. Element e = (Element) it.next();
    259. String k = e.getName();
    260. String v = "";
    261. List children = e.getChildren();
    262. if(children.isEmpty()) {
    263. v = e.getTextNormalize();
    264. } else {
    265. v = getChildrenText(children);
    266. }
    267. m.put(k, v);
    268. }
    269. //关闭流
    270. in.close();
    271. return m;
    272. }
    273. //xml解析
    274. public static SortedMap<String, String> doXMLParseWithSorted(String strxml) throws Exception {
    275. strxml = strxml.replaceFirst("encoding=".*"", "encoding="UTF-8"");
    276. if(null == strxml || "".equals(strxml)) {
    277. return null;
    278. }
    279. SortedMap<String,String> m = new TreeMap<String,String>();
    280. InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
    281. SAXBuilder builder = new SAXBuilder();
    282. Document doc = builder.build(in);
    283. Element root = doc.getRootElement();
    284. List list = root.getChildren();
    285. Iterator it = list.iterator();
    286. while(it.hasNext()) {
    287. Element e = (Element) it.next();
    288. String k = e.getName();
    289. String v = "";
    290. List children = e.getChildren();
    291. if(children.isEmpty()) {
    292. v = e.getTextNormalize();
    293. } else {
    294. v = getChildrenText(children);
    295. }
    296. m.put(k, v);
    297. }
    298. //关闭流
    299. in.close();
    300. return m;
    301. }
    302. public static String getChildrenText(List children) {
    303. StringBuffer sb = new StringBuffer();
    304. if(!children.isEmpty()) {
    305. Iterator it = children.iterator();
    306. while(it.hasNext()) {
    307. Element e = (Element) it.next();
    308. String name = e.getName();
    309. String value = e.getTextNormalize();
    310. List list = e.getChildren();
    311. sb.append("<" + name + ">");
    312. if(!list.isEmpty()) {
    313. sb.append(getChildrenText(list));
    314. }
    315. sb.append(value);
    316. sb.append("</" + name + ">");
    317. }
    318. }
    319. return sb.toString();
    320. }
    321. }
    修改微信ResponseHandler.java

    1. package com.tenpay;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import java.io.UnsupportedEncodingException;
    5. import java.util.Iterator;
    6. import java.util.Map;
    7. import java.util.Set;
    8. import java.util.SortedMap;
    9. import java.util.TreeMap;
    10. import javax.servlet.http.HttpServletRequest;
    11. import javax.servlet.http.HttpServletResponse;
    12. import com.tenpay.util.MD5Util;
    13. import com.tenpay.util.TenpayUtil;
    14. /**
    15. *
    16. * @author miklchen
    17. *
    18. */
    19. public class ResponseHandler {
    20. private String key;
    21. private SortedMap parameters;
    22. private String debugInfo;
    23. private HttpServletRequest request;
    24. private HttpServletResponse response;
    25. private String uriEncoding;
    26. /**
    27. *
    28. * @param request
    29. * @param response
    30. */
    31. public ResponseHandler(HttpServletRequest request,
    32. HttpServletResponse response) {
    33. this.request = request;
    34. this.response = response;
    35. this.key = "";
    36. this.parameters = new TreeMap();
    37. this.debugInfo = "";
    38. this.uriEncoding = "";
    39. }
    40. /**
    41. */
    42. public String getKey() {
    43. return key;
    44. }
    45. /**
    46. *
    47. */
    48. public void setKey(String key) {
    49. this.key = key;
    50. }
    51. /**
    52. * @param parameter
    53. * @return String
    54. */
    55. public String getParameter(String parameter) {
    56. String s = (String)this.parameters.get(parameter);
    57. return (null == s) ? "" : s;
    58. }
    59. /**
    60. * @param parameter
    61. * @param parameterValueֵ
    62. */
    63. public void setParameter(String parameter, String parameterValue) {
    64. String v = "";
    65. if(null != parameterValue) {
    66. v = parameterValue.trim();
    67. }
    68. this.parameters.put(parameter, v);
    69. }
    70. /**
    71. *
    72. * @return SortedMap
    73. */
    74. public SortedMap getAllParameters() {
    75. return this.parameters;
    76. }
    77. public void setAllparamenters(SortedMap map){
    78. this.parameters = map;
    79. }
    80. /**
    81. * 微信异步回调签名
    82. * @return boolean
    83. */
    84. public boolean isTenpaySign() {
    85. StringBuffer sb = new StringBuffer();
    86. Set es = this.parameters.entrySet();
    87. Iterator it = es.iterator();
    88. while(it.hasNext()) {
    89. Map.Entry entry = (Map.Entry)it.next();
    90. String k = (String)entry.getKey();
    91. String v = (String)entry.getValue();
    92. if(!"sign".equals(k) && null != v && !"".equals(v)) {
    93. sb.append(k + "=" + v + "&");
    94. }
    95. }
    96. sb.append("key="+this.getKey());
    97. String enc = TenpayUtil.getCharacterEncoding(this.request, this.response);
    98. String sign = MD5Util.MD5Encode(sb.toString(), enc).toLowerCase();
    99. String tenpaySign = this.getParameter("sign").toLowerCase();
    100. System.out.println("sign:"+sign+" tenpaysign:"+tenpaySign);
    101. return tenpaySign.equals(sign);
    102. }
    103. /**
    104. *
    105. * @throws IOException
    106. */
    107. public void sendToCFT(String msg) throws IOException {
    108. String strHtml = msg;
    109. PrintWriter out = this.getHttpServletResponse().getWriter();
    110. out.println(strHtml);
    111. out.flush();
    112. out.close();
    113. }
    114. /**
    115. *
    116. * @return String
    117. */
    118. public String getUriEncoding() {
    119. return uriEncoding;
    120. }
    121. /**
    122. * @param uriEncoding
    123. * @throws UnsupportedEncodingException
    124. */
    125. public void setUriEncoding(String uriEncoding)
    126. throws UnsupportedEncodingException {
    127. if (!"".equals(uriEncoding.trim())) {
    128. this.uriEncoding = uriEncoding;
    129. String enc = TenpayUtil.getCharacterEncoding(request, response);
    130. Iterator it = this.parameters.keySet().iterator();
    131. while (it.hasNext()) {
    132. String k = (String) it.next();
    133. String v = this.getParameter(k);
    134. v = new String(v.getBytes(uriEncoding.trim()), enc);
    135. this.setParameter(k, v);
    136. }
    137. }
    138. }
    139. /**
    140. */
    141. public String getDebugInfo() {
    142. return debugInfo;
    143. }
    144. /**
    145. *
    146. */
    147. protected void setDebugInfo(String debugInfo) {
    148. this.debugInfo = debugInfo;
    149. }
    150. protected HttpServletRequest getHttpServletRequest() {
    151. return this.request;
    152. }
    153. protected HttpServletResponse getHttpServletResponse() {
    154. return this.response;
    155. }
    156. }

    试试是不是已经可以发起异步回调了。记得key  在  微信支付 -》API安全  下面设置。





  • 相关阅读:
    Eclipse 软件 Java 解决:出现的editor does not contain a main type错误框 问题
    Oracle SQL高级编程——分析函数(窗口函数)全面讲解
    瑞联科技:Pwp3框架 调用存储过程返还数据集合 到前端界面展示
    spring jdbctemplate调用存储过程,返回list对象
    利用jquery操作Radio方法小结
    Oracle临时表
    JdbcUtil
    整理oracle 树形查询
    Js:消息弹出框、获取时间区间、时间格式、easyui datebox 自定义校验、表单数据转化json、控制两个日期不能只填一个
    Java对象之间的深度复制拷贝
  • 原文地址:https://www.cnblogs.com/jpfss/p/9942630.html
Copyright © 2020-2023  润新知