查询支付结果代码示例
本章主要介绍查询支付结果的 Java SDK 代码示例。
代码示例
public void tradeQuery(String transactionNo, String mchTradeNo) {
// 翼辉支付平台公钥
String acopayPublicKey = "<acopayPublicKey>";
// 商户私钥
String mchPrivateKey = "<yourMchPrivateKey>";
// 商户号
String mchNo = "<yourMchNo>";
// 查询支付订单结果请求参数
AcopayTradeQueryRequestModel tradeQueryRequestModel = new AcopayTradeQueryRequestModel();
// 实际业务中指定查询的翼辉订单号
tradeQueryRequestModel.setTransactionNo(transactionNo);
// 实际业务中查询商户的订单号
tradeQueryRequestModel.setMchTradeNo(mchTradeNo);
AcopayResponse<AcopayTradeQueryResponseModel> tradeQueryAcopayResponse = null;
try {
// 创建 AcopayClient 实例
AcopayClient acopayClient = new DefaultAcopayClient(acopayPublicKey, mchPrivateKey, mchNo);
// 发送交易查询请求
tradeQueryAcopayResponse = acopayClient.tradeQuery(tradeQueryRequestModel);
} catch (AcopayException e) {
// 进行异常处理
log.error("支付结果查询异常", e);
}
if (!Objects.isNull(tradeQueryAcopayResponse)) {
if (tradeQueryAcopayResponse.statusSuccessful()) {
log.info("翼辉支付平台返回:{}", tradeQueryAcopayResponse);
// 获取业务数据
AcopayTradeQueryResponseModel data = tradeQueryAcopayResponse.getData();
// 业务请求成处理
} else {
log.info("支付结果查询业务失败,返回状态码:{},返回信息:{}, 异常信息:{}",
tradeQueryAcopayResponse.getStatus(), tradeQueryAcopayResponse.getMessage(),
tradeQueryAcopayResponse.getFieldErrors());
}
}
}