微信支付步骤

微信支付步骤

1.此接口可以返回base64加密后的支付码信息以及订单编号信息
@ApiOperation(value = "根据挂号单号支付")
@ApiImplicitParam(name = "appointNo", value = "挂号单号", required = true)
@PostMapping("/toAppointPay")
public ResponseInfo toAppointPay(String appointNo, @LoginUser User user, HttpServletRequest request,
      HttpServletResponse response) {
   BaseAssert.isBlankOrNull(appointNo, "挂号订单不能为空");
   AppointOrder order = appointOrderService.getByAppointNo(appointNo);
   if (null == order || order.getUserId() != user.getId()) {
      return ResponseInfo.businessFail("订单信息有误");
   }
   if (order.getPayState() != OrderPayStateEnum.UNPAY.getCode()) {
      return ResponseInfo.businessFail("支付状态不正确");
   }
   String ip = IPUtils.getIpAddr(request);
   //根据订单信息以及回调地址微信配置ip地址等信息订单编号、支付二维码信息封装到map中
   Map<String, Object> map = appointOrderService.appointPayOrder(new WxPayConfig(), resourceConfig.getUserUrl() + NotifyConfig.APPOINT_ORDER,
         order, ip);
   return ResponseInfo.success(map);
}
2.此业务逻辑就是生成base64二维码信息以及订单号的
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> appointPayOrder(WXPayConfig wxPayConfig, String notifyUrl, AppointOrder order, String ip) {
   Map<String, Object> map = new HashMap<>();
   if (null == order.getId() || order.getId() == 0) {
      // 首次支付
      String appointNo = CommonUtil.getOrderNo("GH", 6);
      order.setAppointNo(appointNo);
      order.setCreateTime(LocalDateTime.now());
      order.setState(0);
      order.setDoctorDelete(Constant.STATE_NORMAL);
      order.setPayState(OrderPayStateEnum.UNPAY.getCode());
      order.setEvalState(Constant.ORDER_EVAL_STATE_NOT_EVALUATED);
      appointOrderMapper.insert(order);
   }
   PayRecord payRecord = new PayRecord();
   payRecord.setOrderId(order.getId());
   payRecord.setOutTradeNo(IdUtil.simpleUUID());
   payRecord.setPayType(Constant.PAY_TYPE_WX);
   payRecord.setFee(order.getFee());
   payRecord.setOrderType(Constant.ORDER_TYPE_APPOINT);
   payRecord.setState(ThirdPayStateEnum.UNPAY.getCode());
   payRecord.setCreateTime(LocalDateTime.now());
   payRecordMapper.insert(payRecord);

   BigDecimal money = order.getFee().scaleByPowerOfTen(2);//001?
   //调用微信支付接口,根据支付类型返回对应的支付链接地址
   Map<String, String> result = WxPay.unifiedorder(wxPayConfig, "民福康互联网医院-预约挂号", payRecord.getOutTradeNo(),
         String.valueOf(money), ip, TradeTypeEnum.NATIVE.getValue(), notifyUrl, null);
   String codeUrl = null;
   if (result.get("return_code").equals("SUCCESS")) {
      codeUrl = result.get("code_url");
   }
   //根据支付链接生成base64格式的链接
   String base64Img = WxPay.generateBase64Png(codeUrl);
   map.put("appointNo", order.getAppointNo());
   map.put("base64Img", base64Img);
   return map;
}
3.此类是封装微信支付的工具类
package com.api.common.pay;

import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSONObject;
import com.api.common.constant.ConfigConstant;
import com.api.common.enums.TradeTypeEnum;
import com.api.common.utils.HttpClientUtil;
import com.api.common.utils.MD5Util;
import com.api.common.validator.ValidatorUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
public class WxPay {
   
   private static boolean initialized = false;
   
   /**
    * 统一下单
    * @param myConfig
    * @param body        支付信息
    * @param tradeNo          商户订单号
    * @param totalFee    金额,单位 分
    * @param ip
    * @param tradeType   JSAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
    * @param notifyUrl   回调地址
    * @param openid      小程序用户标识
    * @return
    */
    public static Map<String, String> unifiedorder(WXPayConfig myConfig, String body, String tradeNo, 
          String totalFee, String ip, String tradeType, String notifyUrl, String openid) {
        Map<String, String> resp = new HashMap<>();
        WXPay wxpay = new WXPay(myConfig);
        Map<String, String> data = new HashMap<>();
        data.put("body", body);
        data.put("out_trade_no", tradeNo);
        data.put("fee_type", "CNY");
        data.put("total_fee", totalFee);
        data.put("spbill_create_ip", ip);
        data.put("trade_type", tradeType);
        data.put("notify_url", notifyUrl);
        data.put("time_expire", LocalDateTime.now().plusMinutes(15).format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        if (TradeTypeEnum.JSAPI.getValue().equals(tradeType)){
            data.put("openid", openid);
        }
        if (TradeTypeEnum.NATIVE.getValue().equals(tradeType)) {
         data.put("product_id", tradeNo);
      }
        try {
            resp = wxpay.unifiedOrder(data);
            String preperyId;
            String sign;
            Set keyset;
            List<String> list;
            StringBuilder signBuffer;
            if (TradeTypeEnum.JSAPI.getValue().equals(tradeType)) {
                preperyId = resp.get("prepay_id").toString();
                resp = new HashMap<>();
                resp.put("appId", myConfig.getAppID());
                resp.put("signType", "MD5");
                resp.put("package", "prepay_id=" + preperyId);
                resp.put("nonceStr", WXPayUtil.generateNonceStr());
                resp.put("timeStamp", String.valueOf(System.currentTimeMillis()/1000));
                keyset = resp.keySet();
                list = new ArrayList<>(keyset);
                Collections.sort(list);
                signBuffer = new StringBuilder();
                for (int i = 0; i < list.size(); i++) {
                    signBuffer.append(list.get(i) + "=" + resp.get(list.get(i)) + "&");
                }
                signBuffer.append("key=" + myConfig.getKey());
                sign = MD5Util.MD5Encode(signBuffer.toString(), "utf-8").toUpperCase();
                resp.put("paySign", sign);
            }
            if (TradeTypeEnum.APP.getValue().equals(tradeType)){
                preperyId = resp.get("prepay_id").toString();
                resp = new HashMap<>();
                resp.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
                resp.put("appid", myConfig.getAppID());
                resp.put("partnerid", myConfig.getMchID());
                resp.put("prepayid", preperyId);
                resp.put("package", "Sign=WXPay");
                resp.put("noncestr", WXPayUtil.generateNonceStr());
                keyset = resp.keySet();
                list = new ArrayList<>(keyset);
                Collections.sort(list);
                signBuffer = new StringBuilder();
                for (int i = 0; i < list.size(); i++) {
                    signBuffer.append(list.get(i) + "=" + resp.get(list.get(i)) + "&");
                }
                signBuffer.append("key=" + myConfig.getKey());
                sign = MD5Util.MD5Encode(signBuffer.toString(), "utf-8").toUpperCase();
                resp.put("sign", sign);
            }
            log.info(resp.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 查询订单
     * @param myConfig
     * @param tradeNo  商户订单号
     * @return
     */
    public static Map<String, String> query(WXPayConfig myConfig, String tradeNo) {
        Map<String, String> resp = new HashMap<>();
        WXPay wxpay = new WXPay(myConfig);
        Map<String, String> data = new HashMap<>();
        data.put("out_trade_no", tradeNo);
        try {
            resp = wxpay.orderQuery(data);
            log.info(resp.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 订单退款
     * @param myConfig
     * @param tradeNo       商户订单号
     * @param refundNo      退款单号
     * @param totalFee      订单金额,单位 分
     * @param refundFee     退款金额,单位 分
     * @param refundReason  退款原因
     * @return
     */
    public static boolean refund(WXPayConfig myConfig, String tradeNo, String refundNo, 
          String totalFee, String refundFee, String refundReason) {
        Map<String, String> resp;
        try {
            WXPay wxpay = new WXPay(myConfig);
            Map<String, String> data = new HashMap<>();
            data.put("total_fee", totalFee);
            data.put("refund_fee", refundFee);
            data.put("out_trade_no", tradeNo);
            data.put("out_refund_no", refundNo);
            data.put("refund_desc", refundReason);
            resp = wxpay.refund(data);
            log.info(resp.toString());
            if ("SUCCESS".equals(resp.get("result_code"))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 微信支付成功回调
     */
    public static void payNotify(HttpServletRequest request, HttpServletResponse response, BasePayOkCallBack payNotifyCallBack) {
        try {
            String returnXml = IOUtils.toString(request.getInputStream(), StringPool.UTF_8);
            log.info(returnXml);
            if (ValidatorUtil.isNotNull(returnXml)) {
                Map<String, String> notifyMap = WXPayUtil.xmlToMap(returnXml);
                if ("SUCCESS".equals(notifyMap.get("result_code"))) {
                    String outTradeNo = notifyMap.get("out_trade_no");
                    String transactionId = notifyMap.get("transaction_id");
                    log.info("商户订单号:{},微信支付单号:{}", outTradeNo, transactionId);
                    if (payNotifyCallBack != null) {
                        payNotifyCallBack.payCallBack(outTradeNo, transactionId);
                    }
                }
                StringBuffer xmlBuffer = new StringBuffer();
                xmlBuffer.append("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
                response.setContentType("application/json;charset=utf-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().print(xmlBuffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 回调处理
     */
    public static abstract class BasePayOkCallBack {
        public abstract void payCallBack(String outTradeNo, String transactionId);
    }
    
    /**
     * 生成支付二维码
     */
    public static String generateBase64Png(String content) {
       byte[] png = QrCodeUtil.generatePng(content, 300, 300);
      String base64 = "data:image/png;base64," + Base64.encode(png);
      return base64;
    }
    
//    public static void writerPayImage(HttpServletResponse response, String content) throws Exception {
//        ServletOutputStream out = response.getOutputStream();
//        try {
//            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
//            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
//            hints.put(EncodeHintType.MARGIN, 0);
//            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
//            MatrixToImageWriter.writeToStream(bitMatrix, "jpg", out);
//        } catch (Exception e) {
//            throw new BusinessException("生成二维码失败!");
//        } finally {
//            if (out != null) {
//                out.flush();
//                out.close();
//            }
//        }
//    }
   
   /**
     * 生成小程序码
     */
    public static String getQrCode(String scene, String page, int width, String filePath, String requestPath) {
        String fileUrl = null;
        try {
            String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + ConfigConstant.MINIPROGRAM_APP_ID + "&secret="
                    + ConfigConstant.MINIPROGRAM_APP_SECRET;
            JSONObject jsonObject = HttpClientUtil.get(tokenUrl);
            String access_token = jsonObject.getString("access_token");
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            // 提交模式
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            paramJson.put("scene", scene);
            paramJson.put("page", page);
            paramJson.put("width", width);
            paramJson.put("auto_color", true);
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            // 保存小程序码到服务器
            InputStream inputStream = httpURLConnection.getInputStream();
            byte[] getData = readInputStream(inputStream);
            String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
            File folder = new File(filePath + format);
          if (!folder.isDirectory()) {
             folder.mkdirs();
          }
          String filename = UUID.randomUUID().toString() + ".jpg";
          File file = new File(folder, filename);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(getData);
            if (fos != null) {
                fos.close();
            }
            fileUrl = requestPath + "/uploadFile/" + format + "/" + filename;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileUrl;
    }
    
    private static byte[] readInputStream(InputStream inputStream) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bos.toByteArray();
    }
   
   /**
     * 获取小程序微信用户信息
     */
    public static String getWxInfo(String code) {
        try {
            String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + ConfigConstant.MINIPROGRAM_APP_ID + "&secret=" + ConfigConstant.MINIPROGRAM_APP_SECRET + "&js_code="
                    + code + "&grant_type=authorization_code";
            String result = IOUtils.toString(new URL(url), StringPool.UTF_8);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 微信手机号解析
     */
    public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) {
        initialize();
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            Key sKeySpec = new SecretKeySpec(keyByte, "AES");
            // 初始化
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void initialize() {
        if (initialized) {
            return;
        }
        Security.addProvider(new BouncyCastleProvider());
        initialized = true;
    }

    /**
     * 生成iv
     */
    public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        return params;
    }

}
4.此接口供用户扫码后回调的接口
  @ApiOperation(value = "预约挂号支付回调")
  @PostMapping("/wxpay/appointPayNotify")
  @IgnoreAuth
  public void appointPayNotify(HttpServletRequest request, HttpServletResponse response) {
//用户支付完成,微信端会调用此接口将商户号以及支付单号带入回调接口的payCallBack
   WxPay.payNotify(request, response, new WxPay.BasePayOkCallBack(){
   @Override
   public void payCallBack(String outTradeNo, String transactionId) {
      //等待微信支付通知后操作支付记录表以及挂号订单表后回到payNotify中继续执行后续操作
      appointOrderService.payOrderSuccess(outTradeNo, transactionId);
   }
   });
  }
5.此业务是用户支付成功后数据保存到相关的记录表中
@Override
@Transactional(rollbackFor = Exception.class)
public void payOrderSuccess(String outTradeNo, String transactionId) {
   QueryWrapper<PayRecord> qw = new QueryWrapper<>();
   qw.lambda().eq(PayRecord::getOutTradeNo, outTradeNo);
   PayRecord payRecord = payRecordMapper.selectOne(qw);
   if (null == payRecord) {
      return;
   }
   if (payRecord.getState() != 1) {
      return;
   }
   AppointOrder order = getById(payRecord.getOrderId());
   order.setState(AppoOrderStateEnum.WAIT.getCode());
   order.setPayState(OrderPayStateEnum.SUCCESS.getCode());
   order.setPayTime(LocalDateTime.now());
   appointOrderMapper.updateById(order);

   payRecord.setTransactionId(transactionId);
   payRecord.setState(ThirdPayStateEnum.SUCCESS.getCode());
   payRecord.setUpdateTime(LocalDateTime.now());
   payRecordMapper.updateById(payRecord);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值