All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.business.pay.converter.WePayConvert Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.business.pay.converter;

import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import matrix.boot.common.exception.ServiceException;
import matrix.business.common.utils.BigDecimalUtil;
import matrix.business.pay.builder.request.QueryRefundRequest;
import matrix.business.pay.constants.WePayConstant;
import matrix.business.pay.entity.MatrixBusinessPayEntity;
import matrix.business.pay.enums.PayMode;
import matrix.business.pay.dto.PayDto;
import matrix.business.pay.dto.RefundDto;

/**
 * 微信支付转换
 * @author wangcheng
 */
public class WePayConvert {

    /**
     * 微信统一下单请求转换
     * @param payMode 支付方式
     * @param payVo 支付参数
     * @param notifyUrl 通知地址
     * @return 微信统一下单请求
     */
    public static WxPayUnifiedOrderRequest convertPayRequest(PayMode payMode, PayDto payVo, String notifyUrl) {
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest()
                .setProductId(payVo.getOrderId())
                .setBody(payVo.getTitle())
                .setDetail(payVo.getDesc())
                .setOutTradeNo(payVo.getPayId())
                .setTotalFee(BigDecimalUtil.yuanToCents(payVo.getPrice()).intValue())
                .setSpbillCreateIp(payVo.getClientIp())
                .setOpenid(payVo.getOpenId());
        request.setNotifyUrl(notifyUrl);
        request.setSignType("MD5");
        if (PayMode.PC.equals(payMode)) {
            payVo.setQrCodeWidth(300L);
            request.setTradeType(WePayConstant.NATIVE_PAY_CODE);
        } else if (PayMode.H5.equals(payMode)) {
            request.setTradeType(WePayConstant.H5_PAY_CODE);
        } else if (PayMode.APP.equals(payMode)) {
            request.setTradeType(WePayConstant.APP_PAY_CODE);
        } else if (PayMode.QrCode.equals(payMode)) {
            request.setTradeType(WePayConstant.NATIVE_PAY_CODE);
        } else if (PayMode.WE_JSAPI.equals(payMode)) {
            request.setTradeType(WePayConstant.JSAPI_PAY_CODE);
        } else {
            throw new ServiceException("No Pay Mode!");
        }
        return request;
    }

    /**
     * 微信支付查询请求
     * @param payId 支付ID
     * @return 微信支付查询请求
     */
    public static WxPayOrderQueryRequest convertPayQueryRequest(String payId) {
        WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
        request.setOutTradeNo(payId);
        return request;
    }

    /**
     * 微信支付退款请求转换
     * @param refundVo 退款参数
     * @param payEntity 支付实体
     * @param notifyUrl 通知地址
     * @return 微信支付退款请求
     */
    public static WxPayRefundRequest convertRefundRequest(RefundDto refundVo, MatrixBusinessPayEntity payEntity, String notifyUrl) {
        WxPayRefundRequest request = new WxPayRefundRequest();
        request.setTransactionId(refundVo.getOutTradeNo());
        request.setOutTradeNo(refundVo.getPayId());
        request.setOutRefundNo(refundVo.getRefundId());
        request.setTotalFee(BigDecimalUtil.yuanToCents(payEntity.getActualPrice()).intValue());
        request.setRefundFee(BigDecimalUtil.yuanToCents(refundVo.getRefundAmount()).intValue());
        request.setNotifyUrl(notifyUrl);
        return request;
    }

    /**
     * 微信支付退款查询请求转换
     * @param queryRefundRequest 查询退款请求
     * @return 微信支付退款查询请求
     */
    public static WxPayRefundQueryRequest convertQueryRefundRequest(QueryRefundRequest queryRefundRequest) {
        WxPayRefundQueryRequest request = new WxPayRefundQueryRequest();
        request.setOutTradeNo(queryRefundRequest.getPayId());
        request.setOutRefundNo(queryRefundRequest.getRefundId());
        return request;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy