目录

请求示例

// 1, 准备信息:商户入网时生成公私钥对,公钥上传到服务端,私钥保存进行签名使用,拿到authId和商户号mercNo
// 本处的参数请替换成实际各商户的对应值
String authId = DemoKeys.MERC_AUTH_ID;
String mercNo = DemoKeys.MERC_NO;
String priKey = DemoKeys.MERC_PRIVATE_KEY;

// 本处填写商户服务器的地址
String merchantServer = "https://testpaycenter.merchantservice.com/";
// callbackUri 与商户实现的接口地址一致,本处仅供参考
String callBackUri = "/v1/refund/result";
CreateRefundOrderRequest refundOrderRequest = CreateRefundOrderRequest.builder()
    .mercRefundOrderNo("N4480210902091825205700246")// 商户退款订单号,商户自行生成,保证唯一性,建议不超过20位
    .sysTransOrderNo("0010001000002021083118590976048000846000262419")// 原交易订单号
    .refundAmount(100L)// 退款金额,可选,不输入时默认全额退款,部分退款时必填,单位为最小货币单位(分)
    .payload("七天无理由退款") // 退款备注信息
    .callbackUrl(merchantServer + callBackUri) // 退款请求回调地址
    .build();

String bodySourceData = JSONObject.toJSONString(refundOrderRequest);

// 2, 签名加密消息体,初始化PayMercAuth对象
String bodySign = RSAUtils.signByRSAWithPSS(bodySourceData, priKey);
System.out.println("1,Build bodySign:" + bodySign);

PayMercAuth payMercAuth = new PayMercAuth();
// traceId用于问题定位,请求的唯一标识,最好以时间加上随机数来
String traceId = KeyGenUtils.getTraceId();
Long time = System.currentTimeMillis();
payMercAuth.setCallerId(mercNo);
payMercAuth.setTraceId(traceId);
payMercAuth.setTime(time);
payMercAuth.setAuthId(authId);

// 4,组织消息头并签名
String headerSourceData = "authId=" + authId + "&bodySign=" + bodySign + "&callerId=" + mercNo + "&time=" + time
    + "&traceId=" + traceId;
System.out.println("headerSign原文:" + headerSourceData);

String headerSign = RSAUtils.signByRSAWithPSS(headerSourceData, priKey);
System.out.println("headerSign:" + headerSign);
payMercAuth.setHeaderSign(headerSign);
payMercAuth.setBodySign(bodySign);
String PayMercAuthJsonStr = JSONObject.toJSONString(payMercAuth);
System.out.println("---------------------------------");
System.out.println("bodySourceData: ");
System.out.println(bodySourceData);
System.out.println("PayMercAuth: ");
System.out.println(PayMercAuthJsonStr);

// 5, 使用Http方式发送组织好的 请求头和请求体
// 发送的body要和待签名的body原文一致,对body是整体签名,对header是对PayMercAuth结构按字段字典排序进行签名
try {
    // 设置退款请求的地址
    String refundUri = "/payment/api/merc/v1/refund/order?mercNo=" + mercNo;
    String url = DemoKeys.HW_SERVER_ADDR + refundUri;
    String response = HttpURLHelper.sendHttpPost(url, PayMercAuthJsonStr, bodySourceData);
    CreateRefundOrderResponse responses = JSONObject.parseObject(response, CreateRefundOrderResponse.class);
    System.out.println(
        "Refund result code:" + responses.getResultCode() + ",result:" + responses.getResultDesc());
} catch (Exception e) {
    e.printStackTrace();
}