JAVA三方Wechat

微信扫码支付(超详细版)

2019-11-28  本文已影响0人  Hi_JIAQI

Native扫码支付

开发步骤:

Native支付开通申请设置流程:

阅读接口文档进行开发:

所需要的工具类
WXPayConstants类
/**
 * 常量
 */
public class WXPayConstants {
    public enum SignType {
        MD5, HMACSHA256
    }
    public static final String FIELD_SIGN = "sign";
}
WXPayXmlUtil类
import org.w3c.dom.Document;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;


public final class WXPayXmlUtil {

    /**
     * XML解析
     *
     * @param inStream 微信发的流信息
     * @param encoding 编码格式
     * @return
     */
    public static String inputStream2String(InputStream inStream, String encoding) {
        String result = null;
        ByteArrayOutputStream outStream = null;
        try {
            if (inStream != null) {
                outStream = new ByteArrayOutputStream();
                byte[] tempBytes = new byte[1024];
                int count = 0;
                while ((count = inStream.read(tempBytes)) != -1) {
                    outStream.write(tempBytes, 0, count);
                }
                tempBytes = null;
                outStream.flush();
                result = new String(outStream.toByteArray(), encoding);
                outStream.close();
            }
        } catch (Exception e) {
            result = null;
        }
        return result;
    }

    public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        documentBuilderFactory.setXIncludeAware(false);
        documentBuilderFactory.setExpandEntityReferences(false);

        return documentBuilderFactory.newDocumentBuilder();
    }

    public static Document newDocument() throws ParserConfigurationException {
        return newDocumentBuilder().newDocument();
    }
}
WXPayUtil类
import com.itcast.dormmanagesys.utils.pay.WXPayConstants.SignType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.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 java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;

public class WXPayUtil {

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    /**
     * 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>();
            DocumentBuilder documentBuilder = WXPayXmlUtil.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 {
        org.w3c.dom.Document document = WXPayXmlUtil.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(WXPayConstants.FIELD_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(WXPayConstants.FIELD_SIGN)) {
            return false;
        }
        String sign = data.get(WXPayConstants.FIELD_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(WXPayConstants.FIELD_SIGN)) {
            return false;
        }
        String sign = data.get(WXPayConstants.FIELD_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(WXPayConstants.FIELD_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() {
        char[] nonceChars = new char[32];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }

    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception {
        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();
    }

}
import com.itcast.commom.util.Result;
import com.itcast.commom.util.ResultEnum;
import com.itcast.dormmanagesys.utils.pay.Httpclient;
import com.itcast.dormmanagesys.utils.pay.SystemUtil;
import com.itcast.dormmanagesys.utils.pay.WXPayUtil;
import com.itcast.dormmanagesys.utils.pay.WXPayXmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 功能描述:
 *
 * @authro JIAQI
 * @date 2019/11/6 - 10:36
 */
@Slf4j
@Controller
@RequestMapping("/wechat/pay")
public class PayController {
    //公众账号ID
    final private String appid = "你的公众账号ID";
    //商户号
    final private String mch_id = "你的开放平台商户号";
    /*商户密钥*/
    final private String mch_Key = "你的开放平台商户密钥";
    //商品ID(这里写死了,若用到请自行修改使用随机数等等)
    final private String product_id = "CS20191133344";
    //异步回调地址
    final private String notify_url = "你的异步回调地址";

    /**
     * 扫码异步回调地址
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/paycallback")
    public String payCallBack(HttpServletRequest request,
                              HttpServletResponse response) {
        InputStream is = null;
        try {
            //获取请求的异步回调信息
            is = request.getInputStream();
            String xml = WXPayXmlUtil.inputStream2String(is, "UTF-8");
            final Map<String, String> map = WXPayUtil.xmlToMap(xml);
            //判断是否支付成功 业务结果:SUCCESS/FAIL
            if ("SUCCESS".equals(map.get("result_code"))) {
                //公众账号ID
                map.get("appid");
                //商户号
                map.get("mch_id");
                //随机字符串
                map.get("nonce_str");
                //签名
                map.get("sign");
                //用户标识
                map.get("openid");
                //是否关注公众账号
                map.get("is_subscribe");
                //交易类型
                map.get("trade_type");
                //付款银行
                map.get("bank_type");
                //订单金额
                map.get("total_fee");
                //货币种类
                map.get("fee_type");
                //现金支付金额
                map.get("cash_fee");
                //微信支付订单号
                map.get("transaction_id");
                //商户订单号
                map.get("out_trade_no");
                //支付完成时间
                map.get("time_end");
            }
            //回复微信服务器信息
            response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 检验是否支付成功
     *
     * @param out_trade_no 订单号
     * @return 支付结果
     */
    private final String orderqueryURL = "https://api.mch.weixin.qq.com/pay/orderquery";

    @PostMapping("/queryOrder")
    @ResponseBody
    public Result ajaxCheckPay(@RequestBody String out_trade_no) {
        log.info("【所需校验支付状态的订单号】:{}", out_trade_no);
        Map<String, String> reqData = new HashMap<String, String>();
        try {
            reqData.put("appid", appid);
            reqData.put("mch_id", mch_id);
            reqData.put("out_trade_no", out_trade_no);
            reqData.put("nonce_str", WXPayUtil.generateNonceStr());
            //签名
            String sign = WXPayUtil.generateSignature(reqData, mch_Key);
            reqData.put("sign", sign);
            //XML转义即可
            String xml = WXPayUtil.mapToXml(reqData);
            String xmlStr = Httpclient.doPostJson(orderqueryURL, xml);
            final Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
            /*交易状态:
             *SUCCESS—支付成功
             *REFUND—转入退款
             *NOTPAY—未支付
             *CLOSED—已关闭
             *REVOKED—已撤销(付款码支付)
             * USERPAYING--用户支付中(付款码支付)
             */
            if ("SUCCESS".equals(map.get("trade_state"))) {
                log.info("【订单支付成功】:{}", out_trade_no);
                return Result.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.failure(ResultEnum.FAILURE);
    }

    /**
     * 下单获取二维码
     *
     * @param request
     * @param model
     * @return
     */
    private final String unifiedorderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    @GetMapping("/pay")
    public String pay(HttpServletRequest request, Model model) {
        String code_url = null;
        try {
            Map<String, String> reqData = new HashMap<String, String>();
            //公众账号ID
            reqData.put("appid", appid);
            //商户号
            reqData.put("mch_id", mch_id);
            //随机字符串
            reqData.put("nonce_str", WXPayUtil.generateNonceStr());
            //商品描述
            reqData.put("body", "测试PC端支付");
            //商户订单号
            reqData.put("out_trade_no", product_id);
            //标价金额 / 分
            reqData.put("total_fee", "1");
            //终端IP
            reqData.put("spbill_create_ip", SystemUtil.getIpAddress(request));
//            //交易起始时间
//            final String dateStart = DateUtil.getDate(new Date(), DateStyle.YYYYMMDDHHMMSS);
//            reqData.put("time_start", dateStart);
//            System.out.println(dateStart);
//            //交易结束时间
//            reqData.put("time_expire", DateUtil.addMinute(dateStart, 2));
            //通知地址
            reqData.put("notify_url", notify_url);
            //交易类型
            reqData.put("trade_type", "NATIVE");
            //签名
            String sign = WXPayUtil.generateSignature(reqData, mch_Key);
            reqData.put("sign", sign);
            //XML转义即可
            String xml = WXPayUtil.mapToXml(reqData);
            String xmlStr = Httpclient.doPostJson(unifiedorderURL, xml);
            final Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
            if ("SUCCESS".equals(map.get("result_code")) && "OK".equals(map.get("return_msg"))) {
                code_url = map.get("code_url");
            }
            System.out.println(code_url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //二维码URL
        model.addAttribute("code_url", code_url);
        System.out.println(product_id);
        //订单号
        model.addAttribute("out_tradeno", product_id);
        return "/pay";
    }
HttpClient工具类、pom依赖
 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Httpclient {

    public static String doGet(String url, Map<String, String> param) {

        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);

            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

    public static String doGet(String url) {
        return doGet(url, null);
    }

    public static String doPost(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static String doPost(String url) {
        return doPost(url, null);
    }

    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }
}
系统工具类(用来获取ip地址)
package com.itcast.dormmanagesys.utils.pay;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能描述:
 *
 * @authro JIAQI
 * @date 2019/7/18 - 17:55
 */
public class SystemUtil {

    /**
     * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
     * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
     * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
     * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
     * 192.168.1.100
     * 用户真实IP为: 192.168.1.110
     *
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
页面中的二维码生成是使用qrcode.js生成的,可在此GitHub使用文档地址中进行下载
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>内嵌式——PC支付</title>
    <script th:src="@{/commom/js/jquery.min.js}"></script>
    <script th:src="@{/js/qrcode.js}"></script>
</head>
<body>
<div id="code"></div>
<div id="succer" hidden>支付成功</div>
</body>
<script>
    var qrcode = new QRCode(document.getElementById("code"), {
        width: 200,//设置宽高
        height: 200
    });
    qrcode.makeCode('[[${code_url}]]');

    //ajax长轮询 5s 循环一次
    var siv = setInterval(function () {
        $.ajax({
            type: "POST",
            url: "/wechat/pay/queryOrder",
            data: '[[${out_tradeno}]]',
            dataType: 'json',
            contentType: 'application/json',
            success: function (res) {
                if (res.code == 0) {
                    $("#code").hide();
                    $("#succer").show();
                    clearInterval(siv); //成功后
                }
            }, error: function () {
            }
        })
    }, 5000);

</script>
</html>

开发即注意事项:

附录:

①:写法有很多种,个人觉得如果有多余的可以使用Websocket的方式进行支付成功通知,Websocket相比于Ajax长轮询会在性能上有所提升,避免每次多余的前后端交互,可以参考下此Websocket写法进行替换。

②:在本人的笔记中采用了SDK中部分工具类提取的方式,移除了原SDK中一些用不上的代码(开发中也可参考其写法自行进行需要的接口封装),如果说需要用到支付中很多功能的朋友可以直接引用SDK的代码然后通过继承类WXPayConfig的方式进行调用SDK中WXPayUtil类中已写好的方法即可,官网的SDK对微信支付开发者文档中给出的API进行了封装。

SDK调用示例
配置类MyConfig:
import com.github.wxpay.sdk.WXPayConfig;
import java.io.*;

public class MyConfig implements WXPayConfig{

    private byte[] certData;

    public MyConfig() throws Exception {
        String certPath = "/path/to/apiclient_cert.p12";
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getAppID() {
        return "wx8888888888888888";
    }

    public String getMchID() {
        return "12888888";
    }

    public String getKey() {
        return "88888888888888888888888888888888";
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}
统一下单:
import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "腾讯充值中心-QQ会员充值");
        data.put("out_trade_no", "2016090910595900000012");
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", "12");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
上一篇下一篇

猜你喜欢

热点阅读