微信支付步骤
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);
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);
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");
}
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;
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;
}
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;
}
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 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());
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;
}
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) {
WxPay.payNotify(request, response, new WxPay.BasePayOkCallBack(){
@Override
public void payCallBack(String outTradeNo, String transactionId) {
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);
}