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

net.guerlab.sdk.alipay.utils.NotifyUtils Maven / Gradle / Ivy

The newest version!
package net.guerlab.sdk.alipay.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConstants;
import com.alipay.api.internal.util.AlipaySignature;
import net.guerlab.sdk.alipay.properties.AlipayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
 * 通知回调工具类
 *
 * @author guer
 *
 */
public class NotifyUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotifyUtils.class);

    private NotifyUtils() {

    }

    /**
     * 回调数据通过支付宝公钥进行验签
     *
     * @param properties
     *            支付宝配置
     * @param requestParams
     *            请求参数
     * @return 是否检查通过
     */
    public static boolean rsaCheck(AlipayProperties properties, Map requestParams) {
        try {
            return rsaCheck(properties, requestParams, false);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 回调数据通过支付宝公钥进行验签
     *
     * @param properties
     *            支付宝配置
     * @param requestParams
     *            请求参数
     * @param throwException
     *            检查失败是否抛出异常
     * @return 是否检查通过
     *
     * @throws NullPointerException
     *             当requestParams为空的时候抛出NullPointerException
     * @throws AlipayApiException
     *             支付宝公钥验签失败的时候抛出AlipayApiException
     */
    public static boolean rsaCheck(AlipayProperties properties, Map requestParams,
            boolean throwException) throws AlipayApiException {
        if (requestParams == null) {
            if (throwException) {
                throw new NullPointerException("requestParams cann't be null");
            }
            return false;
        }

        Map params = requestParams.entrySet().stream().filter(NotifyUtils::entryCheck)
                .collect(Collectors.toMap(Entry::getKey, e -> String.join(",", e.getValue())));

        try {
            return AlipaySignature.rsaCheckV1(params, properties.getAlipayPublicKey(), AlipayConstants.CHARSET_UTF8,
                    AlipayConstants.SIGN_TYPE_RSA2);
        } catch (AlipayApiException e) {
            LOGGER.debug(e.getMessage(), e);
            if (throwException) {
                throw e;
            }
        }

        return false;
    }

    private static boolean entryCheck(Entry entry) {
        return entry != null && entry.getKey() != null && entry.getValue() != null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy