申请退款代码示例
本章主要介绍申请退款的 Java SDK 代码示例。
代码示例
public void refund(AcopayRefundRequestModel refundRequestModel) {
// 翼辉支付平台公钥
String acopayPublicKey = "<acopayPublicKey>";
// 商户私钥
String mchPrivateKey = "<yourMchPrivateKey>";
// 商户号
String mchNo = "<yourMchNo>";
// 商户退款结果通知地址
String refundNotifyUrl = "<yourRefundNotifyUrl>";
// 根据商户实际业务进行参数校验
// 根据商户订单规则生成退款单号
refundRequestModel.setMchRefundNo(AcopayCommonUtil.getRandomString(40));
// 填写商户的退款通知地址
refundRequestModel.setNotifyUrl(refundNotifyUrl);
try {
// 创建 AcopayClient 实例
AcopayClient acopayClient = new DefaultAcopayClient(acopayPublicKey, mchPrivateKey, mchNo);
// 发起退款请求
AcopayResponse<AcopayRefundResponseModel> refundAcopayResponse = acopayClient.refund(refundRequestModel);
if (refundAcopayResponse.statusSuccessful()){
log.info("退款接口返回:{}", refundAcopayResponse);
// 获取业务数据
AcopayRefundResponseModel data = refundAcopayResponse.getData();
// 业务请求成功处理
} else {
// 业务请求失败处理
log.info("退款接口业务失败,返回状态码:{}, 返回信息:{}, 异常信息:{}",
refundAcopayResponse.getStatus(), refundAcopayResponse.getMessage(),
refundAcopayResponse.getFieldErrors());
}
} catch (AcopayException e) {
// 进行异常处理
log.error("退款请求异常", e);
}
}