微信支付接口(公众号支付)+微信支付回调函数 附代码

 

前段时间做微信支付,微信浏览器填写金额商品名之后提交跳转付款页面确认然后返回界面判断,今天来详细说下

 

国际惯例先贴代码

mcontroller.java

 

public void wxpay() {
        if(this.getPara("openid")==null){
        this.redirect(Conts.wxpayoauth.replace("*state*", "wxpay"));
        return;
        }
        //上面这块主要是验证下,获得openid,验证功能改天再开一篇文章,然后把openid存一下
        this.setAttr("openid", this.getPara("openid"));
        //价格和info(info0+info1拼接而成)纯粹是需求需要,这里大家灵活处理
        if(this.getPara("price")==null){
        this.setAttr("res", "");
        this.setAttr("str", "");
        this.setAttr("tag",false);
        this.render("wxpay.html");
        return;
        }
        this.setAttr("tag", true);
        String price = this.getPara("price");
        String info0 = this.getPara("info0");
        String info1 = this.getPara("info1");
        if("1".equals(info1))
        info1="综合费";
        else if("2".equals(info1))
        info1="激励基金";
        else if("3".equals(info1))
        info1="房屋押金";
        else if("4".equals(info1))
        info1="门禁卡";
        else if("5".equals(info1))
        info1="车位费";

        String info = info0+"("+info1+")";

        String jsapi_ticket =Sha1Util.getAccessToken(PropKit.get("appId"),PropKit.get("appSecret"));
//看清楚.这是ticket..用token在https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi里换的,大家一定要注意这里,这是个坑,一定要按照微信的规则得到这串,并且一定保证微信规则能得到返回值,还有个支付授权目录,这个一定要记得修改我写的是我这个方法的访问地址就是。。。/m/wxpay/.http://jingyan.baidu.com/article/ed2a5d1f340dd409f7be177c.html 这个是获得access——token的规则,其实官方的demo中工具类获取的,还有这容易出错,记得吧ip添加到白名单,这有个我搞不懂的,有些ip就是获取不到token,但是加了白名单之后就可以了,难不成遇见一个加一个?有说只有几个特殊的家进入就行,这个是个容易出错的地方,提醒下大家
        String nonce_str = Sha1Util.getNonceStr();// 随机字符串
        String timestamp = Sha1Util.getTimeStamp();// 时间戳
        String appid = PropKit.get("appId");//APPID
        String url="http://man666666n.cn/m/pay/";//发起支付的前端页面的URL地址.而且...而且必须在微信支付里面配置才行!!!
        String sign = null;
        try {
        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("jsapi_ticket", jsapi_ticket);
        packageParams.put("noncestr", nonce_str);
        packageParams.put("timestamp", timestamp);
        packageParams.put("url", url);
        sign = Sha1Util.createSHA1Sign(packageParams);
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        String res="appId : \"" + appid + "\",timestamp : \"" + timestamp //微信个傻逼..这里的timestamp是小写~~
        + "\", nonceStr : \"" + nonce_str
        + "\", signature : \"" + sign + "\"";
        System.out.println("sign签名="+res);
        this.setAttr("price", price);
        this.setAttr("info", info);
        this.setAttr("res", res);
        this.setAttr("str",  dopay(this.getPara("openid"),price,info,nonce_str,timestamp));

        this.render("wxpay.html");
        }

工具类如果需要在官方的demo中有
mcontroller
 

public String dopay(String openId,String money,String info,String nonce_str,String timestamp) {

        // 网页授权后获取传递的参数
        String currTime = TenpayUtil.getCurrTime();
        String orderNo = currTime+Sha1Util.getNonceStr().substring(0, 18);
        float sessionmoney = Float.parseFloat(money);
        String finalmoney = String.format("%.2f", sessionmoney);
        finalmoney = finalmoney.replace(".", "");
        finalmoney = String.valueOf(Integer.parseInt(finalmoney));

        // 商户相关资料
        String appid = PropKit.get("appId");
        String appsecret = PropKit.get("appSecret");
        String mch_id = PropKit.get("mch_id");//邮件里的MCHID
        String partnerkey = PropKit.get("paterner_key");;//在微信商户平台pay.weixin.com里自己生成的那个key
        String body = info;
        // 商户订单号
        String out_trade_no = orderNo;
        // 订单生成的机器 IP
        String spbill_create_ip = Sha1Util.getIp2(getRequest());
        // 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。这个就是notify_url方法,在后面贴的,微信会返回我们一些值我们判断一下,返回success就结束了订单状态了,不然会一直访问回调
        String notify_url = "http://man666666n/m/notify_url";
        String trade_type = "JSAPI";
        String openid =openId;

        Wxpaydto wx = new Wxpaydto();
        wx.setBody(info);
        wx.setOpenId(this.getPara("openid"));
        wx.setOrderId(orderNo);
        wx.setSpbillCreateIp(spbill_create_ip);
        wx.setTotalFee(money);
        wx.setTime(new Date());
        wx.setNotifyUrl("http://man6666n.cn/m/notify_url");
        wx.setIstrue("0");
        wx.save();

        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("body", body);
        packageParams.put("out_trade_no", out_trade_no);
        packageParams.put("total_fee", finalmoney);
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);
        packageParams.put("openid", openid);

        RequestHandler reqHandler = new RequestHandler(null, null);
        reqHandler.init(appid, appsecret, partnerkey);

        String sign = reqHandler.createSign(packageParams);
        String xml = "<xml>" + "<appid>" + appid + "</appid>"
        + "<mch_id>"+ mch_id + "</mch_id>"
        + "<nonce_str>" + nonce_str+ "</nonce_str>"
        + "<sign><![CDATA[" + sign + "]]></sign>"
        + "<body><![CDATA[" + body + "]]></body>"
        + "<out_trade_no>"+ out_trade_no+ "</out_trade_no>"
        + "<total_fee>"+ finalmoney+ "</total_fee>"
        + "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"
        + "<notify_url>" + notify_url + "</notify_url>"
        + "<trade_type>" + trade_type + "</trade_type>"
        + "<openid>"+ openid + "</openid>" + "</xml>";

        String allParameters = "";
        try {
        allParameters = reqHandler.genPackage(packageParams);
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        String prepay_id = "";
        try {
        prepay_id = new GetWxOrderno().getPayNo(createOrderURL, xml);
        if (prepay_id.equals("")) {
        System.out.println("统一支付接口获取预支付订单出错");
        }
        } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }
        SortedMap<String, String> finalpackage = new TreeMap<String, String>();
        String nonceStr2 = nonce_str;
        String prepay_id2 = "prepay_id=" + prepay_id;
        String packages = prepay_id2;
        finalpackage.put("appId", appid);
        finalpackage.put("timeStamp", timestamp);
        finalpackage.put("nonceStr", nonceStr2);
        finalpackage.put("package", packages);
        finalpackage.put("signType", "MD5");
        String finalsign = reqHandler.createSign(finalpackage);
        System.out.println("timestamp:\"" + timestamp
        +"\",appid:\"" + appid
        + "\",nonceStr:\"" + nonceStr2 + "\",package:\""
        + packages + "\",signType: \"MD5" + "\",paySign:\""
        + finalsign + "\"");
        return "timestamp:\"" + timestamp
        +"\",appid:\"" + appid
        + "\",nonceStr:\"" + nonceStr2 + "\",package:\""
        + packages + "\",signType: \"MD5" + "\",paySign:\""
        + finalsign + "\"";
        }
//隐约记得好像两次的timestamp跟sign好像要求一样.这个就是js返回的一个逻辑,具体根据项目来写,微信会在两处都返回,一个是js一个是回调函数,官方的说法是js不作为标准可能会有错误
public void paymsg(){
        String tag = this.getPara("tag");
        if(tag.equals("payok")){
        this.setAttr("msg", "支付成66666配合。");
        }
        else if(tag.equals("payfail"))
        this.setAttr("msg", "支付失66666重试。");
        else if(tag.equals("paycencel"))
        this.setAttr("msg", "支付取消。");
        else
        this.setAttr("msg", "未知错误。");

        this.render("msgclose.html");
        }
public void notify_url() throws IOException{
//这个就是回调的判断
        InputStream re = this.getRequest().getInputStream();
        System.out.println("得到的contextpath"+re);
        System.out.println(this.getRequest());
        try {
        processResponseXml(inputStream2String(re));
        } catch (Exception e) {
        e.printStackTrace();
        }
        this.renderText("SUCCESS");
        }

 

官方工具类
WXPayUtil 

 

 

package com.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.*;
import java.security.MessageDigest;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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


public class WXPayUtil {
	
	public enum SignType {
        MD5, HMACSHA256
    }
    /**
     * XML格式字符串转换为Map
     *
     * @param strXML XML字符串
     * @return XML数据转换后的Map
     * @throws Exception
     */
    public static Map<String, String> xmlToMap(String strXML) throws Exception {
        try {
            Map<String, String> data = new HashMap<String, String>();
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            try {
                stream.close();
            } catch (Exception ex) {
                // do nothing
            }
            return data;
        } catch (Exception ex) {
            WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
            throw ex;
        }

    }

    /**
     * 将Map转换为XML格式的字符串
     *
     * @param data Map类型数据
     * @return XML格式的字符串
     * @throws Exception
     */
    public static String mapToXml(Map<String, String> data) throws Exception {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document document = documentBuilder.newDocument();
        org.w3c.dom.Element root = document.createElement("xml");
        document.appendChild(root);
        for (String key: data.keySet()) {
            String value = data.get(key);
            if (value == null) {
                value = "";
            }
            value = value.trim();
            org.w3c.dom.Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
        try {
            writer.close();
        }
        catch (Exception ex) {
        }
        return output;
    }


    /**
     * 生成带有 sign 的 XML 格式字符串
     *
     * @param data Map类型数据
     * @param key API密钥
     * @return 含有sign字段的XML
     */
    public static String generateSignedXml(final Map<String, String> data, String key) throws Exception {
        return generateSignedXml(data, key, SignType.MD5);
    }

    /**
     * 生成带有 sign 的 XML 格式字符串
     *
     * @param data Map类型数据
     * @param key API密钥
     * @param signType 签名类型
     * @return 含有sign字段的XML
     */
    public static String generateSignedXml(final Map<String, String> data, String key, SignType signType) throws Exception {
        String sign = generateSignature(data, key, signType);
        data.put("sign", sign);
        return mapToXml(data);
    }


    /**
     * 判断签名是否正确
     *
     * @param xmlStr XML格式数据
     * @param key API密钥
     * @return 签名是否正确
     * @throws Exception
     */
    public static boolean isSignatureValid(String xmlStr, String key) throws Exception {
        Map<String, String> data = xmlToMap(xmlStr);
        if (!data.containsKey("sign") ) {
            return false;
        }
        String sign = data.get("sign");
        return generateSignature(data, key).equals(sign);
    }

    /**
     * 判断签名是否正确,必须包含sign字段,否则返回false。使用MD5签名。
     *
     * @param data Map类型数据
     * @param key API密钥
     * @return 签名是否正确
     * @throws Exception
     */
    public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
        return isSignatureValid(data, key, SignType.MD5);
    }

    /**
     * 判断签名是否正确,必须包含sign字段,否则返回false。
     *
     * @param data Map类型数据
     * @param key API密钥
     * @param signType 签名方式
     * @return 签名是否正确
     * @throws Exception
     */
    public static boolean isSignatureValid(Map<String, String> data, String key, SignType signType) throws Exception {
        if (!data.containsKey("sign") ) {
            return false;
        }
        String sign = data.get("sign");
        System.out.println("data+"+data+",key="+key+",signtype="+signType);
        System.out.println("sign="+sign);
        return generateSignature(data, key, signType).equals(sign);
    }

    /**
     * 生成签名
     *
     * @param data 待签名数据
     * @param key API密钥
     * @return 签名
     */
    public static String generateSignature(final Map<String, String> data, String key) throws Exception {
        return generateSignature(data, key, SignType.MD5);
    }

    /**
     * 生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。
     *
     * @param data 待签名数据
     * @param key API密钥
     * @param signType 签名方式
     * @return 签名
     */
    public static String generateSignature(final Map<String, String> data, String key, SignType signType) throws Exception {
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals("sign")) {
                continue;
            }
            if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
                sb.append(k).append("=").append(data.get(k).trim()).append("&");
        }
        sb.append("key=").append(key);
        if (SignType.MD5.equals(signType)) {
            return MD5(sb.toString()).toUpperCase();
        }
        else if (SignType.HMACSHA256.equals(signType)) {
            return HMACSHA256(sb.toString(), key);
        }
        else {
            throw new Exception(String.format("Invalid sign_type: %s", signType));
        }
    }


    /**
     * 获取随机字符串 Nonce Str
     *
     * @return String 随机字符串
     */
    public static String generateNonceStr() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }


    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception {
        java.security.MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 生成 HMACSHA256
     * @param data 待处理数据
     * @param key 密钥
     * @return 加密结果
     * @throws Exception
     */
    public static String HMACSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 日志
     * @return
     */
    public static Logger getLogger() {
        Logger logger = LoggerFactory.getLogger("wxpay java sdk");
        return logger;
    }

    /**
     * 获取当前时间戳,单位秒
     * @return
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis()/1000;
    }

    /**
     * 获取当前时间戳,单位毫秒
     * @return
     */
    public static long getCurrentTimestampMs() {
        return System.currentTimeMillis();
    }

    /**
     * 生成 uuid, 即用来标识一笔单,也用做 nonce_str
     * @return
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }

}

 

 

 

 

 

 

前端  wxpay.html

前端调用这块需要好好看一下,稍微有点绕,不知道其他大神有什么更好的实现方法

 

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>自****</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<script src="/************min.js"></script>
		<link rel="stylesheet" href="/m***in.css">
	</head>
	<body leftmargin="0" topmargin="0" rightmargin="0" topmargin="0" style="background: #ffffff;">
        <!--img src="/***.jpg" width="100%" height="100%" style="position:absolute;top:0;left:0;right:100;bottom:500;z-index:-1" /-->
        <div style="margin-top: 80px;text-align: center;width: 100%;">
        	<img src="/****ogo.png" style="width: 280px;height: 60px;"/>
        </div>
        <div style="text-align: center;width: 100%;margin-top: 32px;">
        </div>
        
        <div style="margin-top: 32px;text-align: center;" id="app">
        
        <#if tag>
        	<p>
        	请确认信息:
        	<br/>
        	价格:${price!}元<br/>
        	备注:${info!}<br/>
        	确认信息无误请点击确认付款按钮
        	</p>
        
        <#else>
        	<form action="" method="get" id="payform">
        	<input type="hidden" name="openid" value="${openid!}">
        	<input type="text" style="border:0px;width: 84%;margin-left: 8%;height: 
        	45px;background-color: #cccccc;" placeholder="请输入金额" id="price" v-model="message" name="price" >
        	
        	<input type="text" style="border:0px;margin-top: 10px;width: 84%;
        	margin-left: 8%;height: 45px;background-color: #cccccc;" placeholder="请输入公司名称  " 
        	id="info0" name="info0">
        	<br>
        	<select type="text" id="info1" name="info1"
        	style="border:0px;margin-top: 10px;width: 84%;margin-left: 8%;height: 45px;background-color: #cccccc;"   >
              <option value="1">综**</option>
              <option value="2">激8***金</option>
              <option value="3">房***金</option>
              <option value="4">门***卡</option>
              <option value="5">车***</option>
            </select>
        	
        	</form>
        	
		     <h3 id ="showprice" v-if="message === ''">{{ message}}</h3>
		     <h3 id ="showprice" v-else="message === ''">¥{{ message}}</h3>
        	
        </#if>	
        	<button style="margin-top: 10px;background-color: #343c8d;width: 84%;margin-left: 8%;height: 45px;border:0px;color:#ffffff;" onclick="sub()">确认付款</button>
        </div>
       <script src="https://cdn.boot******js"></script>
		    
		<script>
		new Vue({
		  el: '#app',
		  data: {
		    message: ''
		  }
		})
		</script>
        <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
		wx.config({
		    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
		    ${res!},// 必填,签名,见附录1
		    jsApiList: ['chooseWXPay'], // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
		});
		wx.ready(function(){
		});
		wx.error(function(res){
			
		    // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
		});
		function callpay(){
			wx.checkJsApi({
			    jsApiList: ['chooseWXPay'], // 检查微信支付接口是否可用
			    success: function (res) {
			    	if(res.checkResult.chooseWXPay){
			    		wx.chooseWXPay({
							 ${str!}, // 支付签名
							 cancel:function(res){
							    	//微信返回的状态 取消:res.errMsg == "chooseWXPay:cancel"
							    	//alert("支付取消。");
							    	location.href="**ycencel";
							    },
							    error:function(res){
							    	//alert("支付失败,请返回重试。");
							    	location.href="/**ail";
							    },
							    success:function(res){
							    	/*微信返回的状态 成功:res.errMsg == "chooseWXPay:ok"*/
							    	//alert("支付成***配合。");
							    	location.href="/****payok";
							    }
			    		});
			    	}
			    }
			});
		}
		
		</script>
        <script>
          
        	function sub(){
        	<#if tag>
        	callpay();
        	<#else>
        		var price = document.getElementById("price").value;
        		var info0 = document.getElementById("info0").value;
        		
        		
        		if(!isNaN(price)){
				   
				}else{
				   alert("请填写正确格式!");
        			return;
				}
        		
        		if(price=="" || price==""||info0=="" || info0==""){
        			alert("请完整填写表单!");
        			return;
        		}
        		document.getElementById("payform").submit();
        	</#if>	

        	}
        </script>
    </body>
</html>

 

 

 

 

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值