详解APP微信支付(java后台_统一下单和回调)
程序员文章站
2024-02-24 23:37:52
1.微信配置信息 global.properties
2.方法wxpay用于生成预支付订单信息
方法notifyweixinpay用于微信支付成功后的回调, 注意:...
1.微信配置信息 global.properties
2.方法wxpay用于生成预支付订单信息
方法notifyweixinpay用于微信支付成功后的回调, 注意: 在手机端使用微信支付成功后,微信服务器会根据提供的回调地址进行回调, parametermap.put("notify_url", wxnotify); (见下面代码)
在局域网是无法进行回调的,必须将你的服务端放在公网上进行测试, 回调函数会被多次调用,如果第一次成功后,你可以将业务数据状态标志为已处理, 对于相同订单的其它回调就不需要再次处理了;
package com.main.controller; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.unsupportedencodingexception; import java.math.bigdecimal; import java.util.date; import java.util.hashmap; import java.util.map; import java.util.sortedmap; import java.util.treemap; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jdom.jdomexception; import org.springframework.http.mediatype; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import com.main.model.weixinprepay; import com.main.util.configmanager; import com.main.util.dateutil; import com.main.util.generalconstant; import com.main.util.paycommonutil; import com.main.util.result; import com.main.util.stringutil; @controller @requestmapping("/pay") public class paycontroller { string randomstring = paycommonutil.getrandomstring(32); //支付成功后的回调函数 public static string wxnotify = "http://com.zhuohuicalss/pay/notifyweixinpay"; public paycontroller() { system.out.println("maincontroller构造函数"); } /** * @param totalamount 支付金额 * @param description 描述 * @param request * @return */ @requestmapping(value = "/wxpay", produces = mediatype.application_json_value) @responsebody public result wxpay(httpservletrequest request) { result result = new result(); long userid = new long(1);//basecontroller.getuserid(); bigdecimal totalamount = new bigdecimal(request.getparameter("totalprice")); string trade_no = ""; string description=""; try { trade_no = new string(request.getparameter("ordernum").getbytes("iso-8859-1"),"utf-8"); description = request.getparameter("description"); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } string openid = ""; map<string, string> map = weixinprepay(trade_no,totalamount,description,openid,request); sortedmap<string, object> finalpackage = new treemap<string, object>(); //应用id finalpackage.put("appid", configmanager.getinstance().getconfigitem("wxappid")/*paycommonutil.appid*/); //商户号 finalpackage.put("partnerid", configmanager.getinstance().getconfigitem("mch_id")); long time = (system.currenttimemillis() / 1000); //时间戳 finalpackage.put("timestamp", time.tostring()); //随机字符串 finalpackage.put("noncestr", map.get("nonce_str")); //预支付交易会话id finalpackage.put("prepayid", map.get("prepay_id")); //扩展字段 finalpackage.put("package", "sign=wxpay"); weixinprepay prepay = new weixinprepay(); prepay.setappid(configmanager.getinstance().getconfigitem("wxappid")); prepay.setmchid(configmanager.getinstance().getconfigitem("mch_id")); prepay.settimestamp(time.tostring()); prepay.setnoncestr(map.get("nonce_str")); prepay.setprepayid(map.get("prepay_id")); prepay.setsigntype("md5"); prepay.setpaysign(sign); result.setdata(prepay); result.setstatecode(generalconstant.success); result.setdesc("微信支付加载成功"); return result; } /** * 统一下单 * 应用场景:商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识后再在app里面调起支付。 * @param trade_no * @param totalamount * @param description * @param openid * @param sym * @param request * @return */ @suppresswarnings("unchecked") public map<string, string> weixinprepay(string trade_no,bigdecimal totalamount, string description, string openid, httpservletrequest request) { sortedmap<string, object> parametermap = new treemap<string, object>(); parametermap.put("appid", configmanager.getinstance().getconfigitem("wxappid")); //应用appid parametermap.put("mch_id", configmanager.getinstance().getconfigitem("mch_id")/*paycommonutil.mch_id*/); //商户号 //parametermap.put("device_info", "web"); parametermap.put("nonce_str", randomstring); parametermap.put("body", description); parametermap.put("out_trade_no", trade_no); parametermap.put("fee_type", "cny"); system.out.println("jiner"); bigdecimal total = totalamount.multiply(new bigdecimal(100)); //接口中参数支付金额单位为【分】,参数值不能带小数,所以乘以100 java.text.decimalformat df=new java.text.decimalformat("0"); parametermap.put("total_fee", df.format(total)); system.out.println("jiner2"); parametermap.put("spbill_create_ip", paycommonutil.getremotehost(request)); parametermap.put("notify_url", wxnotify); parametermap.put("trade_type", "app");//"jsapi" //trade_type为jsapi是 openid为必填项 //parametermap.put("openid", openid); system.out.println(""); string sign = paycommonutil.createsign("utf-8", parametermap); system.out.println("jiner2"); parametermap.put("sign", sign); string requestxml = paycommonutil.getrequestxml(parametermap); system.out.println(requestxml); string result = paycommonutil.httpsrequest( "https://api.mch.weixin.qq.com/pay/unifiedorder", "post", requestxml); system.out.println(result); map<string, string> map = null; try { map = paycommonutil.doxmlparse(result); } catch (jdomexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return map; } /** * 此函数会被执行多次,如果支付状态已经修改为已支付,则下次再调的时候判断是否已经支付,如果已经支付了,则什么也执行 * @param request * @param response * @return * @throws ioexception * @throws jdomexception */ @requestmapping(value = "notifyweixinpay", produces = mediatype.application_json_value) // @requestdescription("支付回调地址") @responsebody public string notifyweixinpay(httpservletrequest request, httpservletresponse response) throws ioexception, jdomexception { system.out.println("微信支付回调"); inputstream instream = request.getinputstream(); bytearrayoutputstream outsteam = new bytearrayoutputstream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = instream.read(buffer)) != -1) { outsteam.write(buffer, 0, len); } string resultxml = new string(outsteam.tobytearray(), "utf-8"); map<string, string> params = paycommonutil.doxmlparse(resultxml); outsteam.close(); instream.close(); map<string,string> return_data = new hashmap<string,string>(); if (!paycommonutil.istenpaysign(params)) { // 支付失败 return_data.put("return_code", "fail"); return_data.put("return_msg", "return_code不正确"); return stringutil.getmaptoxml(return_data); } else { system.out.println("===============付款成功=============="); // ------------------------------ // 处理业务开始 // ------------------------------ // 此处处理订单状态,结合自己的订单数据完成订单状态的更新 // ------------------------------ string total_fee = params.get("total_fee"); double v = double.valueof(total_fee) / 100; string out_trade_no = string.valueof(long.parselong(params.get("out_trade_no").split("o")[0])); date accounttime = dateutil.stringtodate(params.get("time_end"), "yyyymmddhhmmss"); string ordertime = dateutil.datetostring(new date(), "yyyy-mm-dd hh:mm:ss"); string totalamount = string.valueof(v); string appid = params.get("appid"); string tradeno = params.get("transaction_id"); return_data.put("return_code", "success"); return_data.put("return_msg", "ok"); return stringutil.getmaptoxml(return_data); } } }
3.用到的一些工具类
import java.io.inputstream; import java.util.*; /** * 读取配置文件的类 单例类 * @author administrator * */ public class configmanager { // 属性文件命名 private properties m_props = null; private static map<string,string> configmap; private static configmanager m_instance = null; private static properties props = null; private configmanager() { m_props = new properties(); configmap = new hashmap<string,string>(); try { props = system.getproperties(); //获取系统属性 m_props.load(getinputstream()); getsysconfigmsg(); } catch (exception e) { e.printstacktrace(); } } public synchronized static configmanager getinstance() { if(m_instance == null){ m_instance = new configmanager(); } return m_instance; } public inputstream getinputstream() { inputstream is = null; try { is = getclass().getclassloader().getresourceasstream("global.properties"); } catch (exception e) { e.printstacktrace(); } return is; } public map<string,string> getsysconfigmsg(){ set<object> keyset = m_props.keyset(); iterator<object> it = keyset.iterator(); while(it.hasnext()){ string nextkey = it.next().tostring(); configmap.put(nextkey,getconfigitem(nextkey)); } return configmap; } public string getconfigitem(string name) { string val = m_props.getproperty(name).trim(); if("filesavepath".equals(name)){ if(props.getproperty("os.name").startswith("windows")){ val = val.split("#")[0].tostring().trim(); }else{ val = val.split("#")[1].tostring().trim(); } } return val; } public map<string,string> getconfigmap(){ return configmap; } }
import java.text.dateformat; import java.text.parseposition; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.regex.pattern; public class dateutil { // 格式:年-月-日 小时:分钟:秒 public static final string format_one = "yyyy-mm-dd hh:mm:ss"; // 格式:年-月-日 小时:分钟 public static final string format_two = "yyyy-mm-dd hh:mm"; // 格式:年月日 小时分钟秒 public static final string format_three = "yyyymmdd-hhmmss"; // 格式:年月日 public static final string format_four = "yyyymmdd"; // 格式:年-月-日 public static final string long_date_format = "yyyy-mm-dd"; // 格式:月-日 public static final string short_date_format = "mm-dd"; // 格式:小时:分钟:秒 public static final string long_time_format = "hh:mm:ss"; //格式:年-月 public static final string montg_date_format = "yyyy-mm"; // 年的加减 public static final int sub_year = calendar.year; // 月加减 public static final int sub_month = calendar.month; // 天的加减 public static final int sub_day = calendar.date; // 小时的加减 public static final int sub_hour = calendar.hour; // 分钟的加减 public static final int sub_minute = calendar.minute; // 秒的加减 public static final int sub_second = calendar.second; static final string daynames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; public dateutil() { } /** * 把符合日期格式的字符串转换为日期类型 */ public static date stringtodate(string datestr, string format) { date d = null; simpledateformat formater = new simpledateformat(format); try { formater.setlenient(false); d = formater.parse(datestr); } catch (exception e) { // log.error(e); d = null; } return d; } /** * 把符合日期格式的字符串转换为日期类型 */ public static date stringtodate(string datestr, string format, parseposition pos) { date d = null; simpledateformat formater = new simpledateformat(format); try { formater.setlenient(false); d = formater.parse(datestr, pos); } catch (exception e) { d = null; } return d; } /** * 把日期转换为字符串 */ public static string datetostring(date date, string format) { string result = ""; simpledateformat formater = new simpledateformat(format); try { result = formater.format(date); } catch (exception e) { // log.error(e); } return result; } /** * 获取当前时间的指定格式 */ public static string getcurrdate(string format) { return datetostring(new date(), format); } /** * * @title: datesub * @date 2014-1-9 上午10:44:02 * @description: 得到指定日期前(后)的日期 * @param: @param datekind 例:calendar.day_of_month * @param: @param datestr 指定日期 * @param: @param amount 增加(减去)的时间量 * @param: @return * @return: string * @throws * @author mtf */ public static string datesub(int datekind, string datestr, int amount) { date date = stringtodate(datestr, montg_date_format); calendar calendar = calendar.getinstance(); calendar.settime(date); calendar.add(datekind, amount); return datetostring(calendar.gettime(), format_one); } /** * 昨日日期 * @return */ public static string yearthdate(string datestr){ date date = stringtodate(datestr, long_date_format);//取时间 calendar calendar = calendar.getinstance(); calendar.settime(date); calendar.add(calendar.date,-1);//把日期往后增加一天.整数往后推,负数往前移动 //date=calendar.gettime(); //这个时间就是日期往后推一天的结果 return datetostring(calendar.gettime(), long_date_format); } /** * 两个日期相减 * @return 相减得到的秒数 */ public static long timesub(string firsttime, string sectime) { long first = stringtodate(firsttime, format_one).gettime(); long second = stringtodate(sectime, format_one).gettime(); return (second - first) / 1000; } /** * 两个日期相减 * 参数地date * second 两个日期相差的秒 * @return 相减得到的秒数 * 后面时间减去前面时间 再减去 相差秒数 如果大于0 返回 fasle */ public static boolean timesub(date firsttime, date sectime,long secs) { long first = firsttime.gettime(); long second = sectime.gettime(); // 判断两个时间 是否间隔那么长 secs。 return (second - first - secs) > 0 ? false:true; } /** * 两个日期相减 * 参数地date * @return 相减得到的秒数 * 后面时间减去前面时间 如果大于0 返回 false */ public static boolean timesub(date firsttime, date sectime) { long first = firsttime.gettime(); long second = sectime.gettime(); return (second - first)>0?false:true; } /** * 获得某月的天数 */ public static int getdaysofmonth(string year, string month) { int days = 0; if (month.equals("1") || month.equals("3") || month.equals("5") || month.equals("7") || month.equals("8") || month.equals("10") || month.equals("12")) { days = 31; } else if (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11")) { days = 30; } else { if ((integer.parseint(year) % 4 == 0 && integer.parseint(year) % 100 != 0) || integer.parseint(year) % 400 == 0) { days = 29; } else { days = 28; } } return days; } /** * 获取某年某月的天数 */ public static int getdaysofmonth(int year, int month) { calendar calendar = calendar.getinstance(); calendar.set(year, month - 1, 1); return calendar.getactualmaximum(calendar.day_of_month); } /** * 获得当前日期 */ public static int gettoday() { calendar calendar = calendar.getinstance(); return calendar.get(calendar.date); } /** * 获得当前月份 */ public static int gettomonth() { calendar calendar = calendar.getinstance(); return calendar.get(calendar.month) + 1; } /** * 获得当前年份 */ public static int gettoyear() { calendar calendar = calendar.getinstance(); return calendar.get(calendar.year); } /** * 返回日期的天 */ public static int getday(date date) { calendar calendar = calendar.getinstance(); calendar.settime(date); return calendar.get(calendar.date); } /** * 返回日期的年 */ public static int getyear(date date) { calendar calendar = calendar.getinstance(); calendar.settime(date); return calendar.get(calendar.year); } /** * 返回日期的月份,1-12 */ public static int getmonth(date date) { calendar calendar = calendar.getinstance(); calendar.settime(date); return calendar.get(calendar.month) + 1; } /** * 计算两个日期相差的天数,如果date2 > date1 返回正数,否则返回负数 */ public static long daydiff(date date1, date date2) { return (date2.gettime() - date1.gettime()) / 86400000; } /** * 比较两个日期的年差 */ public static int yeardiff(string before, string after) { date beforeday = stringtodate(before, long_date_format); date afterday = stringtodate(after, long_date_format); return getyear(afterday) - getyear(beforeday); } /** * 比较指定日期与当前日期的差 */ public static int yeardiffcurr(string after) { date beforeday = new date(); date afterday = stringtodate(after, long_date_format); return getyear(beforeday) - getyear(afterday); } /** * 获取每月的第一周 */ public static int getfirstweekdayofmonth(int year, int month) { calendar c = calendar.getinstance(); c.setfirstdayofweek(calendar.saturday); // 星期天为第一天 c.set(year, month - 1, 1); return c.get(calendar.day_of_week); } /** * 获取每月的最后一周 */ public static int getlastweekdayofmonth(int year, int month) { calendar c = calendar.getinstance(); c.setfirstdayofweek(calendar.saturday); // 星期天为第一天 c.set(year, month - 1, getdaysofmonth(year, month)); return c.get(calendar.day_of_week); } /** * 获得当前日期字符串,格式"yyyy-mm-dd hh:mm:ss" * * @return */ public static string getnow() { calendar today = calendar.getinstance(); return datetostring(today.gettime(), format_one); } /** * 判断日期是否有效,包括闰年的情况 * * @param date * yyyy-mm-dd * @return */ public static boolean isdate(string date) { stringbuffer reg = new stringbuffer( "^((\\d{2}(([02468][048])|([13579][26]))-?((((0?"); reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))"); reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|"); reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12"); reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))"); reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))"); reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?["); reg.append("1-9])|(1[0-9])|(2[0-8]))))))"); pattern p = pattern.compile(reg.tostring()); return p.matcher(date).matches(); } /***** * 时间 增加、减少 n个小时以后时间 * @param date * yyyy-mm-dd hh:mm:ss * @param num>0 小时 * @param type 增加和减少标志 * **/ public static date adjustdatebyhour(date d ,integer num, int type) { calendar cal= calendar.getinstance(); dateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); cal.settime(d); if(type==0){ cal.add(calendar.minute,-num); // system.out.println("date:"+df.format(cal.gettime())); }else { cal.add(calendar.minute,num); //system.out.println("date:"+df.format(cal.gettime())); } return cal.gettime(); } /***** * 时间 增加、减少 n个分钟以后时间 * @param date * yyyy-mm-dd hh:mm:ss * @param num>0 分钟 * @param type 增加和减少标志 * **/ public static date adjustdatebyminutes(date d ,integer num, int type) { calendar cal= calendar.getinstance(); dateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); cal.settime(d); if(type==0){ cal.add(calendar.minute,-num); // system.out.println("date:"+df.format(cal.gettime())); }else { cal.add(calendar.minute,num); // system.out.println("date:"+df.format(cal.gettime())); } return cal.gettime(); } public static void main(string[] args){ // string datestr = dateutil.yearthdate("2017-05-30"); // system.out.println(datestr); // long min = dateutil.timesub("2017-04-12 00:00:00", "2017-04-13 00:00:00")/60; // system.out.println(min); string settlementdate = dateutil.datetostring(new date(), "yyyy-mm-dd"); long day = dateutil.daydiff(dateutil.stringtodate("2017-06-22", "yyyy-mm-dd"),dateutil.stringtodate(settlementdate, "yyyy-mm-dd")); if(day >= 0){ system.out.println(day); } string goodsarrivetime = "2017-04-02 17:00-18:00"; int space_index = goodsarrivetime.indexof(" "); string arrive_date = goodsarrivetime.substring(0, space_index); string arrive_time = goodsarrivetime.substring(space_index+1, goodsarrivetime.length()); system.out.println(arrive_date); system.out.println(arrive_time); string arrive_start_time = arrive_time.substring(0, 2); string arrive_end_time = arrive_time.substring(6,8); system.out.println(arrive_start_time); system.out.println(arrive_end_time); string time = dateutil.getcurrdate("hh"); system.out.println(time); string time2 = dateutil.getcurrdate("mm"); system.out.println(time2); } }
import java.security.messagedigest; public class md5util { private static string bytearraytohexstring(byte b[]) { stringbuffer resultsb = new stringbuffer(); for (int i = 0; i < b.length; i++) resultsb.append(bytetohexstring(b[i])); return resultsb.tostring(); } private static string bytetohexstring(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexdigits[d1] + hexdigits[d2]; } public static string md5encode(string origin, string charsetname) { string resultstring = null; try { resultstring = new string(origin); messagedigest md = messagedigest.getinstance("md5"); if (charsetname == null || "".equals(charsetname)) resultstring = bytearraytohexstring(md.digest(resultstring .getbytes())); else resultstring = bytearraytohexstring(md.digest(resultstring .getbytes(charsetname))); } catch (exception exception) { } return resultstring; } private static final string hexdigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; } import org.apache.http.consts; import org.apache.http.httpentity; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httppost; import org.apache.http.conn.ssl.sslconnectionsocketfactory; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.apache.http.ssl.sslcontexts; import org.apache.http.util.entityutils; import org.jdom.document; import org.jdom.element; import org.jdom.jdomexception; import org.jdom.input.saxbuilder; import javax.net.ssl.sslcontext; import javax.servlet.http.httpservletrequest; import java.io.*; import java.net.connectexception; import java.net.httpurlconnection; import java.net.url; import java.security.keystore; import java.util.*; public class paycommonutil { //微信参数配置 public static string api_key = configmanager.getinstance().getconfigitem("api_key"); //随机字符串生成 public static string getrandomstring(int length) { //length表示生成字符串的长度 string base = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"; random random = new random(); stringbuffer sb = new stringbuffer(); for (int i = 0; i < length; i++) { int number = random.nextint(base.length()); sb.append(base.charat(number)); } return sb.tostring(); } //请求xml组装 public static string getrequestxml(sortedmap<string,object> parameters){ stringbuffer sb = new stringbuffer(); sb.append("<xml>"); set es = parameters.entryset(); iterator it = es.iterator(); while(it.hasnext()) { map.entry entry = (map.entry)it.next(); string key = (string)entry.getkey(); string value = (string)entry.getvalue(); if ("attach".equalsignorecase(key)||"body".equalsignorecase(key)||"sign".equalsignorecase(key)) { sb.append("<"+key+">"+"<![cdata["+value+"]]></"+key+">"); }else { sb.append("<"+key+">"+value+"</"+key+">"); } } sb.append("</xml>"); return sb.tostring(); } //生成签名 public static string createsign(string characterencoding,sortedmap<string,object> parameters){ stringbuffer sb = new stringbuffer(); set es = parameters.entryset(); iterator it = es.iterator(); while(it.hasnext()) { map.entry entry = (map.entry)it.next(); string k = (string)entry.getkey(); object v = entry.getvalue(); if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + api_key); system.out.println(sb.tostring()); string sign = md5util.md5encode(sb.tostring(), characterencoding).touppercase(); return sign; } /** * 验证回调签名 * @return */ public static boolean istenpaysign(map<string, string> map) { string characterencoding="utf-8"; string charset = "utf-8"; string signfromapiresponse = map.get("sign"); if (signfromapiresponse == null || signfromapiresponse.equals("")) { system.out.println("api返回的数据签名数据不存在,有可能被第三方篡改!!!"); return false; } system.out.println("服务器回包里面的签名是:" + signfromapiresponse); //过滤空 设置 treemap sortedmap<string,string> packageparams = new treemap(); for (string parameter : map.keyset()) { string parametervalue = map.get(parameter); string v = ""; if (null != parametervalue) { v = parametervalue.trim(); } packageparams.put(parameter, v); } stringbuffer sb = new stringbuffer(); set es = packageparams.entryset(); iterator it = es.iterator(); while(it.hasnext()) { map.entry entry = (map.entry)it.next(); string k = (string)entry.getkey(); string v = (string)entry.getvalue(); if(!"sign".equals(k) && null != v && !"".equals(v)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + api_key); //将api返回的数据根据用签名算法进行计算新的签名,用来跟api返回的签名进行比较 //算出签名 string resultsign = ""; string tobesign = sb.tostring(); if (null == charset || "".equals(charset)) { resultsign = md5util.md5encode(tobesign, characterencoding).touppercase(); }else{ try{ resultsign = md5util.md5encode(tobesign, characterencoding).touppercase(); }catch (exception e) { resultsign = md5util.md5encode(tobesign, characterencoding).touppercase(); } } string tenpaysign = ((string)packageparams.get("sign")).touppercase(); return tenpaysign.equals(resultsign); } //请求方法 public static string httpsrequest(string requesturl, string requestmethod, string outputstr) { try { url url = new url(requesturl); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setdoinput(true); conn.setusecaches(false); // 设置请求方式(get/post) conn.setrequestmethod(requestmethod); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); // 当outputstr不为null时向输出流写数据 if (null != outputstr) { outputstream outputstream = conn.getoutputstream(); // 注意编码格式 outputstream.write(outputstr.getbytes("utf-8")); outputstream.close(); } // 从输入流读取返回内容 inputstream inputstream = conn.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8"); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); string str = null; stringbuffer buffer = new stringbuffer(); while ((str = bufferedreader.readline()) != null) { buffer.append(str); } // 释放资源 bufferedreader.close(); inputstreamreader.close(); inputstream.close(); inputstream = null; conn.disconnect(); return buffer.tostring(); } catch (connectexception ce) { system.out.println("连接超时:{}"+ ce); } catch (exception e) { system.out.println("https请求异常:{}"+ e); } return null; } //退款的请求方法 public static string httpsrequest2(string requesturl, string requestmethod, string outputstr) throws exception { keystore keystore = keystore.getinstance("pkcs12"); stringbuilder res = new stringbuilder(""); fileinputstream instream = new fileinputstream(new file("/home/apiclient_cert.p12")); try { keystore.load(instream, "".tochararray()); } finally { instream.close(); } // trust own ca and all self-signed certs sslcontext sslcontext = sslcontexts.custom() .loadkeymaterial(keystore, "1313329201".tochararray()) .build(); // allow tlsv1 protocol only sslconnectionsocketfactory sslsf = new sslconnectionsocketfactory( sslcontext, new string[] { "tlsv1" }, null, sslconnectionsocketfactory.browser_compatible_hostname_verifier); closeablehttpclient httpclient = httpclients.custom() .setsslsocketfactory(sslsf) .build(); try { httppost httpost = new httppost("https://api.mch.weixin.qq.com/secapi/pay/refund"); httpost.addheader("connection", "keep-alive"); httpost.addheader("accept", "*/*"); httpost.addheader("content-type", "application/x-www-form-urlencoded; charset=utf-8"); httpost.addheader("host", "api.mch.weixin.qq.com"); httpost.addheader("x-requested-with", "xmlhttprequest"); httpost.addheader("cache-control", "max-age=0"); httpost.addheader("user-agent", "mozilla/4.0 (compatible; msie 8.0; windows nt 6.0) "); stringentity entity2 = new stringentity(outputstr , consts.utf_8); httpost.setentity(entity2); system.out.println("executing request" + httpost.getrequestline()); closeablehttpresponse response = httpclient.execute(httpost); try { httpentity entity = response.getentity(); system.out.println("----------------------------------------"); system.out.println(response.getstatusline()); if (entity != null) { system.out.println("response content length: " + entity.getcontentlength()); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(entity.getcontent())); string text = ""; res.append(text); while ((text = bufferedreader.readline()) != null) { res.append(text); system.out.println(text); } } entityutils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } return res.tostring(); } //xml解析 public static map doxmlparse(string strxml) throws jdomexception, ioexception { strxml = strxml.replacefirst("encoding=\".*\"", "encoding=\"utf-8\""); if(null == strxml || "".equals(strxml)) { return null; } map m = new hashmap(); inputstream in = new bytearrayinputstream(strxml.getbytes("utf-8")); saxbuilder builder = new saxbuilder(); document doc = builder.build(in); element root = doc.getrootelement(); list list = root.getchildren(); iterator it = list.iterator(); while(it.hasnext()) { element e = (element) it.next(); string k = e.getname(); string v = ""; list children = e.getchildren(); if(children.isempty()) { v = e.gettextnormalize(); } else { v = getchildrentext(children); } m.put(k, v); } //关闭流 in.close(); return m; } public static string getchildrentext(list children) { stringbuffer sb = new stringbuffer(); if(!children.isempty()) { iterator it = children.iterator(); while(it.hasnext()) { element e = (element) it.next(); string name = e.getname(); string value = e.gettextnormalize(); list list = e.getchildren(); sb.append("<" + name + ">"); if(!list.isempty()) { sb.append(getchildrentext(list)); } sb.append(value); sb.append("</" + name + ">"); } } return sb.tostring(); } public static string getremotehost(httpservletrequest request){ string ip = request.getheader("x-forwarded-for"); if(ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)){ ip = request.getheader("proxy-client-ip"); } if(ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)){ ip = request.getheader("wl-proxy-client-ip"); } if(ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)){ ip = request.getremoteaddr(); } return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip; } }
package com.lemonjr.api.utils; import java.util.map; import java.util.regex.matcher; import java.util.regex.pattern; public class stringutil { /** * 数值类型前面补零(共13位) * @param num * @return */ public static string supplementzerogeneratethirteen(int num){ string str = string.format("%013d", num); return str; } /** * 数值类型前面补零(共16位) * @param num * @return */ public static string supplementzerogeneratesixteen(int num){ string str = string.format("%016d", num); return str; } /** * 数值类型前面补零(共3位) * @param num * @return */ public static string supplementzerogeneratethree(int num){ string str = string.format("%03d", num); return str; } /** * 判断字符串是不是double型 * @param str * @return */ public static boolean isnumeric(string str){ pattern pattern = pattern.compile("[0-9]+[.]{0,1}[0-9]*[dd]{0,1}"); matcher isnum = pattern.matcher(str); if( !isnum.matches() ){ return false; } return true; } public static string trim(string str, boolean nullflag){ string tempstr = null; if (str != null) { tempstr = str.trim(); } if (nullflag) { if ("".equals(tempstr) || "null".equals(tempstr)) { tempstr = null; } } else { if (tempstr == null) { tempstr = ""; } } return tempstr; } public static string replace(string strsource, string strfrom, string strto) { if(strsource==null){ return null; } int i = 0; if ((i = strsource.indexof(strfrom, i)) >= 0) { char[] csrc = strsource.tochararray(); char[] cto = strto.tochararray(); int len = strfrom.length(); stringbuffer buf = new stringbuffer(csrc.length); buf.append(csrc, 0, i).append(cto); i += len; int j = i; while ((i = strsource.indexof(strfrom, i)) > 0) { buf.append(csrc, j, i - j).append(cto); i += len; j = i; } buf.append(csrc, j, csrc.length - j); return buf.tostring(); } return strsource; } public static string deal(string str) { str = replace(str, "\\", "\\\\"); str = replace(str, "'", "\\'"); str = replace(str, "\r", "\\r"); str = replace(str, "\n", "\\n"); str = replace(str, "\"", "\\\""); return str; } public static string getmaptoxml(map<string,string> param){ stringbuffer sb = new stringbuffer(); sb.append("<xml>"); for (map.entry<string,string> entry : param.entryset()) { sb.append("<"+ entry.getkey() +">"); sb.append(entry.getvalue()); sb.append("</"+ entry.getkey() +">"); } sb.append("</xml>"); return sb.tostring(); } public static void main(string[] args){ //string a = stringutil.supplementzerogeneratethirteen(1000); double a = 32.; system.out.println(stringutil.isnumeric("32.")); system.out.println(a); } }
4.用到的jar包
<!--微信 --> <dependency> <groupid>com.github.liyiorg</groupid> <artifactid>weixin-popular</artifactid> <version>2.8.5</version> </dependency> <!--httpclient--> <dependency> <groupid>commons-httpclient</groupid> <artifactid>commons-httpclient</artifactid> <version>3.1</version> </dependency> <dependency> <groupid>org.jdom</groupid> <artifactid>jdom2</artifactid> <version>2.0.6</version> </dependency>
以上所述是小编给大家介绍的app微信支付(java后台_统一下单和回调)详解整合,希望对大家有所帮助
上一篇: Day08_11练习04