微信app支付和微信网页支付 java

微信app支付和微信网页支付大致相同,但是细节上还是有些区分的

 

1.微信网页支付代码

 

 @PostMapping(value = "wxPay")
    @ApiOperation("微信充值")
    @ResponseBody
    public synchronized ResultModel wxPay(HttpServletRequest request,String accessToken,double amount) {
        ZcUser user = (ZcUser) JedisUtil.getObject(accessToken);
        if (null!=user) {
            ZcPay zcPay = new ZcPay();
            zcPay.setCreateTime(LocalDateTime.now());

            BigDecimal bg = new BigDecimal(amount);
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            zcPay.setPayMoney(f1);
            String orderNum = UUID.randomUUID().toString().replaceAll("-", "");
            zcPay.setPayNo(orderNum);
            zcPay.setUserId(user.getUserId());
            zcPayService.save(zcPay);
            Map resultMap = getWxPayvip(orderNum, f1, request,user.getUserOpenId());
            return ResultModel.success(resultMap);
        }
        return  ResultModel.failure(102,"请先登录");
    }
 public Map getWxPayvip(String orderNum, Double amount, HttpServletRequest request,String openid) {
        Props props = new Props("config.properties", "utf-8");
        WxPayApiConfig apiConfig = null;
        try {
            apiConfig = WxPayApiConfigKit.getApiConfig("wx5bb27f7eed2c1292");
        } catch (Exception ex) {
            apiConfig = WxPayApiConfig.New()
                    .setAppId("wx5bb27f7eed2c1292")
                    .setMchId(props.getStr("shopNo"))
                    .setPaternerKey(props.getStr("shopSecret")).setNotifyUrl(props.getStr("notifyUrl"))
                    .setPayModel(WxPayApiConfig.PayModel.BUSINESSMODEL);//PayModel.BUSINESSMODEL:普通商户模式  PayModel.SERVICEMODE:服务商模式
            WxPayApiConfigKit.setThreadLocalWxPayApiConfig(apiConfig);
        }
        Map resultMap = null;
        Map maps = new HashMap<>();
        maps.put("appid", apiConfig.getAppId());
        maps.put("openid",openid);
        maps.put("mch_id", apiConfig.getMchId());
        maps.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
        maps.put("body", "body");
        maps.put("out_trade_no", orderNum);
        amount = amount * 100;
        maps.put("total_fee", amount.intValue() + "");
        maps.put("spbill_create_ip", IpKit.getRealIp(request));
        maps.put("notify_url",apiConfig.getNotifyUrl());
        maps.put("trade_type", "JSAPI");
        maps.put("sign", PaymentKit.createSign(maps, apiConfig.getPaternerKey()));
        String orderResult = WxPayApi.pushOrder(false, maps);
        resultMap = PaymentKit.xmlToMap(orderResult);
        if ("SUCCESS".equals(resultMap.get("return_code"))) {
            Map signMap = new HashMap<>();
            signMap.put("appId", resultMap.get("appid"));
            signMap.put("nonceStr", UUID.randomUUID().toString().replaceAll("-", ""));
            signMap.put("package", "prepay_id="+resultMap.get("prepay_id"));
            //System.out.println(resultMap.get("prepay_id"));
           // signMap.put("partnerid", apiConfig.getPaternerKey());
            signMap.put("signType","MD5");
            signMap.put("timeStamp", (System.currentTimeMillis() / 1000) + "");
            String sign = PaymentKit.createSign(signMap, apiConfig.getPaternerKey());
            signMap.put("sign", sign);
            return signMap;
        }
        return resultMap;
    }

 

2.微信app支付

 

 @PostMapping("pay")
    @ResponseBody
    public Result pay(String taskName, String orderNum, Double amount, Integer payType, HttpServletRequest request) throws IOException, AlipayApiException {
        if (1 == payType) {
            String sign = getAlipaySign(taskName, orderNum, amount);
            return ResultUtil.success(sign);
        } else {
            Map resultMap = getWxPay(orderNum, amount, request);
            return ResultUtil.success(resultMap);
        }
    }

 

    public Map getWxPay(String orderNum, Double amount, HttpServletRequest request) {
        Props props = new Props("wx.properties", "utf-8");
        WxPayApiConfig apiConfig = null;
        try {
            apiConfig = WxPayApiConfigKit.getApiConfig("自己的appid");
        } catch (Exception ex) {
            apiConfig = WxPayApiConfig.New()
                    .setAppId("自己的appid")
                    .setMchId(props.getStr("wx.mchId"))
                    .setPaternerKey(props.getStr("wx.partnerKey")).setNotifyUrl(props.getStr("wx.notifyUrl"))
                    .setPayModel(WxPayApiConfig.PayModel.BUSINESSMODEL);//PayModel.BUSINESSMODEL:普通商户模式  PayModel.SERVICEMODE:服务商模式
            WxPayApiConfigKit.setThreadLocalWxPayApiConfig(apiConfig);
        }

        Map resultMap = null;
        Map maps = new HashMap<>();
        maps.put("appid", apiConfig.getAppId());
        maps.put("mch_id", apiConfig.getMchId());
        maps.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
        maps.put("body", "xxx");
        maps.put("out_trade_no", orderNum);
        amount = amount * 100;
        maps.put("total_fee", amount.intValue() + "");
        maps.put("spbill_create_ip", IpKit.getRealIp(request));
        maps.put("notify_url", apiConfig.getNotifyUrl());
        maps.put("trade_type", "APP");
        maps.put("sign", PaymentKit.createSign(maps, apiConfig.getPaternerKey()));
        String orderResult = WxPayApi.pushOrder(false, maps);
        resultMap = PaymentKit.xmlToMap(orderResult);
        if ("SUCCESS".equals(resultMap.get("return_code"))) {
            Map signMap = new HashMap<>();
            signMap.put("appid", resultMap.get("appid"));
            signMap.put("partnerid", apiConfig.getPaternerKey());
            signMap.put("prepayid", resultMap.get("prepay_id"));
            signMap.put("noncestr", resultMap.get("nonce_str"));
            signMap.put("timestamp", (System.currentTimeMillis() / 1000) + "");
            signMap.put("package", "Sign=WXPay");
            String sign = PaymentKit.createSign(signMap, apiConfig.getPaternerKey());
            signMap.put("sign", sign);
            return signMap;
        }
        return resultMap;

    }

微信支付回掉地址

/**
     * 微信的回调接口
     * @param request
     * @return
     */
    @PostMapping("wxNotifyUrlvip")
    @ApiOperation("微信回调接口,不需要绑的")
    @ResponseBody
    public String wxNotifyUrlvip(HttpServletRequest request) {
        String xmlMsg = HttpKit.readData(request);
        Map params = PaymentKit.xmlToMap(xmlMsg);
        String result_code = params.get("result_code");
        String return_code = params.get("return_code");
//		// 微信支付订单号
        String transaction_id = params.get("transaction_id");
//		// 商户订单号
        String out_trade_no = params.get("out_trade_no");

        //判断订单是否完成业务
        ZcPay order = zcPayService.getOne(new QueryWrapper().eq("payNo", out_trade_no));
        if (order == null) {
            System.err.println("微信支付回调通知异常,订单不存在:" + out_trade_no);
            return "failure";
        }
        Integer state = order.getIsSuccess();
        if (state == 1) {
            //发送通知等
            Map xml = new HashMap<>(16);
            xml.put("return_code", "SUCCESS");
            xml.put("return_msg", "OK");
            return PaymentKit.toXml(xml);
        }
        String paternerKey = props.getStr("shopSecret");
        if (PaymentKit.verifyNotify(params, paternerKey) && ("SUCCESS").equals(result_code) && "SUCCESS".equals(return_code)) {
            //更新订单信息
            System.out.println("支付成功============================================================");
            order.setIsSuccess(1);
            zcPayService.updateById(order);//更新订单状态为 1
      


        
       
           

            //发送通知等
            Map xml = new HashMap<>(16);
            xml.put("return_code", "SUCCESS");
            xml.put("return_msg", "OK");
            return PaymentKit.toXml(xml);
        } else {
            System.err.println("验证签名失败");
            Map xml = new HashMap<>(16);
            xml.put("return_code", "FAIL");
            xml.put("return_msg", "FAIL");
            return PaymentKit.toXml(xml);
        }
    }

 

你可能感兴趣的:(微信app支付和微信网页支付 java)