具体实现代码,工具类就懒得贴出来了,调用下自己的就行了,主要是几种价格,价格一定要正确,如果价格不正确校验是不通过的
package wx;
import com.derbysoft.easygo.utils.*;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
/**
* Created by DT167 on 2017/7/24.
*/
public class FreeReCharge {
private String getSandBoxKeyUrl= "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
private static String parntKey = "derbysoftwhtravelderbysoftcomaaa";
// private static String sandParntkey = "";//商户
//private static String sandParntkey = "";//服务商
private static String sandParntkey = "";
// private static String mchId = "1337465801";
// private static String mchId = "";
private static String mchId = "";
private String nonceStr = "";
private String sign = "";
private String filePath = "C:\\Users\\DT167\\Desktop\\derby\\cert2\\apiclient_cert.p12";
private static String create_nonce_str() {
return UUID.randomUUID().toString().replace("-","");
}
/**
* 获取沙箱密钥
* a7dc4cb5fcac7fa42bc98b3320523801
* @return
*/
public String getSandboxnew(){
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
String result = null;
nonceStr =create_nonce_str();
String json = "";
HashMap<String,String> signMap =new HashMap<String,String>();
try{
signMap.put("mch_id",mchId);
signMap.put("nonce_str",nonceStr);
signMap.put("sign",helper.getSign(signMap,parntKey));
String parm = CustomClientSSL.mapToXML(CustomClientSSL.arrayHashMapToList(signMap), false);
result = RequestUtils.sendPostRequest(getSandBoxKeyUrl, parm);
//a7dc4cb5fcac7fa42bc98b3320523801
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/**
* 查询沙箱订单
* @param orderNo
* @return
*/
public String querySand(String orderNo){
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
String result = null;
nonceStr =create_nonce_str();
String json = "";
HashMap<String,String> signMap =new HashMap<String,String>();
try{
signMap.put("appid","wx7d0b728b9b412e2b");
signMap.put("mch_id",mchId);
signMap.put("out_trade_no",orderNo);
signMap.put("nonce_str",nonceStr);
signMap.put("sign_type","MD5");
signMap.put("sign",helper.getSign(signMap,sandParntkey));
String parm = CustomClientSSL.mapToXML(CustomClientSSL.arrayHashMapToList(signMap), false);
result = RequestUtils.sendPostRequest(ScanPayUtil.PAY_QUERY_API, parm);
//a7dc4cb5fcac7fa42bc98b3320523801
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void test033(String orderNo,String money) throws Exception {
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
String bizPayUrl = helper.getBizPayUrl("豪华大床房", orderNo, money);
System.out.print(bizPayUrl);
}
public String refundWeXin(String out_trade_no,String out_refund_no,String total_fee,String refund_fee,String refund_desc,String refund_account){
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
try {
String reuslt = helper.refundWeXin(out_trade_no,null,out_refund_no,total_fee,refund_fee,null,null,filePath);
return reuslt;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String scanPay(String sign_type,String body,String out_trade_no,String total_fee,String spbill_create_ip,String auth_code){
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
try {
String reuslt = helper.scanPay(null,body,out_trade_no,total_fee,spbill_create_ip,auth_code);
return reuslt;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String queryRefundWeXin(String out_trade_no,String transaction_id,String out_refund_no,String refund_id){
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
try {
String reuslt = helper.queryRefundWeXin(out_trade_no,transaction_id ,out_refund_no,refund_id);
return reuslt;
} catch (Exception e){
e.printStackTrace();
}
return null;
}
public String downloadBill(Date bill_date, String bill_type, String sign_type){
WxPayHelperTest helper = new WxPayHelperTest("wx7d0b728b9b412e2b", sandParntkey, mchId);
try {
String reuslt = helper.downloadBill(bill_date,bill_type,null);
return reuslt;
} catch (Exception e){
e.printStackTrace();
}
return null;
}
//https://pay.weixin.qq.com/wiki/doc/api/download/mczyscsyl.pdf
//https://pay.weixin.qq.com/wiki/doc/api/download/ptsh_mczjksj.pdf
//不同的验证,需要不同的价格,才能验证通过
public static void main(String[] args) throws Exception {
String orderNo=create_nonce_str();
//4c28813260cf42979047883151c6be2d
System.out.println("orderNo:"+orderNo);
String result = null;
FreeReCharge freeReCharge =new FreeReCharge();
//获取沙箱密码
result = freeReCharge.getSandboxnew();
sandParntkey = result;
System.out.println(result);
//付款,付款价格流程
System.out.println("=========付款=======");
freeReCharge.test033(orderNo,"5.51");
//付款,退款流程
System.out.println("=========付款=======");
freeReCharge.test033(orderNo,"5.52");
//查询
System.out.println("=========查询=======");
result = freeReCharge.querySand(orderNo);
System.out.println(result);
//退款
System.out.println("=========退款=======");
result = freeReCharge.refundWeXin(orderNo,create_nonce_str(),"552","551",null,null);
System.out.println(result);
//查询退款
System.out.println("=========查询退款=======");
result = freeReCharge.queryRefundWeXin(orderNo,null,null,null);
System.out.println(result);
//下载对账单
/*result = freeReCharge.downloadBill(new Date(),"REFUND",null);
System.out.println(result);*/
//刷卡支付,付款流程价格
/* result = freeReCharge.scanPay(null,"xxx-超市",orderNo,"501","8.8.8.8","134972693138721937");
System.out.println(result);*/
//刷卡支付,退款流程价格
result = freeReCharge.scanPay(null,"xxxxx-超市",orderNo,"502","8.8.8.8","134972693138721937");
System.out.println(result);
//刷卡支付查询
System.out.println("=========查询=======");
result = freeReCharge.querySand(orderNo);
System.out.println(result);
//刷卡支付退款
System.out.println("=========退款=======");
result = freeReCharge.refundWeXin(orderNo,create_nonce_str(),"502","501",null,null);
System.out.println(result);
//查询退款
System.out.println("=========查询退款=======");
result = freeReCharge.queryRefundWeXin(orderNo,null,null,null);
System.out.println(result);
}
}