首先微信支付需要以下的字段,关于如何去获取微信支付的字段,网上一大把,注意(像商户号等重要字段不要泄露不要泄露)
MCHID(商户号) 就是商户注册之后,微信支付给你的唯一的数字
APPID(小程序的appid)
SSLFILE(微信支付需要申请证书,这个字段就是把申请的证书下载之后,存放在你服务器的某个文件夹的路径)
NOTIFYURL(支付成功之后,微信会给你这个url发送一条支付成功的消息)
APIKEY(证书的秘钥)文章来源:https://www.toymoban.com/news/detail-486235.html
需要微信支付的jar包文章来源地址https://www.toymoban.com/news/detail-486235.html
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.4.7</version>
</dependency>
2.开始微信支付的demo,首先看微信支付调用支付需要那些必传字段(注意顺序),可以通过这个地址查看https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
3.关于签名加密的方法(后续也会使用到)
public class MD5Util {
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public 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 ) {
return MD5Encode(origin,null);
}
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;
}
}
4.将集合的数据进行xml处理
public static String getRequest(TreeMap<String, String> parameters)
throws Exception { //
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 k = (String) entry.getKey();
String v = (String) entry.getValue();
if ("mch_id".equalsIgnoreCase(k) || "nonce_str".equalsIgnoreCase(k)
|| "sign".equalsIgnoreCase(k)) {
sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
} else {
sb.append("<" + k + ">" + v + "</" + k + ">");
}
}
sb.append("</xml>");
return sb.toString();
}
5.将加密的数据通过url发送给微信支付,成功之后微信支付会将需要的数据返回给你,这里的url是:
6.将微信支付返回的xml数据解析成集合,然后通过集合的key获取到值组装发给前端,大致流程就是这样的了。
//xml解析
public static TreeMap doXMLParse(String strxml) throws JDOMException, IOException {
strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
if(null == strxml || "".equals(strxml)) {
return null;
}
TreeMap m = new TreeMap();
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();
}
7.后续将字段返回给前端之后,前端进行调用,微信支付成功之后,会将支付回调返回给你controller里面的方法,支付成功之后里面进行业务逻辑处理.
总结:关于V2版本的微信支付的难点主要是在于对xml进行解析,以及对证书的验证处理的方法,后续会出支付宝支付的流程,请大家关注我,不会让大家失望的。
到了这里,关于java微信小程序支付V2版本(亲测有效)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!