微信支付(JSAPI) - Java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信支付(JSAPI) - Java相关的知识,希望对你有一定的参考价值。
最近客栈订房,当然是需要微信支付的,官方微信支付文档什么的,只想说 去你妈的文档
so, 经过两天不懈的努力和网上大牛文档,终于做出来了,下面具体来说说,希望能对各位看客有点帮助
放大招,直接上代码
1. Configure.java 文件
1 package com.kzb.website.core.wechat.common; 2 3 import java.util.Calendar; 4 import java.util.Date; 5 6 import org.apache.commons.lang3.StringUtils; 7 8 public class Configure { 9 10 private static String token = null; 11 private static Date tokenTime = null; 12 private static String jsapiTicket = null; 13 private static Date jsapiTicketTime = null; 14 15 public static String MD5 = "MD5"; 16 public static String EMPTY = ""; 17 public static String SUCCESS = "SUCCESS"; 18 public static String HEX_FORMAT = "%02x"; 19 public static String TRADE_TYPE = "JSAPI"; 20 public static String MIDDLE_LINE = "-"; 21 public static String CHARTSET_UTF8 = "UTF-8"; 22 23 public static String NOTIFY_SUCCESS = "<xml>\\n<return_code><![CDATA[SUCCESS]]></return_code>\\n<return_msg><![CDATA[OK]]></return_msg>\\n</xml>"; 24 public static String NOTIFY_FAIL = "<xml>\\n<return_code><![CDATA[FAIL]]></return_code>\\n<return_msg><![CDATA[ERROR]]></return_msg>\\n</xml>"; 25 26 private static String key = "API密钥(32位,在微信商户平台下的API安全栏下)"; 27 // 微信分配的公众号ID(开通公众号之后可以获取到) 28 private static String appID = "XXXXXXXXXXXXXX"; 29 private static String appSecret = "XXXXXXXXXXXXXX"; 30 // 微信支付分配的商户号ID(开通公众号的微信支付功能之后可以获取到) 31 private static String mchID = "1349019501"; 32 // 机器IP 33 private static String ip = "127.0.0.1"; 34 35 // 以下是几个API的路径: 36 // 统一下单 37 public static String UNIFIEDORDER_API = "https://api.mch.weixin.qq.com/pay/unifiedorder"; 38 // access_token API 39 public static String TOKEN_API = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; 40 // 临时票据API 41 public static String TICKET_API = "https://api.weixin.qq.com/cgi-bin/ticket/getticket"; 42 // 微信OPENID API 43 public static String OPENID_API = "https://api.weixin.qq.com/sns/oauth2/access_token"; 44 45 public static void setKey(String key) { 46 Configure.key = key; 47 } 48 49 public static void setAppID(String appID) { 50 Configure.appID = appID; 51 } 52 53 public static void setMchID(String mchID) { 54 Configure.mchID = mchID; 55 } 56 57 public static void setIp(String ip) { 58 Configure.ip = ip; 59 } 60 61 public static String getKey(){ 62 return key; 63 } 64 65 public static String getAppid(){ 66 return appID; 67 } 68 69 public static String getMchid(){ 70 return mchID; 71 } 72 73 public static String getIP(){ 74 return ip; 75 } 76 77 public static String getAppSecret() { 78 return appSecret; 79 } 80 81 public static void setAppSecret(String appSecret) { 82 Configure.appSecret = appSecret; 83 } 84 85 public static String getToken() { 86 return token; 87 } 88 89 public static void setToken(String token) { 90 Configure.token = token; 91 Configure.tokenTime = new Date(); 92 } 93 94 public static String getJsapiTicket() { 95 return jsapiTicket; 96 } 97 98 public static void setJsapiTicket(String jsapiTicket) { 99 Configure.jsapiTicket = jsapiTicket; 100 Configure.jsapiTicketTime = new Date(); 101 } 102 103 public static boolean checkToken() { 104 if (!StringUtils.isEmpty(Configure.token)) { 105 Calendar calendar = Calendar.getInstance(); 106 calendar.setTime(tokenTime); 107 calendar.add(Calendar.SECOND, 7200); 108 return calendar.before(new Date()); 109 } 110 return true; 111 } 112 113 public static boolean checkJsapiTicket() { 114 if (!StringUtils.isEmpty(Configure.jsapiTicket)) { 115 Calendar calendar = Calendar.getInstance(); 116 calendar.setTime(jsapiTicketTime); 117 calendar.add(Calendar.SECOND, 7200); 118 return calendar.before(new Date()); 119 } 120 return true; 121 } 122 }
2. SignMD5.java文件
1 package com.kzb.website.core.wechat.common; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 import java.util.Formatter; 7 import java.util.UUID; 8 9 import org.springframework.security.crypto.password.PasswordEncoder; 10 11 public class SignMD5 implements PasswordEncoder { 12 public static String byteToHex(final byte[] hash) { 13 Formatter formatter = new Formatter(); 14 for (byte b : hash) { 15 formatter.format(Configure.HEX_FORMAT, b); 16 } 17 String result = formatter.toString(); 18 formatter.close(); 19 return result; 20 } 21 22 @Override 23 public String encode(CharSequence charSequence) { 24 try { 25 MessageDigest crypt = MessageDigest.getInstance(Configure.MD5); 26 crypt.reset(); 27 crypt.update(charSequence.toString().getBytes(Configure.CHARTSET_UTF8)); 28 return byteToHex(crypt.digest()); 29 } catch (NoSuchAlgorithmException e) { 30 e.printStackTrace(); 31 } catch (UnsupportedEncodingException e) { 32 e.printStackTrace(); 33 } 34 return Configure.EMPTY; 35 } 36 37 @Override 38 public boolean matches(CharSequence charSequence, String encodedPassword) { 39 return encode(charSequence).equals(encodedPassword); 40 } 41 42 public String createNonceStr() { 43 return UUID.randomUUID().toString().replaceAll(Configure.MIDDLE_LINE,Configure.EMPTY); 44 } 45 46 public String createTimeStamp() { 47 return Long.toString(System.currentTimeMillis() / 1000); 48 } 49 50 }
3. WechatNotify.java文件
1 package com.kzb.website.core.wechat.common; 2 3 import java.io.Serializable; 4 5 import javax.xml.bind.annotation.XmlRootElement; 6 7 @XmlRootElement(name="xml") 8 public class WechatNotify implements Serializable{ 9 10 private static final long serialVersionUID = 8047707600433551023L; 11 private String appid; 12 private String attach; 13 private String bank_type; 14 private String fee_type; 15 private String is_subscribe; 16 private String mch_id; 17 private String nonce_str; 18 private String openid; 19 private String out_trade_no; 20 private String result_code; 21 private String return_code; 22 private String sign; 23 private String sub_mch_id; 24 private String time_end; 25 private String total_fee; 26 private String trade_type; 27 private String transaction_id; 28 29 public String getAppid() { 30 return appid; 31 } 32 33 public void setAppid(String appid) { 34 this.appid = appid; 35 } 36 37 public String getAttach() { 38 return attach; 39 } 40 41 public void setAttach(String attach) { 42 this.attach = attach; 43 } 44 45 public String getBank_type() { 46 return bank_type; 47 } 48 49 public void setBank_type(String bank_type) { 50 this.bank_type = bank_type; 51 } 52 53 public String getFee_type() { 54 return fee_type; 55 } 56 57 public void setFee_type(String fee_type) { 58 this.fee_type = fee_type; 59 } 60 61 public String getIs_subscribe() { 62 return is_subscribe; 63 } 64 65 public void setIs_subscribe(String is_subscribe) { 66 this.is_subscribe = is_subscribe; 67 } 68 69 public String getMch_id() { 70 return mch_id; 71 } 72 73 public void setMch_id(String mch_id) { 74 this.mch_id = mch_id; 75 } 76 77 public String getNonce_str() { 78 return nonce_str; 79 } 80 81 public void setNonce_str(String nonce_str) { 82 this.nonce_str = nonce_str; 83 } 84 85 public String getOpenid() { 86 return openid; 87 } 88 89 public void setOpenid(String openid) { 90 this.openid = openid; 91 } 92 93 public String getOut_trade_no() { 94 return out_trade_no; 95 } 96 97 public void setOut_trade_no(String out_trade_no) { 98 this.out_trade_no = out_trade_no; 99 } 100 101 public String getResult_code() { 102 return result_code; 103 } 104 105 public void setResult_code(String result_code) { 106 this.result_code = result_code; 107 } 108 109 public String getReturn_code() { 110 return return_code; 111 } 112 113 public void setReturn_code(String return_code) { 114 this.return_code = return_code; 115 } 116 117 public String getSign() { 118 return sign; 119 } 120 121 public void setSign(String sign) { 122 this.sign = sign; 123 } 124 125 public String getSub_mch_id() { 126 return sub_mch_id; 127 } 128 129 public void setSub_mch_id(String sub_mch_id) { 130 this.sub_mch_id = sub_mch_id; 131 } 132 133 public String getTime_end() { 134 return time_end; 135 } 136 137 public void setTime_end(String time_end) { 138 this.time_end = time_end; 139 } 140 141 public String getTotal_fee() { 142 return total_fee; 143 } 144 145 public void setTotal_fee(String total_fee) { 146 this.total_fee = total_fee; 147 } 148 149 public String getTrade_type() { 150 return trade_type; 151 } 152 153 public void setTrade_type(String trade_type) { 154 this.trade_type = trade_type; 155 } 156 157 public String getTransaction_id() { 158 return transaction_id; 159 } 160 161 public void setTransaction_id(String transaction_id) { 162 this.transaction_id = transaction_id; 163 } 164 }
4. WechatPayModel.java文件
1 package com.kzb.website.core.wechat.common; 2 3 import javax.xml.bind.annotation.XmlRootElement; 4 5 import org.springframework.security.crypto.password.PasswordEncoder; 6 7 @XmlRootElement(name = "XML") 8 public class WechatPayModel { 9 10 private String appid; 11 private String mch_id; 12 private String nonce_str; 13 private String body; 14 private String out_trade_no; 15 private String total_fee; 16 private String spbill_create_ip; 17 private String notify_url; 18 private String trade_type; 19 private String openid; 20 private String sign; 21 22 public String getAppid() { 23 return appid; 24 } 25 26 public void setAppid(String appid) { 27 this.appid = appid; 28 } 29 30 public String getMch_id() { 31 return mch_id; 32 } 33 34 public void setMch_id(String mch_id) { 35 this.mch_id = mch_id; 36 } 37 38 public String getNonce_str() { 39 return nonce_str; 40 } 41 42 public void setNonce_str(String nonce_str) { 43 this.nonce_str = nonce_str; 44 } 45 46 public String getOut_trade_no() { 47 return out_trade_no; 48 } 49 50 public void setOut_trade_no(String out_trade_no) { 51 this.out_trade_no = out_trade_no; 52 } 53 54 public String getTotal_fee() { 55 return total_fee; 56 } 57 58 public void setTotal_fee(String total_fee) { 59 this.total_fee = total_fee; 60 } 61 62 public String getSpbill_create_ip() { 63 return spbill_create_ip; 64 } 65 66 public void setSpbill_create_ip(String spbill_create_ip) { 67 this.spbill_create_ip = spbill_create_ip; 68 } 69 70 public String getNotify_url() { 71 return notify_url; 72 } 73 74 public void setNotify_url(String notify_url) { 75 this.notify_url = notify_url; 76 } 77 78 public String getTrade_type() { 79 return trade_type; 80 } 81 82 public void setTrade_type(String trade_type) { 83 this.trade_type = trade_type; 84 } 85 86 public String getBody() { 87 return body; 88 } 89 90 public void setBody(String body) { 91 this.body = body; 92 } 93 94 public String getSign() { 95 return sign; 96 } 97 98 public void setSign(String sign) { 99 this.sign = sign; 100 } 101 102 public String getOpenid() { 103 return openid; 104 } 105 106 public void setOpenid(String openid) { 107 this.openid = openid; 108 } 109 110 public void sign(PasswordEncoder encoder) { 111 StringBuilder stringBuilder = new StringBuilder(); 112 stringBuilder.append("appid=").append(getAppid()); 113 stringBuilder.append("&body=").append(getBody()); 114 stringBuilder.append("&mch_id=").append(getMch_id()); 115 stringBuilder.append("&nonce_str=").append(getNonce_str()); 116 stringBuilder.append("¬ify_url=").append(getNotify_url()); 117 stringBuilder.append("&openid=").append(getOpenid()); 118 stringBuilder.append("&out_trade_no=").append(getOut_trade_no()); 119 stringBuilder.append("&spbill_create_ip=").append(getSpbill_create_ip()); 120 stringBuilder.append("&total_fee=").append(getTotal_fee()); 121 stringBuilder.append("&trade_type=").append(getTrade_type()); 122 stringBuilder.append("&key=").append(Configure.getKey()); 123 this.sign = encoder.encode(stringBuilder.toString()); 124 } 125 126 @Override 127 public String toString() { 128 return "<xml>" + 129 "<appid><![CDATA[" + appid + "]]></appid>" + 130 "<body><![CDATA[" + body + "]]></body>" + 131 "<mch_id><![CDATA[" + mch_id + "]]></mch_id>" + 132 "<nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>" + 133 "<notify_url><![CDATA[" + notify_url + "]]></notify_url>" + 134 "<openid><![CDATA[" + openid + "]]></openid>" + 135 "<out_trade_no><![CDATA[" + out_trade_no + "]]></out_trade_no>" + 136 "<spbill_create_ip><![CDATA[" + spbill_create_ip + "]]></spbill_create_ip>" + 137 "<trade_type><![CDATA[" + trade_type + "]]></trade_type>" + 138 "<total_fee><![CDATA[" + total_fee + "]]></total_fee>" + 139 "<sign><![CDATA[" + sign + "]]></sign>" + 140 "</xml>"; 141 } 142 }
5. WechatUtil.java文件
1 package com.kzb.website.core.wechat.common; 2 3 import java.io.DataInputStream; 4 import java.io.IOException; 5 6 import javax.servlet.http.HttpServletRequest; 7 8 import com.kzb.website.core.utils.XMLUtil; 9 10 public class WechatUtil { 11 12 /** 13 * 微信回调成功后 将 xml 转换为 WechatNotify 对象 14 * 15 * @param request 16 * @return WechatNotify 对象 17 */ 18 public static WechatNotify getNotifyBean(HttpServletRequ
以上是关于微信支付(JSAPI) - Java的主要内容,如果未能解决你的问题,请参考以下文章