微信支付

时间:2019-08-02 18:09:05   收藏:0   阅读:1324

一、支付流程:

  技术图片

二、代码实现:

  1)下载sdk,复制sdk文件和相关依赖;

  2)创建类继承WXPayConfig类,配置账号信息:

public class MyWXPayConfig extends WXPayConfig {
    //商家注册微信支付之后会生成一个应用ID
    String getAppID() {
        return "wx632c8f211f8";
    }
    //商户ID
    String getMchID() {
        return "1497984";
    }

    String getKey() {
        return "sbNCm1JnevqI36LrEaxFwcaT0h";
    }

    InputStream getCertStream() {
        return null;
    }

    IWXPayDomain getWXPayDomain() {

        MyWXPayDomain myWXPayDomain = new MyWXPayDomain();

        return myWXPayDomain;
    }
}/

  3)创建类实现IWXPayDomain接口,配置支付的域名:

public class MyWXPayDomain implements IWXPayDomain {
    public void report(String domain, long elapsedTimeMillis, Exception ex) {}

    public DomainInfo getDomain(WXPayConfig config) {
        DomainInfo domainInfo = new DomainInfo("api.mch.weixin.qq.com",true);
        return domainInfo;
    }
}

  4)下单:

    @RequestMapping("doPay/{orderId}")
    public void doPay(@PathVariable  String orderId , HttpServletResponse response){
        //1.创建配置类对象
        MyWXPayConfig config = new MyWXPayConfig();
        //2.创建支付对象
        WXPay wxpay = null;
        try {
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "付款提示信息");
            //订单编号
            data.put("out_trade_no", orderId);
            //商品ID
            data.put("product_id", orderId);
            //设备信息
            data.put("device_info", "");
            //货币单位: 分
            data.put("fee_type", "CNY");
            // 1分
            data.put("total_fee", "1");
            //对于这样123.12.12.123IP地址将会被记录
            data.put("spbill_create_ip", "123.12.12.123");
            //******需要有一个回调的接口,获取此次微信支付的信息。
            data.put("notify_url", "http://x96viv.natappfree.cc/wxPay/wxPayReturn");
            // 此处指定为扫码支付
            data.put("trade_type", "NATIVE");

            //获取付款链接
            Map<String, String> resp = wxpay.unifiedOrder(data);
            String code_url = resp.get("code_url");

            //二、使用zxing插件生成二维码
            HashMap<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            hints.put(EncodeHintType.MARGIN, 2);

            BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, 200, 200, hints);
            MatrixToImageWriter.writeToStream(bitMatrix, "PNG", response.getOutputStream());
            System.out.println("创建二维码完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  5)回调接口:

@RequestMapping("wxPayReturn")
    public void payReturn(HttpServletResponse response, HttpServletRequest request) throws Exception{
        // 从request中取得输入流
        InputStream inputStream = request.getInputStream();
        Map<String,String> map = XmlUtil.doXMLParse(inputStream);
        String out_trade_no = map.get("out_trade_no");
        if (map.get("result_code").equals("SUCCESS")){
            //1.接收到付款成功信息,使用goeasy推送信息到前端页面;
            GoEasy goEasy = new GoEasy( "http://rest-hangzhou.goeasy.io","BC-d5913514b354466c8a8ad5");
            goEasy.publish(out_trade_no, "支付成功");
            //2.返回一个标准格式的回信到微信服务器;
            response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
        }else {
            GoEasy goEasy = new GoEasy( "http://rest-hangzhou.goeasy.io","BC-d5913514b354466c8a8ad5cded274c0e");
            goEasy.publish(out_trade_no, "支付失败");
        }
    }

  6)回调xml解析 工具类:

public class XmlUtil {

    public static Map doXMLParse(InputStream inputStream) {
        Map m = new HashMap();
        try {
            SAXReader reader = new SAXReader();
            org.dom4j.Document doc = reader.read(inputStream);
            // 得到xml根元素
            Element rootElement = doc.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = rootElement.elements();
            for (Iterator<Element> iterator = elementList.iterator(); iterator.hasNext(); ) {
                Element element = iterator.next();
                String k = element.getName();
                String v = element.getText();
                m.put(k, v);
            }
            //关闭流
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return m;
    }

}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!