需要接入微信支付了
微信那边需要支付商户号.
百度搜微信支付,按照官网操作就可以了.
直接上代码:
1 - 引入依赖
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-pay</artifactId> <version>3.8.0</version> </dependency>
2 - 配置
import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WxpayConfig { @Value("${***}") private String appId; @Value("${***}") private String mchId; @Value("${***}") private String key; @Bean public WxPayService wxService() { WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(appId); payConfig.setMchId(mchId); payConfig.setMchKey(key); WxPayService wxPayService = new WxPayServiceImpl(); wxPayService.setConfig(payConfig); return wxPayService; } }
3 - 微信PC支付Native方式,接口就不写了,调用方法而已,返回的是用于生成二维码的url
// 注入wxNotifyUrl,wxTradeType,wxRequestIp,具体参数含义请参考官方说明
// 微信支付 public String wxNativePay(WXpayPara para) throws WxPayException { WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); orderRequest.setDeviceInfo("WEB"); orderRequest.setNotifyUrl(wxNotifyUrl); orderRequest.setOutTradeNo(uuid()); orderRequest.setSpbillCreateIp(wxRequestIp); orderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(para.getTotalFee().toString())); orderRequest.setProductId(outTradeNo); orderRequest.setBody(para.getBody()); orderRequest.setTradeType(wxTradeType);
// 可以进行订单记录相关处理
// 返回一个地址,给前端生成二维码,用户扫码支付 return result.getCodeUrl(); }
4 - 微信支付回调,如果不成功就会按照时间回调,官网有说明
// 注入WxPayService
public void wxPayNotify(HttpServletRequest request) throws IOException, WxPayException, SQLException { String xmlRequest = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding()); WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlRequest); String outTradeNo = notifyResult.getOutTradeNo();
// 成功返回 - SUCCESS,就是"SUCCESS".equals(code) if (!isSuccess(notifyResult.getResultCode())) { throw new WxPayException("WX Notify status is not SUCCESS"); } // 根据outTradeNo查询订单是否存在,因为我在支付的时候先创建订单,省略查询代码
if (order == null) { throw new SQLException(); } // 如果已支付,就不做处理了,因为可能回调多次 if (order.isPay()) { return; } String totalFee = BaseWxPayResult.fenToYuan(notifyResult.getTotalFee()); BigDecimal totalAmount = new BigDecimal(totalFee);
// 判断金额是否一致 if (order.getAmount().compareTo(totalAmount) != 0) { throw new WxPayException("WX Notify totalFee is not equal"); } String tradeNo = notifyResult.getTransactionId(); // 记录交易号,省略代码 }
5 - 接口,返回消息给微信
// 微信异步通知 @RequestMapping(value = "***") public String wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
try { payService.wxPayNotify(request); String resultXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> "; response.setContentType("text/xml"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); out.write(resultXml.getBytes()); out.flush(); out.close(); return resultXml; } catch (Exception e) { e.printStackTrace(); return "failure"; } }
微信接入完成,经过测试,注意配置参数.