1. 程式人生 > >java微信第三方支付

java微信第三方支付

1. 寫好配置項

#微信支付介面
ias.pay.wxpay.payUrl=https://api.mch.weixin.qq.com/pay/unifiedorder
#回撥地址
ias.pay.wxpay.notifyUrl=
#終端IP
ias.pay.wxpay.spbillCreateIp=
ias.pay.wxpay.appId=
ias.pay.wxpay.mchId=
ias.pay.wxpay.tradeType=APP
ias.pay.wxpay.packages=Sign=WXPay
ias.pay.wxpay.key=

2. java測試用例呼叫微信第三方支付,其中的payProp為配置項,

	@Autowired
	private PayProp payProp;
	
	@Test
	public void createPay() {
		WxPay wxPay = new WxPay();
		wxPay.setPayUrl(payProp.getWxpay().getPayUrl());
		wxPay.setAppId(payProp.getWxpay().getAppId());
		wxPay.setMchId(payProp.getWxpay().getMchId());
		wxPay.setSpbillCreateIp(payProp.getWxpay().getSpbillCreateIp());
		wxPay.setNotifyUrl(payProp.getWxpay().getNotifyUrl());
		wxPay.setTradeType(payProp.getWxpay().getTradeType());
		wxPay.setKey(payProp.getWxpay().getKey());
		wxPay.setBody("騰訊充值中心-QQ會員充值");
		wxPay.setNonceStr(隨機字串,長度要求在32位以內。);
		wxPay.setOutTradeNo(訂單號);
		wxPay.setTotalFee(支付金額);
		wxPay.setSign(WeiXinUtil.sign(wxPay, wxPay.getKey()));
		String xml = XmlUtil.toXml(wxPay);
		log.debug("微信支付xml為:\n{}", xml);
		
		String results = RestClient.getClient().postForObject(wxPay.getPayUrl(), xml, String.class);
		log.debug("返回的xml:\n{}", results.toString());
		WXResults wxResults = XmlUtil.toBean(results, WXResults.class);
		if(StringUtil.equals(wxResults.getReturnCode(), "SUCCESS")) {
			log.debug("返回資訊", wxResults.toString());
		} else {
			throw new BusinessException(30010, wxResults.getReturnMsg());
		}
	}

3。支付成功,回撥介面

	@RequestMapping(value="wxpay/notify", produces={"application/xml"})
    public String notify(@RequestBody String callback) throws DocumentException {
		log.debug("微信支付回撥xml為:{}", callback);
		WxNotify notify = XmlUtil.toBean(callback, WxNotify.class);
		if(notify.getReturnCode().equals("SUCCESS") || notify.getResultCode().equals("SUCCESS")) {
			Map<String, Object> map =  XmlUtil.xml2map(callback, false);
				boolean signVerified = WeiXinUtil.isWechatSign(map, payProp.getWxpay().getKey());
				if(signVerified) {
					log.info("微信支付驗籤成功!!!");
					log.info("微信支付完成!!!!!");
				}
		}
		return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

4.其中用到的XmlUtil幫助類附上程式碼

    /** 
     * xml轉map 不帶屬性 
     * @param xmlStr 
     * @param needRootKey 是否需要在返回的map里加根節點鍵 
     * @return 
     * @throws DocumentException 
     */  
    public static Map<String,Object> xml2map(String xmlStr, boolean needRootKey) throws DocumentException {  
        Document doc = DocumentHelper.parseText(xmlStr);  
        Element root = doc.getRootElement();  
        Map<String, Object> map = xml2map(root);  
        if(root.elements().size()==0 && root.attributes().size()==0){  
            return map;  
        }  
        if(needRootKey){  
            //在返回的map里加根節點鍵(如果需要)  
            Map<String, Object> rootMap = new HashMap<String, Object>();  
            rootMap.put(root.getName(), map);  
            return rootMap;  
        }  
        return map;  
    } 
    /**
     *  將傳入xml文字轉換成Java物件
     * @Title: toBean 
     * @param xmlStr
     * @param cls  xml對應的class類
     * @return T   xml對應的class類的例項物件
     * 
     * 呼叫的方法例項:PersonBean person=XmlUtil.toBean(xmlStr, PersonBean.class);
     */
	public static <T> T  toBean(String xmlStr,Class<T> cls){
        //注意:不是new Xstream(); 否則報錯:java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
        XStream xstream=new XStream(new DomDriver());
        xstream.processAnnotations(cls);
        T obj=(T)xstream.fromXML(xmlStr);
        return obj;            
    } 

5. 涉及到的生成簽名和驗籤工具類程式碼

package com.ias.server.pay.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ias.common.utils.bean.ClassUtil;
import com.ias.common.utils.collection.ArrayUtils;
import com.ias.common.utils.date.TimeUtil;
import com.ias.common.utils.encrypt.MD5Util;
import com.ias.common.utils.string.StringUtil;
import com.ias.server.pay.annotations.Sign;

public class WeiXinUtil {
	
	private static final Logger log = LoggerFactory.getLogger(WeiXinUtil.class);
	
	/** 
	 * 微信支付簽名
	 * @author: jiuzhou.hu
	 * @date:2017年3月15日下午12:54:52 
	 * @param obj
	 * @param keyStr
	 * @return
	 */
	public static String sign(Object obj, String keyStr) {
		Map<String, String> fields = new HashMap<>();
		for(Field field : obj.getClass().getDeclaredFields()) {
			Sign sign = field.getAnnotation(Sign.class);
			if(field.getAnnotation(Sign.class) != null) {
				fields.put(field.getName(), sign.value());
			}
		}
		
		List<String> signs = new ArrayList<>();
		for(String key:fields.keySet()) {
			Object ov = ClassUtil.getFieldValue(obj, key);
			if(ov != null) {
				signs.add(fields.get(key) + "=" + ov);
			}
		}
		signs.sort((String s1, String s2) -> s1.compareTo(s2));
		signs.add("key=" + keyStr);
		String _signs = ArrayUtils.toString(signs,'&');
		log.debug("未加密的sign串為:{}", _signs);
		String md5Sign = MD5Util.encode(_signs).toUpperCase();
		log.debug("md5加密過的sign串為:{}", md5Sign);
		return md5Sign;
	}
	
	/**
	 * 微信驗籤
	 * @author feng.ye
	 * @date 2018年7月19日 下午1:27:27
	 * @param map 
	 * @param apiKey
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isWechatSign(Map<String, Object> map,String apiKey) {
	     StringBuffer sb = new StringBuffer();
	     Set es = map.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 (!"sign".equals(k) && null != v && !"".equals(v) && !"key".equals(k)) {
	             sb.append(k + "=" + v + "&");
	         }
	     }
	     sb.append("key=" + apiKey);
	     String sign = MD5Util.encode(sb.toString()).toUpperCase();
	     log.debug("新生成簽名為:{}", sign);
	     String validSign = ((String) map.get("sign")).toUpperCase();
	     log.debug("微信端返回簽名為:{}", validSign);
	     if(StringUtil.isNotBlank(validSign) && StringUtil.equals(sign, validSign)) {
	    	 return true;
	     }else {
	    	 return false;
	     }
	 }
	
	/** 
	 * 獲取10位時間戳
	 * @author: jiuzhou.hu
	 * @date:2017年3月15日下午1:17:49 
	 * @return
	 */
	public static long timestamp() {
		return Long.parseLong(String.valueOf(TimeUtil.getSysTimestamp().getTime()).toString().substring(0,10));
	}
}