package com.skynet.wechat.wxPay.common;
import java.util.Date;
import java.util.Random;
import com.skynet.wechat.api.util.DateUtils;
public class OrderNoUtil {
/**
* 生成微信支付订单号
*
* @return
*/
public static String wxOrderNo() {
// 订单前缀
String temp = DateUtils.getDate(new Date(), "yyyyMMddHHmmssSSS");
// 生成15位随机序列
String randomSte = getRandomString(15);
return temp + randomSte;
}
/**
* 生成微信支付订单号
*
* @return
*/
public static String wdOrderNo(String appid) {
// 订单前缀
String temp = DateUtils.getDate(new Date(), "yyyyMMddHHmm");
// 生成4位随机序列
String randomSte = getRandomInt(4);
return appid + temp + randomSte;
}
private static String getRandomString(int length) { // length表示生成字符串的长度
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString().toUpperCase();
}
private static String getRandomInt(int length) { // length表示生成字符串的长度
String base = "0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString().toUpperCase();
}
// public static void main(String[] args) {
// System.out.println(wxOrderNo());
// }
}
使用方法:
String serialNumber = OrderNoUtil.wxOrderNo(); //生成交易平台流水号
String serialNumber = OrderNoUtil.wdOrderNo(String appid); //生成交易平台流水号 该方法需要一个appId