weixin-java-pay实现APP微信支付与退款

pom依赖:

        
            com.github.binarywang
            weixin-java-pay
            3.6.0
            
                
                    com.github.binarywang
                    qrcode-utils
                
            
        

application.yml配置:

wx:
  app-id: 
  app-secret: 
  mch-id: 
  mch-key: 
  notify-url: 
  refund-notify-url:  # 微信退款通知回调接口
  key-path: classpath:cert/apiclient_cert.p12

 读取配置信息,初始化配置,两个文件:

@Configuration
@ConfigurationProperties(prefix = "wx")
@Data
public class WxProperties {

    private String appId;

    private String mchId;

    private String mchKey;

    private String notifyUrl;

    private String keyPath;

    private String refundNotifyUrl;

}
@Configuration
public class WxConfig {

    private final WxProperties properties;

    public WxConfig(WxProperties properties) {
        this.properties = properties;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setKeyPath(properties.getKeyPath());
        payConfig.setTradeType("APP");
        payConfig.setSignType("MD5");
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}

微信支付/退款/查询:

/**
     * 预支付
     * @param request
     * @return
     */
    public WxPayAppOrderResult wxPrepay(PayRequest request) {

        // TODO 先保存支付请求历史记录

        WxPayAppOrderResult result = null;

        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setOutTradeNo(request.getPaySn());
        orderRequest.setTotalFee((request.getTotalFee());
        orderRequest.setBody(request.getBody());
        orderRequest.setSpbillCreateIp(request.getIp());
        orderRequest.setDeviceInfo(request.getDeviceInfo()); // 非必填
        orderRequest.setDetail(request.getDetail()); // 非必填
        orderRequest.setAttach(request.getAttach()); // 非必填

        try {
            result = wxPayService.createOrder(orderRequest);
        } catch (WxPayException e) {
            log.error("微信支付-生成预付款订单错误", e);
        }
        return result;
    }

    /**
     * 支付通知
     * @param xmlResult
     * @return
     */
    public String wxPayNotify(@RequestBody String xmlResult) {
        WxPayOrderNotifyResult result;
        try {
            result = wxPayService.parseOrderNotifyResult(xmlResult);

            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())) {
                log.error(xmlResult);
                throw new WxPayException("微信支付-通知失败!");
            }
            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
                log.error(xmlResult);
                throw new WxPayException("微信支付-通知失败!");
            }
        } catch (WxPayException e) {
            log.error("微信支付-通知失败", e);
            return WxPayNotifyResponse.fail(e.getMessage());
        }

        String orderSn = result.getOutTradeNo();
        String thirdTradeId = result.getTransactionId();

        // 支付成功业务处理
        return WxPayNotifyResponse.success("OK");
    }

    /**
     * 支付订单查询
     * @param transactionId 微信返回订单号
     * @param paySn         支付流水号
     * @return
     */
    public String wxOrderQuery(String transactionId, String paySn) {
        WxPayOrderQueryResult result = null;
        try {
            result = wxPayService.queryOrder(transactionId, paySn);

            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())) {
                throw new WxPayException("微信支付-查询失败!");
            }
            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
                throw new WxPayException("微信支付-查询失败!");
            }
        } catch (WxPayException e) {
            log.error("微信支付-查询失败,params:" + result, e);
            return null;
        }

        if (!WxPayConstants.WxpayTradeStatus.SUCCESS.equals(result.getTradeState())) {
            // 查询失败
        }
        // 查询成功业务处理
    }

    /**
     * 微信退款
     * @param body
     * @return
     */
    public String wxRefund(RefundRequest body) {
        WxPayRefundRequest refundRequest = new WxPayRefundRequest();
        refundRequest.setOutTradeNo(body.getPaySerial());
        refundRequest.setOutRefundNo(body.getPaySerial());
        refundRequest.setTotalFee(refundFee);
        refundRequest.setRefundFee(refundFee);
        refundRequest.setRefundDesc("商品退款");
        refundRequest.setNotifyUrl(wxProperties.getRefundNotifyUrl());

        try {
            WxPayRefundResult result = wxPayService.refund(refundRequest);

            if (REFUND_SUCCESS.equals(result.getReturnCode()) && REFUND_SUCCESS.equals(result.getResultCode())) {
                // 退款成功
            } else {
                // 退款失败
            }
        } catch (WxPayException e) {
            e.printStackTrace();
        }
    }

    /**
     * 微信退款通知
     * @return
     */
    public String wxRefundNotify(@RequestBody String xmlResult) {
        WxPayRefundNotifyResult result;
        try {
            result = wxPayService.parseRefundNotifyResult(xmlResult);

            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())) {
                log.error(xmlResult);
                throw new WxPayException("微信退款-通知失败!");
            }
            if (!WxPayConstants.RefundStatus.SUCCESS.equals(result.getReqInfo().getRefundStatus())) {
                log.error(xmlResult);
                throw new WxPayException("微信退款-通知失败");
            }
        } catch (WxPayException e) {
            log.error("微信退款-通知失败", e);
            return WxPayNotifyResponse.fail(e.getMessage());
        }
        WxPayRefundNotifyResult.ReqInfo reqInfo = result.getReqInfo();
        reqInfo.getOutTradeNo();

        // 退款成功业务处理
        return WxPayNotifyResponse.success("OK");
    }

 

你可能感兴趣的:(springboot,java)