引入依赖
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.3</version>
</dependency>
小程序代码 具体实现,只需要替换参数
@Override public AjaxResult wxPayOrder(Double money, String orderNo) { try { DyUser loginUser = tokenServices.getLoginUser(); JSONObject order = new JSONObject(); order.put("appid", payConfig.getAppId()); order.put("mchid", payConfig.getMchId()); order.put("description", "充值-店员商城VIP会员"); order.put("out_trade_no", orderNo); order.put("notify_url", payConfig.getNotifyUrl()); JSONObject amount = new JSONObject(); amount.put("total", (long)(money * 100)); amount.put("currency", "CNY"); order.put("amount", amount); JSONObject payer = new JSONObject(); payer.put("openid", loginUser.getOpenId()); order.put("payer", payer); PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(ResourceUtils.getFile(payConfig.getMerchantPrivateKey()))); //不需要传入微信支付证书了 AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(payConfig.getMchId(), new PrivateKeySigner(payConfig.getMerchantSerialNumber(), merchantPrivateKey)), payConfig.getApiKey().getBytes("utf-8")); WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create() .withMerchant(payConfig.getMchId(), payConfig.getMerchantSerialNumber(), merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier)); // ... 接下来,你仍然可以通过builder设置各种参数,来配置你的HttpClient // 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新 HttpClient httpClient = builder.build(); HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"); httpPost.addHeader("Accept", "application/json"); httpPost.addHeader("Content-type","application/json; charset=utf-8"); httpPost.setEntity(new StringEntity(order.toJSONString(), "UTF-8")); // 后面跟使用Apache HttpClient一样 HttpResponse response = httpClient.execute(httpPost); String bodyAsString = EntityUtils.toString(response.getEntity()); JSONObject bodyAsJSON = JSONObject.parseObject(bodyAsString); if(bodyAsJSON.containsKey("code")) { return new AjaxResult(1, bodyAsJSON.getString("message")); } final String prepay_id = bodyAsJSON.getString("prepay_id"); final String timeStamp = String.valueOf(System.currentTimeMillis()); final String nonceStr = RandomStringGenerator.getRandomStringByLength(32); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(payConfig.getAppId() + "\n"); stringBuffer.append(timeStamp + "\n"); stringBuffer.append(nonceStr + "\n"); stringBuffer.append("prepay_id="+prepay_id+"\n"); Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(merchantPrivateKey); signature.update(stringBuffer.toString().getBytes("UTF-8")); byte[] signBytes = signature.sign(); String paySign = Base64.encodeBytes(signBytes); JSONObject params = new JSONObject(); params.put("appId", payConfig.getAppId()); params.put("timeStamp", timeStamp); params.put("nonceStr", nonceStr); params.put("prepay_id", prepay_id); params.put("signType", "RSA"); params.put("paySign", paySign); return AjaxResult.success(params); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
退款也一样需要依赖,具体实现差不多,注意证书路径
@Override
public AjaxResult wxRefund(String orderNo) {
try {
DyPaymentHistory dyorders = historyMapper.getHistoryOrderNo(orderNo);
//判断订单书否存在
if(dyorders==null){
return AjaxResult.error(301001,"订单不存在");
}
if(dyorders.getPayType()!=3){
return AjaxResult.error(301001,"订单状态异常");
}
JSONObject order = new JSONObject();
order.put("out_trade_no", orderNo);
//生成退款单号
long refudnOrderNo = IdUtil.getSnowflake(1, 2).nextId();
order.put("out_refund_no", refudnOrderNo+"");
//订单金额信息
JSONObject amount = new JSONObject();
BigDecimal num = new BigDecimal("100");
BigDecimal paymoney = dyorders.getPayMoney().multiply(num);
amount.put("refund", paymoney);
amount.put("total", paymoney);
amount.put("currency", "CNY");
order.put("amount", amount);
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(ResourceUtils.getFile(payConfig.getMerchantPrivateKey())));
//不需要传入微信支付证书了
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(payConfig.getMchId(), new PrivateKeySigner(payConfig.getMerchantSerialNumber(), merchantPrivateKey)),
payConfig.getApiKey().getBytes("utf-8"));
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(payConfig.getMchId(), payConfig.getMerchantSerialNumber(), merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
HttpClient httpClient = builder.build();
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type","application/json; charset=utf-8");
httpPost.setEntity(new StringEntity(order.toJSONString(), "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
String bodyAsString = EntityUtils.toString(response.getEntity());
JSONObject bodyAsJSON = JSONObject.parseObject(bodyAsString);
if(bodyAsJSON.containsKey("code")) {
return new AjaxResult(1, bodyAsJSON.getString("message"));
}
dyorders.setPayType(2L);
historyMapper.updateDyPaymentHistory(dyorders);
return AjaxResult.success(bodyAsJSON);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
具体异常抛出我还没处理,根据自己业务定义吧