作者热门文章
- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
在微信小程序中实现一个提现功能 ,提现的钱其实来自系统中的其他功能。
当然这个功能,需要一个微信支付的商户号,并且账户中需要有充足的余额。
注:API证书文件的后缀是.p12
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
withDraw:
apiKey: xxxxxxxx
mchid: xxxxxx
mch_appid: xxxxxxxxx
//此路径代表是在项目的resouce根目录
certPath: /xxxxxxxx.p12
count: 10
quota: 200
@Component
public class WithDrawConfig implements WXPayConfig {
//从yaml注入配置
@Value("${withDraw.mch_appid}")
private String mch_appid;
@Value("${withDraw.apiKey}")
private String apiKey;
@Value("${withDraw.mchid}")
private String mchid;
@Value("${withDraw.certPath}")
private String certPath;
@Override
public String getAppID() {
return mch_appid;
}
@Override
public String getMchID() {
return mchid;
}
@Override
public String getKey() {
return apiKey;
}
@Override
public InputStream getCertStream() {
//获取证书,证书建议放到resource目录下
return this.getClass().getResourceAsStream(certPath);
}
@Override
public int getHttpConnectTimeoutMs() {
return 8000;
}
@Override
public int getHttpReadTimeoutMs() {
return 10000;
}
}
@Slf4j
@Component
@RequiredArgsConstructor
public class WithDrawUtils {
@Value("${withDraw.mch_appid}")
private String mch_appid;
@Value("${withDraw.mchid}")
private String mchid;
@Value("${withDraw.apiKey}")
private String apiKey;
private final WithDrawConfig withDrawConfig;
//生成订单号日期时间+随机字符
public String getOrderNumber() {
SimpleDateFormat date = new SimpleDateFormat("yyyyMMddHHmmss");
return date.format(new Date()) + RandomStringUtils.randomNumeric(4);
}
public Map<String, String> fillRequest(Map<String, String> reqData) throws Exception {
reqData.put("mch_appid", mch_appid);
reqData.put("mchid", mchid);
reqData.put("nonce_str", WXPayUtil.generateNonceStr().toUpperCase());
reqData.put("sign", WXPayUtil.generateSignature(reqData, apiKey, WXPayConstants.SignType.MD5));
return reqData;
}
public String getMd5ByString(String str) {
StringBuilder hexString = new StringBuilder();
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(str.getBytes());
byte[] hash = mdTemp.digest();
for (byte b : hash) {
if ((0xff & b) < 0x10) {
hexString.append("0").append(Integer.toHexString((0xFF & b)));
} else {
hexString.append(Integer.toHexString(0xFF & b));
}
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hexString.toString();
}
/**
* 将对象直接转换成String类型的 XML输出
*
* @param obj
* @return
*/
public String convertToXml(Object obj) {
// 创建输出流
StringWriter sw = new StringWriter();
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FRAGMENT,
Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
public String getRestInstance(String url, String data) throws Exception {
String UTF8 = "UTF-8";
URL httpUrl = new URL(url);
char[] password = mchid.toCharArray();//证书密码
InputStream certStream = withDrawConfig.getCertStream();//获取证书的流
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(certStream, password);
// 实例化密钥库 & 初始化密钥工厂
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), (TrustManager[]) null, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(withDrawConfig.getHttpConnectTimeoutMs());
httpURLConnection.setReadTimeout(withDrawConfig.getHttpReadTimeoutMs());
httpURLConnection.connect();
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data.getBytes(UTF8));
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, UTF8));
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
String resp = stringBuffer.toString();
try {
bufferedReader.close();
} catch (IOException ignored) {
}
try {
inputStream.close();
} catch (IOException ignored) {
}
try {
outputStream.close();
} catch (IOException ignored) {
}
if (certStream != null) {
try {
certStream.close();
} catch (IOException ignored) {
}
}
return resp;
}
}
官方接口文档:【微信支付】付款开发者文档 (qq.com)
//开始提现,生成订单号
String orderNumber = withDrawUtils.getOrderNumber();
//自定义的将提现所需要的参数封装的实体类
WithDrawDTO withDrawDTO = new WithDrawDTO();
withDrawDTO.setPartner_trade_no(orderNumber);
withDrawDTO.setDesc("xxxxx");
withDrawDTO.setAmount(appletWithDrawDTO.getAmount());
//此参数代表,开启真实姓名校验,也可以关闭,详看官方文档的参数说明
withDrawDTO.setCheck_name("FORCE_CHECK");
withDrawDTO.setRe_user_name(appletWithDrawDTO.getName());
//微信小程序用户的openid
withDrawDTO.setOpenid(wxUser.getOpenid());
Map<String, String> params = JSON.parseObject(JSON.toJSONString(withDrawDTO), new TypeReference<Map<String, String>>() {
});
params = withDrawUtils.fillRequest(params);
withDrawDTO.setNonce_str(params.get("nonce_str"));
withDrawDTO.setMchid(params.get("mchid"));
withDrawDTO.setMch_appid(params.get("mch_appid"));
withDrawDTO.setSign(params.get("sign"));
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
String post = withDrawUtils.getRestInstance(url, withDrawUtils.convertToXml(withDrawDTO));
Map<String, String> result = WXPayUtil.xmlToMap(post);
//result为调用接口之后的返回参数,可以根据返回参数判断是否成功
WithDrawEnum resultEnum;
if ("SUCCESS".equals(result.get("result_code"))) {
//提现成功
resultEnum = WithDrawEnum.fromText("SUCCESS");
return resultEnum;
} else {
//提现失败
resultEnum = WithDrawEnum.fromText(result.get("err_code"));
throw new BadRequestException(resultEnum);
}
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum WithDrawEnum {
SUCCESS(0, "提现成功"),
FAIL(400, "提现失败,余额小于提现金额"),
HANDLE_FRE(5000, "操作太频繁"),
NO_AUTH(5001, "没有该接口权限"),
AMOUNT_LIMIT(5002, "金额超限"),
PARAM_ERROR(5003, "参数错误"),
OPENID_ERROR(5004, "Openid错误"),
SEND_FAILED(5005, "付款错误"),
NOTENOUGH(5006, "余额不足"),
SYSTEMERROR(5007, "系统繁忙,请稍后再试。"),
NAME_MISMATCH(5008, "姓名校验出错"),
SIGN_ERROR(5009, "签名错误"),
XML_ERROR(5010, "发送内容出错"),
FATAL_ERROR(5011, "两次发送参数不一致"),
FREQ_LIMIT(5012, "超过频率限制,请稍后再试。"),
MONEY_LIMIT(5013, "已经达到今日付款总额上限/已达到付款给此用户额度上限"),
CA_ERROR(5014, "商户证书校验出错"),
V2_ACCOUNT_SIMPLE_BAN(5015, "无法给未实名用户付款"),
PARAM_IS_NOT_UTF8(5016, "发送参数中包含不规范字符"),
SENDNUM_LIMIT(5017, "该用户今日付款次数超过限制, 如有需要请进入【微信支付商户平台-产品中心-付款到零钱-产品设置】进行修改"),
RECV_ACCOUNT_NOT_ALLOWED(5018, "收款账户不在收款账户列表"),
PAY_CHANNEL_NOT_ALLOWED(5019, "本商户号未配置此功能"),
SEND_MONEY_LIMIT(5020, "已达到今日商户付款额度上限"),
RECEIVED_MONEY_LIMIT(5021, "已达到今日付款给此用户额度上限");
private int code;
private String msg;
public static WithDrawEnum fromText(String text) {
if (text != null) {
for (WithDrawEnum b : WithDrawEnum.values()) {
if (text.equalsIgnoreCase(b.name())) {
return b;
}
}
}
return null;
}
}
微信相关接口的调用,主要还是要耐心看文档的说明,才能实现业务需求。
笙歌 痞性. 剩余 青怏 洒脱 现实 搁浅° 软萌 路途 娼妓 离骚 逐你 微尘 迷魂 ▼ 扎心 堇夏 残年っ ╭淡妆╮ 24K.纯疯 隐痛
我正在尝试不费吹灰之力地实现完全有效的持久性无知。不过我有很多问题: 最简单的选项 这真的很简单——可以像在 SOA 中那样使用 Spring Data 注释对实体进行注释(但让它们真正执行逻辑)?除
我想完成一项简单的任务。 我在可变宽度容器中有一个图像。 容器的宽度可以为 300、400、700 或 900 像素。这是通过媒体查询完成的图像应占据该容器的所有宽度。所以它也将是 300、400、7
我在使用 Storyboards AutoRotation 和 iPhone 时遇到问题。我已经将一个非常简单的项目与 3 个 View Controller 放在一起,并将其上传到 gitHub 以
我是一名优秀的程序员,十分优秀!