步骤:
1、扫码 获取被扫对象微信支付二维码 获得支付授权码 即微信钱包→付款 里面那个条形码上面的那一串数字
2、把授权码及所需要的参数传给提交订单api
3、用查询订单api查询订单付款情况
4、用while循环重复查询10次 如果都是交易失败就调用撤销订单api
用到的jar包:
(注意项目里面尽量让这三个包httpclient和httpcore还有httpmime版本如图匹配好 而且这三个包在项目里面都只能出现一个版本 如:httpclient-4.2.4和httpclient-4.3.4都有的情况下 只能保留一个)
wechatscan.java
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 = "645yh45y45y45y45y45y45y45y45y5445y";
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);
}
}
}
如果是服务商的话 wechatscan里面的sub_mch_id一定需要有 sub_appid就可能用到
wechatquery.java
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 = "645yh45y45y45y45y45y45y45y45y5445y";
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;
}
}
}
这个是查询订单的 wechatscan把获取的数据发到微信服务器之后 就要用这个类来对订单状态查询 然后决定后面应该是交易成功好似撤销订单
wechatcancel.java
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 = "645yh45y45y45y45y45y45y45y45y5445y";
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::::");
}
}
最后这个是撤销订单的类 这里要注意的是 需要微信支付平台上申请退款权限 获得一个证书 java的话只用到apiclient_cert.p12 其他语言就需要用到所有证书了 具体怎么用证书就是写在ClientCustomSSL里面 这个东西微信支付官方例子里面有 直接下载官方demo 就能找到一个ClientCustomSSL.java的文件 这个基本上直接搬项目就好了
注意事项:
1、金额是以分为单位的 而且不能有小数点
2、注意开发文档里面写的 哪一个是必传参数 就算没什么用的 也不能传空白
3、注意key 这个key是微信支付平台账号还是微信公众号平台上能找到的 是用来生成签名用的
4、查询和扫码这些不需要证书的 基本上写法都一样 不过遇到退款的 写法就会有改变 不只要生成xml传给微信服务器 还要对证书解释 一起传微信服务器才行
5、对应证书 java只用到.p12那个 不过写php或者asp的时候 就要把所有的都用上
6、clientcustomSSL里面要配置一下mchid