网上花积分下载下来的,代码结构在查询订单的时候写的不是很好,可以按照自己的实际情况进行修改
微信官方文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
wechatscan.java
package com.demo;
import java.net.ConnectException;
import java.util.Map;
import java.util.TreeMap;
import com.demo.dao.PayDao;
import com.demo.dao.QueryDao;
import com.demo.utils.HttpUtil;
import com.demo.utils.XMLUtil;
public class wechatscan {
public static String appid = "";
public static String sub_appid = "";
public static String mch_id = "";
public static String device_info = "";
public static String nonce_str = "";
public static String sign = "";//签名
//public static String sign_type = ""; //默认MD5
public static String body = "";
public static String detail = "";
public static String attach = "";
public static String out_trade_no = "";//日期+4位随机数
public static String total_fee = "";
public static String fee_type = "";
public static String spbill_create_ip = "";
public static String goods_tag = "";
public static String auth_code = "";
public static String sub_mch_id = "";
public static String key = "0074ce7121374489bf625488d429d5e3";
public static void scanpay(PayDao pd) throws Exception {
appid = pd.getAppid()==null?"":pd.getAppid();
sub_appid = pd.getSub_appid()==null?"":pd.getSub_appid();
mch_id = pd.getMch_id()==null?"":pd.getMch_id();
device_info = pd.getDevice_info()==null?"":pd.getDevice_info();
String currTime = Method.getCurrTime();
String time_start = currTime.substring(8, currTime.length());
String strRandom = Method.buildRandom(4) + "";
nonce_str = time_start + strRandom; //随机数生成算法
sign = "";//签名
body = pd.getBody()==null?"":pd.getBody();
detail = pd.getDetail()==null?"":pd.getDetail();
attach = pd.getAttach()==null?"":pd.getAttach();
String time = currTime.substring(0, currTime.length());
out_trade_no = time+strRandom;//日期+4位随机数
total_fee = pd.getTotal_fee()==null?"0":pd.getTotal_fee();
fee_type = pd.getFee_type()==null?"":pd.getFee_type();
spbill_create_ip = Method.localIp();
goods_tag = pd.getGoods_tag()==null?"":pd.getGoods_tag();
auth_code = pd.getAuth_code()==null?"":pd.getAuth_code();
sub_mch_id = pd.getSub_mch_id()==null?"":pd.getSub_mch_id();
String MICROPAY_URL = "https://api.mch.weixin.qq.com/pay/micropay";
Map<Object, String> map = new TreeMap<Object, String>();
map.put("appid", appid);
map.put("sub_appid", sub_appid);
map.put("mch_id", mch_id);
map.put("device_info", device_info);
map.put("nonce_str", nonce_str);
map.put("body", body);
//map.put("detail", detail);
map.put("attach", attach);
map.put("out_trade_no", out_trade_no);
map.put("fee_type", fee_type);
map.put("total_fee", total_fee);
map.put("spbill_create_ip", spbill_create_ip);
map.put("auth_code", auth_code);
//map.put("goods_tag", goods_tag);
map.put("sub_mch_id", sub_mch_id);
sign = Method.createSign("UTF-8", map,key);
map.put("sign", sign);
String requestXML = Method.getRequestXml(map);
Map map1 = null;
try{
String resXml = HttpUtil.postData(MICROPAY_URL, requestXML);
map1 = XMLUtil.doXMLParse(resXml);
}catch (ConnectException ce){
System.out.println("连接超时:"+ce.getMessage());
}catch (Exception e){
System.out.println("https请求异常:"+e.getMessage());
}
System.out.println("SCAN requestXML::::"+requestXML);
System.out.println("SCAN resXml::::"+HttpUtil.postData(MICROPAY_URL, requestXML));
QueryDao qd = new QueryDao();
qd.setAppid(appid);
qd.setMch_id(mch_id);
qd.setOut_trade_no(out_trade_no);
qd.setSub_mch_id(sub_mch_id);
int querycount = 10;
boolean booleanquery = false;
while(querycount>0){
if(wechatquery.orderquery(qd)!=true && querycount>0){
querycount = querycount - 1;
booleanquery = false;
}else{
querycount = 0;
booleanquery = true;
}
Thread.sleep(500);
}
if(booleanquery==false&&querycount==0){
wechatcancel.ordercancel(qd);
}
}
}
wechatquery.java(查询订单)
package com.demo;
import java.net.ConnectException;
import java.util.Map;
import java.util.TreeMap;
import com.demo.dao.QueryDao;
import com.demo.utils.HttpUtil;
import com.demo.utils.XMLUtil;
public class wechatquery {
public static boolean orderquery(QueryDao qd) throws Exception {
int successcode = 0; //0 success 1 fail
String appid = qd.getAppid()==null?"":qd.getAppid();
String sub_appid = qd.getSub_appid()==null?"":qd.getSub_appid();
String mch_id = qd.getMch_id()==null?"":qd.getMch_id();
String currTime = Method.getCurrTime();
String time_start = currTime.substring(8, currTime.length());
String strRandom = Method.buildRandom(4) + "";
String nonce_str = time_start + strRandom; //随机数生成算法
String sign = "";//签名
//String sign_type = ""; //默认MD5
String out_trade_no = qd.getOut_trade_no()==null?"":qd.getOut_trade_no();//商户系统内部的订单号,微信订单号、商户订单号二选一必传
String sub_mch_id = qd.getSub_mch_id()==null?"":qd.getSub_mch_id();
String transaction_id = qd.getTransaction_id()==null?"":qd.getTransaction_id();
String QUERY_URL = "https://api.mch.weixin.qq.com/pay/orderquery";
String key = "0074ce7121374489bf625488d429d5e3";
Map<Object, String> map = new TreeMap<Object, String>();
map.put("appid", appid);
map.put("sub_appid", sub_appid);
map.put("mch_id", mch_id);
map.put("nonce_str", nonce_str);
//map.put("sign_type", sign_type);
map.put("out_trade_no", out_trade_no);
map.put("sub_mch_id", sub_mch_id);
map.put("transaction_id", transaction_id);
sign = Method.createSign("UTF-8", map,key);
map.put("sign", sign);
String requestXML = Method.getRequestXml(map);
Map map1 = null;
try{
String resXml = HttpUtil.postData(QUERY_URL, requestXML);
map1 = XMLUtil.doXMLParse(resXml);
System.out.println(map1.get("result_code"));
}catch (ConnectException ce){
System.out.println("连接超时:"+ce.getMessage());
}catch (Exception e){
System.out.println("https请求异常:"+e.getMessage());
}
System.out.println("QUERY requestXML::::"+requestXML);
System.out.println("QUERY resXml::::"+HttpUtil.postData(QUERY_URL, requestXML));
if(map1.get("return_code").equals("SUCCESS")&& map1.get("result_code").equals("SUCCESS")){
if(map1.get("trade_state").equals("SUCCESS")){
successcode = 0;
}else if(map1.get("trade_state").equals("USERPAYING")){
successcode = 1;
}
}else{
successcode = 1;
}
if(successcode == 0){
return true;
}else{
return false;
}
}
}
wechatcancel.java(取消订单)
package com.demo;
import java.net.ConnectException;
import java.util.Map;
import java.util.TreeMap;
import com.demo.dao.QueryDao;
import com.demo.utils.ClientCustomSSL;
import com.demo.utils.HttpUtil;
import com.demo.utils.XMLUtil;
public class wechatcancel {
public static void ordercancel(QueryDao qd) throws Exception {
String appid = qd.getAppid()==null?"":qd.getAppid();
String mch_id = qd.getMch_id()==null?"":qd.getMch_id();
String out_trade_no = qd.getOut_trade_no()==null?"":qd.getOut_trade_no();//商户系统内部的订单号,微信订单号、商户订单号二选一必传
String sub_mch_id = qd.getSub_mch_id()==null?"":qd.getSub_mch_id();
String currTime = Method.getCurrTime();
String time_start = currTime.substring(8, currTime.length());
String strRandom = Method.buildRandom(4) + "";
String nonce_str = time_start + strRandom; //随机数生成算法
String sign = "";//签名
//String QUERY_URL = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
String key = "0074ce7121374489bf625488d429d5e3";
Map<Object, String> map = new TreeMap<Object, String>();
map.put("appid", appid);
map.put("mch_id", mch_id);
map.put("out_trade_no", out_trade_no);
map.put("sub_mch_id", sub_mch_id);
map.put("nonce_str", nonce_str);
sign = Method.createSign("UTF-8", map,key);
map.put("sign", sign);
String requestXML = Method.getRequestXml(map);
ClientCustomSSL clientCustomSSL = new ClientCustomSSL();
clientCustomSSL.clientcustomSSL(requestXML);
System.out.println("Cancel resXml::::");
}
}
完整源码百度云地址:https://pan.baidu.com/s/1EJ_W1NknNdJLx_s7MMLeBg