商户通过转账接口发起付款后,在用户确认收款之前可以通过该接口撤销付款。该接口返回成功仅表示撤销请求已受理,系统会异步处理退款等操作,以最终查询单据返回状态为准。
1、微信文档
https://pay.weixin.qq.com/doc/v3/merchant/4012716458
2、接口说明
- 支持商户:
【普通商户】 - 请求方式:
【POST】/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/{out_bill_no}/cancel - 请求域名:
【主域名】https://api.mch.weixin.qq.com
3、代码实现
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.http.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import java.util.HashMap;
@Slf4j
public class WxPayUtils {
/**
* 撤销转账
*
* @param outBillNo 商户单号
*/
public void cancelTransferBills(String outBillNo) {
OkHttpClient okHttpClient = new OkHttpClient();
HttpClient httpClient = new DefaultHttpClientBuilder()
.config(rsaAutoCertificateConfig())
.okHttpClient(okHttpClient)
.build();
HttpHeaders headers = new HttpHeaders();
headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
HashMap<Object, Object> map = new HashMap<>();
JsonRequestBody build = new JsonRequestBody.Builder()
.body(JSONUtil.toJsonStr(map))
.build();
HttpRequest executeSendGetHttpRequest = new HttpRequest.Builder()
.httpMethod(HttpMethod.POST)
.url("https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/" + outBillNo + "/cancel")
.headers(headers)
.body(build)
.build();
try {
HttpResponse<JSONObject> execute = httpClient.execute(executeSendGetHttpRequest, JSONObject.class);
JSONObject responseBody = execute.getServiceResponse();
log.error("撤销转账返回:{}", responseBody.toJSONString());
} catch (ServiceException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* API安全加密配置
*/
private RSAAutoCertificateConfig rsaAutoCertificateConfig() {
return new RSAAutoCertificateConfig.Builder()
// 商户号
.merchantId("xxxxxxxxxxx")
// 商户API证书私钥的存放路径
.privateKeyFromPath("v3/apiclient_key.pem")
// 商户API证书序列号
.merchantSerialNumber("xxxxxxxxxxx")
// APIv3密钥
.apiV3Key("xxxxxxxxxxx")
.build();
}
}
请求响应得到:
{
"body": {
"out_bill_no": "plfk2020042013",
"transfer_bill_no": "1330000071100999991182020050700019480001",
"state": "CANCELING",
"update_time": "2015-05-20T13:29:35.120+08:00"
}
}
如您在阅读中发现不足,欢迎留言!!!