服务端(java)实现微信支付二次签名

直接上代码--关键就是PayCommonUtil.java核心类

1、ConfigUtil.java 微信支付相关信息

    /**
     * 微信支付服务号相关信息
     */
    public final static String APPID = "wx********";//服务号的应用号
    public final static String MCH_ID = "148*****";//商户号
    public final static String API_KEY = "LtRzm*******";//API密钥
    public final static String SIGN_TYPE = "MD5";//签名加密方式
	public final static String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";//请求下单的http

	public final static String NOTIFY_URL = "http://域名/wxpay/returnmsg.do";//回调地址
	
	public final static String WXLOGIN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";

2、MyX509TrustManager.java信任管理器

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
 * 信任管理器
 */
public class MyX509TrustManager implements X509TrustManager {

    // 检查客户端证书
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    // 检查服务器端证书
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    // 返回受信任的X509证书数组
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

3、XMLUtil.java    xml解析工具类

public class XMLUtil {
	/**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
     *
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
        if (null == strxml || "".equals(strxml)) {
            return null;
        }
        Map m = new HashMap();
        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if (children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = XMLUtil.getChildrenText(children);
            }
            m.put(k, v);
        }
        //关闭流
        in.close();
        return m;
    }

    /**
     * 获取子结点的xml
     *
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if (!children.isEmpty()) {
            Iterator it = children.iterator();
            while (it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if (!list.isEmpty()) {
                    sb.append(XMLUtil.getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }
        return sb.toString();
    }
}

4、MD5Util 加密工具类


import java.security.MessageDigest;
 
public class MD5Util {
	private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));
 
		return resultSb.toString();
	}
 
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}
 
	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}
 
	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
		"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}

5、PayCommonUtil.java 进行2此签名核心工具类

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;


public class PayCommonUtil
{
	/**
	 * 创建支付宝交易对象
	 */
	public static AlipayClient getAliClient()
	{
		AlipayClient alipayClient = new DefaultAlipayClient(ConfigUtil.ALIPAY_PAYURL,
				ConfigUtil.ALIPAY_APPID,
				ConfigUtil.ALIPAY_PRIVATE_KEY, 
				ConfigUtil.ALIPAY_FORMAT, 
				ConfigUtil.ALIPAY_CHARSET,
				ConfigUtil.ALIPAY_PUBLIC_KEY, 
				ConfigUtil.ALIPAY_SIGN_TYPE);
		return alipayClient;
	}
	
	/**
	 * 创建微信交易对象
	 */
	public static SortedMap<Object, Object> getWXPrePayID()
	{
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		parameters.put("appid", ConfigUtil.APPID);//应用ID
		parameters.put("mch_id", ConfigUtil.MCH_ID);//商户号
		parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());//随机字符串
		parameters.put("fee_type", "CNY");//货币类型,默认类型人民币
		parameters.put("notify_url", ConfigUtil.NOTIFY_URL);//接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
		parameters.put("trade_type", "APP");//支付类型:APP
		return parameters;
	}
	
	/**
	 * 支付统一下单接口返回正常的prepay_id,
	 * 再按签名规范重新生成签名后,将数据传输给APP。
	 * 参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。
	 * 注意:package的值格式为Sign=WXPay
	 */
	public static SortedMap<Object, Object> startWXPay(String result)
	{
		try
		{
			Map<String, String> map = XMLUtil.doXMLParse(result);
			SortedMap<Object, Object> parameterMap = new TreeMap<Object, Object>();
			parameterMap.put("appid", ConfigUtil.APPID);
			parameterMap.put("partnerid", ConfigUtil.MCH_ID);
			parameterMap.put("prepayid", map.get("prepay_id"));
			parameterMap.put("package", "Sign=WXPay");
			parameterMap.put("noncestr", PayCommonUtil.CreateNoncestr());
			// 本来生成的时间戳是13位,但是ios必须是10位,所以截取了一下
			parameterMap.put("timestamp",
					Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0, 10)));
			String sign = PayCommonUtil.createSign("UTF-8", parameterMap);
			parameterMap.put("sign", sign);
			return parameterMap;
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 创建sign签名-- Utf-8格式
	 * @param characterEncoding 编码格式
	 * @param parameters 请求参数
	 * @return
	 */
	public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while(it.hasNext()){
			Map.Entry entry = (Map.Entry)it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)){
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + ConfigUtil.API_KEY);
		String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
		return sign;
	}
	/**
	 * 将请求参数转换为xml格式的string
	 * @param parameters 请求参数
	 * @return
	 */
	public static String getRequestXml(SortedMap<Object, Object> parameters){
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		Set es = parameters.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 ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k))
			{
				sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
			} else
			{
				sb.append("<" + k + ">" + v + "</" + k + ">");
			}
		}
		sb.append("</xml>");
		return sb.toString();
	}
	
	/**
	 * SSL协议发起http请求
	 * @param requestUrl 请求地址
	 * @param requestMethod 请求方式(GET、POST)
	 * @param outputStr 提交的数据
	 * @return 返回微信服务器响应的信息
	 */
	public static String httpsRequest(String requestUrl, String requestMethod, String outputStr){
		try{
			// 创建SSLContext对象,并使用我们指定的信任管理器初始化
			TrustManager[] tm = {new MyX509TrustManager()};
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            //从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            //从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            //释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
		}catch(ConnectException ce){
			System.out.println("连接超时:"+ce.getMessage());
		}catch(Exception e){
			System.out.println("https请求异常:"+ e.getMessage());
		}
		return null;
	}
	
	/**
	 * 创建随机数
	 * @return
	 */
	public static String CreateNoncestr()
	{
		String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String res = "";
		for (int i = 0; i < 16; i++)
		{
			Random rd = new Random();
			res += chars.charAt(rd.nextInt(chars.length() - 1));
		}
		return res;
	}
	
	/**
	 * 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
	 * @return boolean
	 */
	public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams)
	{
		StringBuffer sb = new StringBuffer();
		Set es = packageParams.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))
			{
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + ConfigUtil.API_KEY);
		// 算出摘要
		String mysign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toLowerCase();
		String tenpaySign = ((String) packageParams.get("sign")).toLowerCase();
		System.out.println(tenpaySign + " " + mysign);
		return tenpaySign.equals(mysign);
	}
}

6、微信支付下单接口

    /**
	 * 微信支付下单
	 * @param body:商品描述
	 * @param detail:商品详情
	 * @param attach:附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
	 * @param out_trade_no:商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
	 * @param total_price:订单总金额,单位为分,详见支付金额
	 * @param spbill_create_ip:用户端实际ip
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/wxPayOrder",produces = "text/html;charset=UTF-8",method={RequestMethod.POST}) 
	@ResponseBody
	public String wxPayOrder() throws Exception{
		logBefore(logger, "微信支付--下单");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = WeixinConsts.FAIL;
		String returnStr="";
		try{
	    	String body = "会员充值";
	    	String detail ="加入VIP会员";
	    	//获取充值金额
	    	String price = pd.getString("AMOUNT");
	    	SortedMap<Object,Object> parameters = PayCommonUtil.getWXPrePayID(); // 获取预付单,此处已做封装,需要工具类
	    	parameters.put("body", body);//商品描述
	    	parameters.put("detail", detail);//商品详情
	    	parameters.put("out_trade_no", UUID.randomUUID().toString().substring(0, 15)); //商品订单id
	    	//parameters.put("total_fee", "1");//总金额,每次支付一分钱
	    	parameters.put("spbill_create_ip", "127.0.0.1");//终端IP,支持IPV4和IPV6两种格式的IP地址。调用微信支付API的机器IP
	    	//微信默认从单位为分,所以用此方法将单位调整成元
	    	int price100 = new BigDecimal(price).multiply(new BigDecimal(100)).intValue();
	    	parameters.put("total_fee", String.valueOf(price100));
	    	// 设置签名
	    	String sign = PayCommonUtil.createSign("UTF-8", parameters);
	    	parameters.put("sign", sign);
	    	//封装请求参数结束,获取xml结果
	    	String requestXML = PayCommonUtil.getRequestXml(parameters);
	    	//调用统一下单接口
	    	String payResult = PayCommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST",requestXML);
	    	System.out.println("\n调用统一下单接口,结果:"+payResult);
	    	SortedMap<Object, Object> parMap = PayCommonUtil.startWXPay(payResult);
	    	System.out.println("最终的map是:" + parMap.toString());
	    	net.sf.json.JSONObject jsonObj = net.sf.json.JSONObject.fromObject(parMap);
			StringBuilder msg = new StringBuilder();
			msg.append("{\"code\":\"200\",");
			msg.append("\"msg\":\"查询成功!\",");
			msg.append("\"datas\":");
			msg.append(jsonObj.toString());
			msg.append("}");
			System.out.println(msg.toString());
			returnStr = msg.toString();
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	map.put("result", result);
			logAfter(logger);
		}
		return returnStr;
	}

/**
	 * 微信支付--回调通知业务处理
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/returnmsg.do", produces = "text/html;charset=UTF-8",method={RequestMethod.POST})
	public String returnmsg(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String msg = "";
		//解析结果存储在HashMap
		Map<String,String> map = new HashMap<String,String>();
		InputStream inputStream = request.getInputStream();
		
		//读取输入流
		SAXReader reader = new SAXReader();
		Document document = reader.read(inputStream);
		//得到xml根元素
		Element root = document.getRootElement();
		//得到根元素的所有子节点
		List<Element> elementList = root.elements();
		
		//遍历所有子节点
		for(Element e:elementList){
			map.put(e.getName(), e.getText());
		}
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(map);
		
		System.out.println("===消息通知的结果:" + json.toString() + "==========================");
		System.out.println("===return_code===" + map.get("return_code"));
		System.out.println("===return_msg===" + map.get("return_msg"));
		System.out.println("===out_trade_no===" + map.get("out_trade_no"));
		
		//验证签名的过程
		//判断是否支付成功
		String returnCode = map.get("return_code");
		if("SUCCESS".equals(returnCode)){
			/**
			 * 支付成功之后的业务处理
			 */
			msg = "SUCCESS";
		}else if("FAIL".equals(returnCode)){
			/**
			 * 支付失败后的业务处理
			 */
			msg = "FAIL";
		}
		//释放资源
		inputStream.close();
		inputStream = null;
		return msg;
	}

相关文档:

Unity IOS 接入微信登录

Unity Android 接入微信登录

如有问题可以微信咨询,可扫描下方微信添加互相沟通:

如果对你有帮助,请给点赞助支持下奥!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶半欲缺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值