2 qq 37203372 qq_37203372 于 2017.08.31 16:49 提问

支付宝退款退款成功返回信息后校验sign报错

发送给支付宝之后返回的信息是正确的,可是报com.alipay.api.AlipayApiException: com.alipay.api.AlipayApiException: sign check fail: check Sign and Data Fail!错误
try {
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.app_id, AlipayConfig.private_key_RSA2, "JSON", AlipayConfig.input_charset, AlipayConfig.public_key_RSA2, "RSA2");
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
AlipayTradeRefundRequest req = new AlipayTradeRefundRequest ();
Map map = new HashMap();
map.put("out_trade_no",order.getOrderId());
map.put("refund_amount",order.getBackMoney().doubleValue());
map.put("out_request_no",order.getBackId());
JSONObject jsonObject = JSONObject.fromObject(map);
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
req.setBizContent(jsonObject.toString());
AlipayTradeRefundResponse response = alipayClient.execute(req);
//调用成功,则处理业务逻辑
System.out.println("返回结果"+response.toString());
if(response.isSuccess()){
return true;
//.....
}

    }catch (Exception e){
        log.error("alipay backPayInfo",e);
    }
    return false;

2个回答

jx_luckyhua
jx_luckyhua   2017.11.21 15:55

支付回调验签都OK,就是交易退款一直出现sign check fail: check Sign and Data Fail!,
金额也退到支付宝了,支付宝的应用私钥、应用公钥和支付宝公钥我都检查了好多遍没问题。

jx_luckyhua
jx_luckyhua   2017.11.21 16:14

我找到问题了,

 AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(outTradeNo);
        model.setRefundAmount(String.valueOf(refundAmount));
        request.setBizModel(model);
        AlipayTradeRefundResponse response;
        try {
            response = alipayClient.execute(request);
            log.debug("ali refund response code = {}, msg = {} ", response.getCode(), response.getMsg());
            return response.isSuccess();
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error("ali refund exception = " + e);
        }
        return Boolean.FALSE;

退款的是使用支付宝公钥的验签,默认使用的是初始化客户端时候的公钥,需要把初始化客户端的公钥修改为支付宝的公钥

 alipayClient = new DefaultAlipayClient(REQUEST_URL, APP_ID, RSA_PRIVATE_KEY, FORMAT, CHARSET, **ALIPAY_PUBLIC_KEY**, SIGN_TYPE);

应用公钥实际没有用处。

Csdn user default icon
上传中...
上传图片
插入图片