最近的项目在处理前任留下的微信支付,应为没有进行封装,很难重用,所有自己花了点时间整理了一下,做成工具类和帮助类,方便别人不一定,但方便自己是肯定的。贴出来交流一下。
以下是一些代码,其中用到的一些包要自己去下载了。
package com.test.weixin.help;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.apache.log4j.Logger;
import com.test.weixin.util.SignUtil;
import com.test.weixin.util.UrlUtil;
import com.test.weixin.util.XMLUtil;
public class PayHelper {
private static Logger log = org.apache.log4j.Logger.getLogger(PayHelper.class);
/**
* 统一下单请求地址
*/
public static final String UNIFIEDORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
/**
* tradeType 下单类型:JSAPI--公众号支付
*/
public static final String TRADE_TYPE_JSAPI = "JSAPI";
/**
* tradeType 下单类型:APP--app支付
*/
public static final String TRADE_TYPE_APP = "APP";
/**
* tradeType 下单类型NATIVE--原生扫码支付
*/
public static final String TRADE_TYPE_NATIVE = "NATIVE";
private String appId = null;
private String mchId = null;
private String mchKey = null;
public PayHelper(String appId, String mchId,String mchKey) {
this.appId = appId;
this.mchId = mchId;
this.mchKey = mchKey;
}
/**
* 公众号统一下单
* @param openId
* @param fee
* @param tradeNo
* @param notifyUrl
* @param ip
* @param body
* @param more 更多参数参见微信文档
* @return
* @throws Exception
*/
public String unifiedH5(String openId,int fee,String tradeNo,String notifyUrl,String ip,String body,Map<String,String> more)throws Exception{
Map<String,String> map = new HashMap<String, String>();
map.put("openid",openId);
map.put("total_fee",String.valueOf(fee));
map.put("out_trade_no",tradeNo);
map.put("notify_url",notifyUrl);
map.put("spbill_create_ip",ip);
map.put("body",body);
if(null!=more){
map.putAll(more);
}
return unified(TRADE_TYPE_JSAPI,map);
}
/**
* 根据预支付id 生成包含所有必须参数的map对象
* @param prepayId
* @return
* @throws Exception
*/
public Map<String,String> getClientPrepayMap(String prepayId)throws Exception{
Map<String,String> map = new HashMap<String,String>();
map.put("ap