本文介绍了“二维码付款”的代码。其他微信支付方式的代码都在源码中。
一、准备开发所需的账号以及配置信息
首先想要接入微信支付我们需要两个玩意:
一是公众号/小程序/企业微信(开发用的)这个是为了获取 APPID
一是微信支付商户(收钱用的) 获取 api_key mch_id
1、前往:https://mp.weixin.qq.***/ (微信公众平台)注册一个应用,类型只能是:公众号/小程序/企业微信,注册完成需要完成”微信认证“(微信需要收取300元)。
2、前往:https://pay.weixin.qq.***(微信支付商户平台)注册一个商户,支付成功后的钱就会在这个账号里面。
1、APPID:应用id也就是 公众号/小程序的ID
2、Api_key: 对应的APIv2密钥
3、mch_Id:商户ID (收钱的商家ID)对应的是 【微信支付商户号】
4.将申请的下来的APPID绑定到商户号下,添加成功后再次到工作号里面
【广告与服务—>微信支付】这个时候会看到关联申请,同意就可以了。到这一步前置工作就完成了
二、准备环境
项目采用SpringBoot
微信支付有两种版本:V3和V2,本文的接入版本为V2
1、导入jar包
1.1微信支付jar包
<dependency>
<groupId>***.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
1.2导入hutool工具类jar包
<dependency>
<groupId>***.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.12</version>
</dependency>
2、设置开发参数
如果自己就是商户 那么可以将参数设置到配置文件application.yml中,如果是多商户则建立商户收款配置表 将信息维护到数据库中
在application.yml,设置好开发参数
pay:
appid: wx123456789a439 #微信公众号appid
api_key: gwxkjvfewvfabvcrxgrawvgs924ceaxj #公众号设置的api密钥
mch_id: 1603596731 #微信商户平台 商户id
本文是多商户
-
数据库参考
-
微信支付工具类
package ***.manage.***mon.utils;
import javax.***.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.***.URL;
/**
* 微信支付工具类
*
*/
public class WxChatPay***monUtil {
/**
* 发送 http 请求
* @param requestUrl 请求路径
* @param requestMethod 请求方式(GET/POST/PUT/DELETE/...)
* @param outputStr 请求参数体
* @return 结果信息
*/
public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
try {
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取ip
* @param request 请求
* @return ip 地址
*/
public static String getIp(HttpServletRequest request) {
if (request == null) {
return "";
}
String ip = request.getHeader("X-Requested-For");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 从流中读取微信返回的xml数据
* @param httpServletRequest
* @return
* @throws IOException
*/
public static String readXmlFromStream(HttpServletRequest httpServletRequest) throws IOException {
InputStream inputStream = httpServletRequest.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
final StringBuffer sb = new StringBuffer();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
} finally {
bufferedReader.close();
inputStream.close();
}
return sb.toString();
}
/**
* 设置返回给微信服务器的xml信息
* @param returnCode
* @param returnMsg
* @return
*/
public static String setReturnXml(String returnCode, String returnMsg) {
return "<xml><return_code><![CDATA[" + returnCode + "]]></return_code><return_msg><![CDATA[" + returnMsg + "]]></return_msg></xml>";
}
}
- 微信支付接口地址
package ***.manage.***mon.utils;
/**
* 微信支付接口地址
*
*/
public class WeChatPayUrl {
//统一下单预下单接口url
public static final String Uifiedorder = "https://api.mch.weixin.qq.***/pay/unifiedorder";
//订单状态查询接口URL
public static final String Orderquery = "https://api.mch.weixin.qq.***/pay/orderquery";
//订单申请退款
public static final String Refund = "https://api.mch.weixin.qq.***/secapi/pay/refund";
//付款码 支付
public static final String MicroPay = "https://api.mch.weixin.qq.***/pay/micropay";
//微信网页授权 获取“code”请求地址
public static final String GainCodeUrl = "https://open.weixin.qq.***/connect/oauth2/authorize";
}
- 钱 工具类
package ***.manage.***mon.utils;
import ***.manage.***mon.object.YouNumberUtil;
import java.math.BigDecimal;
/**
* 钱 工具类
*
* Created by YouHan on 2019-06-28 09:12:00
* Copyright © 2019 YouHan All rights reserved.
*/
public class MoneyUtils {
public static final String YUAN = "元";
public static final String FEN = "分";
/**
* 元转分
*
* @param s
* @return java.lang.Integer
* @date 2020/9/10 9:03
* @author YouHan
*/
public static Integer yuanToFen(String s) {
if (!YouNumberUtil.isNumber(s)) {
return 0;
}
return new BigDecimal(s).multiply(new BigDecimal(100)).intValue();
}
/**
* 处理分
*
* @param s
* @return java.lang.Integer
* @author YouHan
* @date 2022/7/23
*/
public static Integer handleFen(String s) {
if (!YouNumberUtil.isNumber(s)) {
return 0;
}
return new BigDecimal(s).intValue();
}
/**
* 分转元
* 可以为正负小数(这里保留2位小数)
*
* @param s
* @return java.lang.String
* @date 2020/9/10 9:01
* @author YouHan
*/
public static String fenToYuan(String s) {
if (!YouNumberUtil.isNumber(s) || "0".equals(s) || "-0".equals(s)) {
return "0.00";
}
return new BigDecimal(s)
.divide(new BigDecimal(100))
.setScale(2, BigDecimal.ROUND_DOWN)
.toString();
}
/**
* 处理元
* 可以为正负小数(这里保留2位小数)
*
* @param s
* @return java.lang.String
* @author YouHan
* @date 2022/7/23
*/
public static String handleYuan(String s) {
if (!YouNumberUtil.isNumber(s) || "0".equals(s) || "-0".equals(s)) {
return "0.00";
}
return new BigDecimal(s)
.setScale(2, BigDecimal.ROUND_DOWN)
.toString();
}
public static void main(String[] args) {
System.out.println(yuanToFen("10.00"));
}
}
- 数字 client
package ***.manage.***mon.object;
import org.apache.***mons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* String client
*
* Created by YouHan on 2019-09-11 08:57:56
* Copyright © 2019 YouHan All rights reserved.
*/
public class YouStringUtil {
/**
* 下划线
*/
public static final Pattern LINE = Pattern.***pile("_(\\w)");
/**
* 驼峰
*/
public static final Pattern HUMP = Pattern.***pile("[A-Z]");
/**
* 添加内容
*
* @param content
* @param length
* @return java.lang.String
* @author YouHan
* @date 2021/6/17 9:59
*/
public static String appendContent(String content, int length) {
if (length <= 0) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i ++) {
sb.append(content);
}
return sb.toString();
}
/**
* 添加前缀内容
*
* @param s
* @param content
* @param length
* @return java.lang.String
* @date 2019-08-12 09:53
* @author YouHan
*/
public static String appendPrefixContent(String s, String content, int length) {
if (length <= 0) {
return null;
}
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < length; i ++) {
sb.append(content, 0, content.length());
}
return sb.toString();
}
/**
* 添加后缀内容
*
* @param s
* @param content
* @param length
* @return java.lang.String
* @date 2019-08-12 09:56
* @author YouHan
*/
public static String appendSuffixContent(String s, String content, int length) {
if (length <= 0) {
return null;
}
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < length; i ++) {
sb.append(content);
}
return sb.toString();
}
/**
* Set 转 String
*
* @param stringSet
* @return java.lang.String
* @author YouHan
* @date 2021/6/3 9:26
*/
public static String setToString(Set<String> stringSet) {
return setToString(stringSet, null);
}
/**
* Set 转 String
*
* @param stringSet
* @param regex
* @return java.lang.String
* @date 2021/6/3 9:21
* @author YouHan
*/
public static String setToString(Set<String> stringSet, String regex) {
// 参数校验
if (CollectionUtils.isEmpty(stringSet)) {
return null;
}
if (StringUtils.isBlank(regex)) {
regex = ",";
}
// List to String
StringBuilder sb = new StringBuilder(stringSet.size());
for (String s : stringSet) {
sb.append(s).append(regex);
}
// 返回结果
return sb.substring(0, sb.length() - 1);
}
/**
* 字符串列表转字符串
*
* @author YouHan
* @generatedDate: 2018/10/9 17:25
* @param stringList 要转换的字符串列表
* @return
*/
public static String listToString(List<String> stringList) {
return listToString(stringList, null);
}
/**
* 字符串列表转字符串
*
* @author YouHan
* @generatedDate: 2018/10/9 17:25
* @param stringList 要转换的字符串列表
* @return
*/
public static String listToString(List<String> stringList, String regex) {
// 参数校验
if (CollectionUtils.isEmpty(stringList)) {
return null;
}
if (StringUtils.isBlank(regex)) {
regex = ",";
}
// List to String
StringBuilder sb = new StringBuilder(stringList.size());
for (String s : stringList) {
sb.append(s).append(regex);
}
// 返回结果
return sb.substring(0, sb.length() - 1);
}
/**
* 字符串转列表
*
* @param s
* @return java.client.List<java.lang.String>
* @date 2019-09-11 09:11
* @author YouHan
*/
public static List<String> stringToList(String s) {
/**
* 参数校验
*/
if (StringUtils.isBlank(s)) {
return null;
}
return stringToList(s, null);
}
/**
* 字符串转列表
*
* @param s
* @param regex 分割规则,默认为逗号
* @return java.client.List<java.lang.String>
* @date 2019-09-11 09:11
* @author YouHan
*/
public static List<String> stringToList(String s, String regex) {
/**
* 参数校验
*/
if (StringUtils.isBlank(s)) {
return null;
}
/**
* 默认逗号隔开
*/
if (StringUtils.isBlank(regex)) {
regex = ",";
}
/**
* 去除首尾空格
*/
String blankString = " ";
while (s.startsWith(blankString)) {
s = s.substring(1);
}
while (s.endsWith(blankString)) {
s = s.substring(0, s.length() -1);
}
/**
* 返回结果列表
*/
List<String> resultList = new ArrayList<>();
/**
* 只有单个元素
*/
if (!s.contains(regex)) {
resultList.add(s);
return resultList;
}
String[] strings = s.split(regex);
for (String e : strings) {
resultList.add(e);
}
return resultList;
}
/**
* 过滤逗号
* @param s
* @return
*/
public static String filter***maString(String s) {
// 数据为空校验
if (StringUtils.isEmpty(s)) {
return null;
}
// 去除 并列逗号
s = s.replace(",,", ",");
// 去除 首逗号
if (s.startsWith(",")) {
s = s.substring(1, s.length() - 1);
}
// 去除 尾逗号
if (s.endsWith(",")) {
s = s.substring(0, s.length() - 1);
}
return s;
}
/**
* 是否包含中文(包括中文标点符号和空格)
*
* @param s
* @return boolean
* @date 2020/9/9 13:30
* @author YouHan
*/
public static Boolean isContainChinese(String s) {
/**
* 参数校验
*/
if (StringUtils.isBlank(s)) {
return false;
}
if (s.contains(" ")) {
return true;
}
/**
* 中文正则表达式
*/
String regex = "[\u4e00-\u9fa5]";
if (s.matches(regex)) {
return Boolean.TRUE;
}
/**
* 中文标点符号处理
*/
char[] chars = s.toCharArray();
for (char c : chars) {
if (isChinesePunctuation(c)) {
return true;
}
}
return false;
}
/**
* 过滤中文(包括标点符号和空格)
*
* @param s
* @return java.lang.String
* @date 2020/9/9 14:08
* @author YouHan
*/
public static String filterChinese(String s) {
/**
* 参数校验
*/
if (StringUtils.isBlank(s)) {
return "";
}
s = s.replace(" ", "");
if (!isContainChinese(s)) {
return s;
}
/**
* 过滤中文字符
*/
char[] chars = s.toCharArray();
StringBuilder sb = new StringBuilder(chars.length);
for (char c : chars) {
if (isContainChinese(String.valueOf(c))) {
continue;
}
sb.append(c);
}
return sb.toString();
}
/**
* 判断是否为中文标点符号
*
* @param c
* @return java.lang.Boolean
* @date 2020/9/9 13:43
* @author YouHan
*/
public static boolean isChinesePunctuation(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.CJK_***PATIBILITY_FORMS
|| ub == Character.UnicodeBlock.VERTICAL_FORMS) {
return true;
}
return false;
}
/**
* 获取 UUID
*
* @param
* @return java.lang.String
* @date 2021/4/9 14:08
* @author YouHan
*/
public static String getUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 安全比较(可防止时序攻击 timing attack)
*/
public static boolean safeEqual(String a, String b) {
if (StringUtils.isBlank(a) || StringUtils.isBlank(b)) {
return false;
}
if (a.length() != b.length()) {
return false;
}
int equal = 0;
for (int i = 0; i < a.length(); i++) {
equal |= a.charAt(i) ^ b.charAt(i);
}
return equal == 0;
}
/**
* 驼峰转下划线
*
* @param s
* @return java.lang.String
* @date 2021/5/6 22:20
* @author YouHan
*/
public static String humpToLine(String s) {
Matcher matcher = HUMP.matcher(s);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
if (sb.toString().startsWith("_")) {
sb.deleteCharAt(0);
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 下划线转驼峰
*
* @param s
* @return java.lang.String
* @date 2021/5/6 22:21
* @author YouHan
*/
public static String li***oHump(String s) {
s = s.toLowerCase();
Matcher matcher = LINE.matcher(s);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 生成加密的内容
*
* @param s
* @return java.lang.String
* @author YouHan
* @date 2021/6/17 10:10
*/
public static String hide(String s) {
/**
* 1
* 1*
* 1**
* 1***
* 1***5
* 12***6
* 12***67
* 123***78
* 123***789
* 123****890
* 123*****8901
*/
if (s.isEmpty() || s.length() == 1) {
return s;
}
if (s.length() == 2) {
return s.substring(0, 1) + "*";
}
if (s.length() == 3 || s.length() == 4) {
return s.substring(0, 1) + appendContent("*", s.length() - 1);
}
if (s.length() == 5) {
return s.substring(0, 1) + "***" + s.substring(4);
}
if (s.length() == 6 || s.length() == 7) {
return s.substring(0, 2) + appendContent("*", 3) + s.substring(5);
}
if (s.length() == 8) {
return s.substring(0, 3) + "***" + s.substring(6);
}
return s.substring(0, 3) + appendContent("*", s.length() - 6) + s.substring(s.length() - 3);
}
}
- String client
package ***.manage.***mon.object;
import org.apache.***mons.lang3.StringUtils;
import java.util.concurrent.ThreadLocalRandom;
/**
* 数字 client
*
* Created by YouHan on 2020-09-09 13:28:40
* Copyright © 2021 YouHan All rights reserved.
*/
public class YouNumberUtil {
/**
* 整数正则表达式
*/
public static final String INTEGER_REGEX = "^[-\\+]?[\\d]*$";
/**
* 数字正则表达式
*/
public static final String NUMBER_REGEX = "^-?\\d+(\\.\\d+)?$";
/**
* 是否是整数
*
* @param s
* @return java.lang.Boolean
* @date 2020/9/12 8:38
* @author YouHan
*/
public static boolean isInteger(String s) {
if (StringUtils.isBlank(s)) {
return false;
}
return s.matches(INTEGER_REGEX);
}
/**
* 判断给定字符串是否为十六进制数
*
* @param value 值
* @return 是否为十六进制
*/
public static boolean isHex(String value) {
final int index = (value.startsWith("-") ? 1 : 0);
if (value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index)) {
try {
Long.decode(value);
} catch (NumberFormatException e) {
return false;
}
return true;
}
return false;
}
/**
* 是否是数字(包括小数)
*
* @param s
* @return java.lang.Boolean
* @date 2020/9/9 14:01
* @author YouHan
*/
public static boolean isNumber(String s) {
if (StringUtils.isBlank(s)) {
return false;
}
return s.matches(NUMBER_REGEX);
}
/**
* 十进制转十六进制
*
* @param n 十进制数
* @return java.lang.String
* @date 2019/4/8 09:22
* @author YouHan
*/
public static String intToHex(Integer n) {
if (null == n) {
return null;
}
return String.format("%X", n);
}
/**
* 十进制转十六进制
*
* @param n 十进制数
* @return java.lang.String
* @date 2019/4/8 09:22
* @author YouHan
*/
public static String longToHex(Long n) {
if (null == n) {
return null;
}
return String.format("%X", n);
}
/**
* 十进制转十六进制
*
* @param n
* @param length
* @return java.lang.String
* @date 2019-08-12 09:56
* @author YouHan
*/
public static String intToHexPrefix(Integer n, Integer length) {
if (null == n) {
return null;
}
if (null == length || length <= 0) {
return null;
}
String result = String.format("%X", n);
if (result.length() < length) {
result = YouStringUtil.appendPrefixContent(result, "0", length - result.length());
}
return result;
}
/**
* 十进制转十六进制
*
* @param n
* @param length
* @return java.lang.String
* @date 2019-08-12 09:56
* @author YouHan
*/
public static String longToHexPrefix(Long n, Integer length) {
if (null == n) {
return null;
}
if (null == length || length <= 0) {
return null;
}
String result = String.format("%X", n);
if (result.length() < length) {
result = YouStringUtil.appendPrefixContent(result, "0", length - result.length());
}
return result;
}
/**
* 十进制转十六进制
*
* @param n 十进制数
* @return java.lang.String
* @date 2019/4/8 09:22
* @author YouHan
*/
public static String intToHexSuffix(Integer n, Integer length) {
if (null == n) {
return null;
}
if (null == length || length <= 0) {
return null;
}
String result = String.format("%X", n);
if (result.length() < length) {
result = YouStringUtil.appendSuffixContent(result, "0", length - result.length());
}
return result;
}
/**
* 十进制转十六进制
*
* @param n 十进制数
* @return java.lang.String
* @date 2019/4/8 09:22
* @author YouHan
*/
public static String longToHexSuffix(Long n, Integer length) {
if (null == n) {
return null;
}
if (null == length || length <= 0) {
return null;
}
String result = String.format("%X", n);
if (result.length() < length) {
result = YouStringUtil.appendSuffixContent(result, "0", length - result.length());
}
return result;
}
/**
* 十六进制转十进制
*
* @param hex
* @return java.lang.Integer
* @date 2019/4/8 09:49
* @author YouHan
*/
public static Integer hexToInt(String hex) {
Long n = hexToLong(hex);
if (null == n) {
return null;
}
// 超出整数最大值,不予处理
if (Integer.MAX_VALUE < n) {
return null;
}
return Integer.valueOf(String.valueOf(n));
}
/**
* 十六进制转十进制
*
* @param hex
* @return java.lang.Integer
* @date 2019/4/8 09:49
* @author YouHan
*/
public static Long hexToLong(String hex) {
if (StringUtils.isBlank(hex)) {
return null;
}
// 去除前缀为 0 的 十六进制
if (hex.length() > 1 && hex.startsWith("0")) {
hex = hex.substring(1);
}
return Long.valueOf(hex, 16);
}
/**
* 字符串转十六进制
*
* @param s
* @return
*/
public static String stringToHex(String s) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = s.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return sb.toString().trim();
}
/**
* 十六进制转字符串
*
* @param s
* @return
*/
public static String hexToString(String s) {
String str = "0123456789ABCDEF";
char[] hexs = s.toCharArray();
byte[] bytes = new byte[s.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
/**
* 去除末尾多余的 0
*
* @param s
* @return java.lang.String
* @author YouHan
* @date 2021/7/2 15:39
*/
public static String stripTrailingZeros(String s) {
if (StringUtils.isBlank(s)) {
return "0";
}
if (!isNumber(s)) {
return "0";
}
if (!s.contains(".")) {
return s;
}
while (s.endsWith("0")) {
s = s.substring(0, s.length() - 1);
}
if (s.endsWith(".")) {
s = s.substring(0, s.length() - 1);
}
return s;
}
/**
* int 转 String
* 1024以内高效,超出后,正常转换
*/
static int cacheSize = 1024;
static String[] caches = new String[cacheSize];
static {
for (int i = 0; i < cacheSize; i++) {
caches[i] = String.valueOf(i);
}
}
public static String int2String(int data) {
if (data < cacheSize) {
return caches[data];
}
return String.valueOf(data);
}
/**
* 获取几位的 int 随机数
*
* @param length
* @return int
* @author YouHan
* @date 2021/12/19
*/
public static int getRandomInt(int length) {
return (int) ((Math.random() * 9 + 1) * 10 * length);
}
/**
* 获取几位的 long 随机数
*
* @param length
* @return long
* @author YouHan
* @date 2021/12/19
*/
public static long getRandomLong(long length) {
return (long) ((Math.random() * 9 + 1) * 10 * length);
}
/**
* 获取随机数
*
* @param
* @return java.client.concurrent.ThreadLocalRandom
* @author YouHan
* @date 2021/6/3 10:29
*/
public static ThreadLocalRandom getRandom() {
return ThreadLocalRandom.current();
}
/**
* 获取缓存穿透时间(单位秒),最长不超过 5 分钟
*
* @param
* @return java.lang.Long
* @date 2021/4/26 9:50
* @author YouHan
*/
public static Long getCachePe***rationTime() {
return Long.valueOf(int2String(getRandom().nextInt(300)));
}
/**
* 获取数据库缓存时间(单位秒),最长不超过 1 小时
*
* @param
* @return java.lang.Long
* @date 2021/4/26 9:50
* @author YouHan
*/
public static Long getDBCacheTime() {
return Long.valueOf(int2String(getRandom().nextInt(3600)));
}
/**
* 十六进制高低位转换
*
* @param hexString
* @return java.lang.String
* @author YouHan
* @date 2021/12/11
*/
public static String hexHighLowPositionConvert(String hexString) {
if (StringUtils.isBlank(hexString) || hexString.length() % 2 != 0) {
return null;
}
StringBuilder result = new StringBuilder();
for (int i = hexString.length() - 2; i >= 0; i = i - 2) {
result.append(hexString.substring(i, i + 2));
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(Long.MAX_VALUE);
}
}
- 上业务代码
Controller
/**
* 调用统一下单接口,并组装生成支付所需参数对象.
*
* @param orderInfoVO 统一下单请求参数
*/
@Operation(summary = "调用统一下单接口")
@PostMapping("/unifiedOrder")
public AjaxResult unifiedOrder(HttpServletRequest request, @RequestBody OrderInfoVO orderInfoVO) {
return orderInfoService.unifiedOrder(request, orderInfoVO);
}
Service
AjaxResult unifiedOrder(HttpServletRequest request, OrderInfoVO orderInfoVO);
ServiceImpl
@Override
public AjaxResult unifiedOrder(HttpServletRequest request, OrderInfoVO orderInfoVO) {
//根据typeId查询支付金额 根据自己的业务逻辑自行处理
SysFunctionType sysFunctionType = sysFunctionTypeMapper.selectFunctionTypeById(orderInfoVO.getTypeId());
//通过customId 查询用户信息 根据自己的业务逻辑自行处理
SysCustom sysCustom = sysCustomMapper.selectSysCustomById(orderInfoVO.getCustomId());
//根据自己的业务逻辑自行处理 OrderInfo为我自己业务中的实体类
OrderInfo orderInfo = new OrderInfo();
orderInfo.setId(orderInfoVO.getOrderId());
//支付类型
orderInfo.setPaymentType(orderInfoVO.getPayType());
//交易类型
orderInfo.setTradeType("NATIVE");
//支付金额(BigDecimal 例子:10.00)
orderInfo.setPaymentPrice(sysFunctionType.getPrice());
orderInfo.setName(sysCustom.getName()+"体检报告");
String body = orderInfo.getName();
body = body.length() > 40 ? body.substring(0,39) : body;
//更新编号防止不同终端微信报重复订单号
orderInfo.setOrderNo(IdUtil.getSnowflake(0,0).nextIdStr());
//公众号
req.put("appid", payConfig.getAppId());
// 商户号
req.put("mch_id", payConfig.getMchId());
// 32位随机字符串
req.put("nonce_str", WXPayUtil.generateNonceStr());
// 商品描述
req.put("body", body);
// 商户订单号
req.put("out_trade_no", orderInfo.getOrderNo());
// 标价金额(分)
req.put("total_fee", String.valueOf(MoneyUtils.yuanToFen(String.valueOf(orderInfo.getPaymentPrice()))));
// 终端IP
req.put("spbill_create_ip", request.getRemoteAddr());
// 回调地址+携带的返回参数 domain 为配置的域名[不可为ip地址]
req.put("notify_url", domain+"/system/order/info/notify-order-wx"+"/"+sysDevice.getTenantId()+"/"+orderInfo.getId()+"/"+orderInfoVO.getTypeId());
// 交易类型
req.put("trade_type", "NATIVE");
// 签名
req.put("attach", String.valueOf(orderInfo.getTenantId()));
try {
// 签名
req.put("sign", WXPayUtil.generateSignature(req, payConfig.getMchKey(), WXPayConstants.SignType.MD5));
String xmlBody = WXPayUtil.generateSignedXml(req, payConfig.getMchKey());
System.err.println(String.format("微信支付预下单请求 xml 格式:\n%s", xmlBody));
String result = WxChatPay***monUtil.httpsRequest(WeChatPayUrl.Uifiedorder, "POST", xmlBody);
this.updateOrderInfo(orderInfo);
System.err.println(String.format("%s", result));
Map<String, String> WxResultMap = WXPayUtil.xmlToMap(result);
WxResultMap.put("orderNo",orderInfo.getOrderNo());
if (ObjectUtil.isNotEmpty(WxResultMap.get("return_code")) && WxResultMap.get("return_code").equals("SU***ESS")) {
if (WxResultMap.get("result_code").equals("SU***ESS")) {
System.out.println("预下单成功");
System.out.println("QrCode:"+WxResultMap.get("code_url"));
return AjaxResult.su***ess(WxResultMap);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
参数疑惑解释:
notify_url:此参数为回调通知地址(公网必须可以访问),当这笔订单用户支付成功之后,”微信“会异步请求你这个地址告诉你 某个订单支付成功了。后面会讲到这个怎么写这个接口 包括如何在本地环境进行测试。
完成上面的代码,简单的一个支付后端接口实现就完成了。
3:题外篇 回调地址
异步回调通知官方文档:https://pay.weixin.qq.***/wiki/doc/api/native.php?chapter=9_7&index=8
简单来说就是:
在一笔订单支付成功之后微信会告诉你的服务器这笔订单支付成功了,然后你就需要根据你的项目业务逻辑进行处理,改变数据库的支付结果,然后发货。所以你需要写一个接口放到你们项目中,让微信来调用你的接口就行了。
回调的接口地址必须是公网可以进行访问的,如果开发中您的项目公网没有办法访问的话,微信是无法调用的。所以我们需要弄一个内网穿透 花生壳:https://hsk.oray.***/(免费)
这个时候把域名地址配置到 application.yml
***pany:
domain: https://33q716k372.imdo.co
4.回调方法
Controller
/**
* 支付回调(微信)
*
* @param xmlData 微信XML数据
* @param tenantId 商家id
* @param orderId 订单id
* @param typeId 类型id
* @return 返回支付结果
*/
@Operation(summary = "支付回调(微信)")
@PostMapping("/notify-order-wx/{tenantId}/{orderId}/{typeId}")
public String notifyOrderWx(HttpServletRequest request, HttpServletResponse response,
@RequestBody String xmlData, @PathVariable("tenantId") Long tenantId,
@PathVariable("orderId") Long orderId,
@PathVariable("typeId") Long typeId) throws IOException {
log.info("支付回调(微信):" + xmlData);
if(tenantId == null || orderId == null || typeId == null){
System.out.println("验签失败");
response.getWriter().write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>");
}
String resXml = "";
try {
//通过商家id查询支付配置
PayConfig payConfig = payConfigService.selectPayConfigByTenantId(tenantId,"1");
Map<String, Object> ResultMap = orderInfoService.WeChatPayCallback(xmlData, payConfig.getMchKey());
Map<String, String> WxResultMapData = new HashMap<>();
if (ResultMap.get("Verify").equals("YES")) {
//验签成功
System.out.println("验签成功");
WxResultMapData = (Map<String, String>) ResultMap.get("data");
System.out.println("WxResultMapData:" + JSONUtil.toJsonStr(WxResultMapData));
log.info("收到微信回调:{}", JSONUtil.toJsonStr(WxResultMapData));
OrderInfo orderInfo = orderInfoService.selectOrderInfoByOrderNo(WxResultMapData.get("out_trade_no"));
System.out.println("通知微信验签成功");
//自信业务逻辑处理
orderInfoService.notifyOrder(orderInfo,tenantId,orderId,typeId,WxResultMapData);
resXml = String.valueOf(ResultMap.get("returnWeChat"));
} else if (ResultMap.get("Verify").equals("NO")) {
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[" + WxResultMapData.get("err_code_des") + "]]></return_msg>" + "</xml> ";
}
}catch (Exception e) {
throw new RuntimeException(e);
}finally {
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
}
return WxPayNotifyResponse.su***ess("成功");
}
Service
public Map<String, Object> WeChatPayCallback(String xmlData, String apiKey);
ServiceImpl
@Override
public Map<String, Object> WeChatPayCallback(String xmlData, String apiKey) {
Map<String, Object> ResultMap = new HashMap<String, Object>();
//解析到微信返回过来的xml数据
try {
//xml转Map
Map<String, String> WxResultMap = WXPayUtil.xmlToMap(xmlData);
//验证签名
boolean SignStatus = WXPayUtil.isSignatureValid(WxResultMap, apiKey);
if (SignStatus) {
//验证成功
//要返回给微信的xml数据
String returnWeChat = WxChatPay***monUtil.setReturnXml("SU***ESS", "OK");
ResultMap.put("Verify", "YES");
ResultMap.put("returnWeChat", returnWeChat);
ResultMap.put("data", WxResultMap);
} else {
//验证失败(表示可能接口被他人调用 需要留意)
ResultMap.put("Verify", "NO");
ResultMap.put("msg", "验签失败。");
}
return ResultMap;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}