支付宝:验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配

来源:csdn博客 分类: 文章浏览史 发布时间:2020-09-18 17:52:58 最后更新:2020-09-18 浏览:157
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-09-18 17:52:58

springboot项目,公钥证书方式,进行支付宝退款时,支付宝返回:“验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配”。返回值里面,sub_code值是isv.invalid-signature。

使用AlipayClient的sdkExecute和execute都不好使,改为certificateExecute,问题解决。

以下是我的代码:

        AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(paySn);
        model.setRefundAmount(payAmount);
        model.setRefundReason("商品退款");
        refundRequest.setBizModel(model);

        try {
            AlipayTradeRefundResponse response = alipayClient.certificateExecute(refundRequest);
            if (response.isSuccess() && response.getFundChange().equalsIgnoreCase("Y")) {
                // 退款成功
            } else {
                // 退款失败
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error(" - aliRefund - ", e);
        }
@Configuration
public class AliPayConfig {

    private final AliPayProperties properties;

    public AliPayConfig(AliPayProperties properties) {
        this.properties = properties;
    }

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException, FileNotFoundException {
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        certAlipayRequest.setAppId(properties.getAppId());
        certAlipayRequest.setPrivateKey(properties.getPrivateKey());
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset(AlipayConstants.CHARSET_UTF8);
        certAlipayRequest.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
        certAlipayRequest.setCertPath(ResourceUtils.getFile(properties.getAppCertPath()).getAbsolutePath());
        certAlipayRequest.setAlipayPublicCertPath(ResourceUtils.getFile(properties.getAlipayCertPath()).getAbsolutePath());
        certAlipayRequest.setRootCertPath(ResourceUtils.getFile(properties.getAlipayRootCertPath()).getAbsolutePath());
        return new DefaultAlipayClient(certAlipayRequest);
    }
}
@Configuration
@ConfigurationProperties(prefix = "alipay")
@Data
public class AliPayProperties {

    private String appId;

    private String privateKey;

    private String notifyUrl;

    private String appCertPath;

    private String alipayCertPath;

    private String alipayRootCertPath;
}

 

php技术微信