com.wechat.pay.java.service.ecommercerefund.EcommerceRefundService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wechatpay-java Show documentation
Show all versions of wechatpay-java Show documentation
A Java SDK for WeChat Pay APIv3
// Copyright 2021 Tencent Inc. All rights reserved.
//
// 电商退款API
//
// 境内电商退款功能相关API文档
//
// API version: 1.1.8
// Code generated by WechatPay APIv3 Generator based on [OpenAPI
// Generator](https://openapi-generator.tech); DO NOT EDIT.
package com.wechat.pay.java.service.ecommercerefund;
import static com.wechat.pay.java.core.http.UrlEncoder.urlEncode;
import static com.wechat.pay.java.core.util.GsonUtil.toJson;
import static java.util.Objects.requireNonNull;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.http.Constant;
import com.wechat.pay.java.core.http.DefaultHttpClientBuilder;
import com.wechat.pay.java.core.http.HostName;
import com.wechat.pay.java.core.http.HttpClient;
import com.wechat.pay.java.core.http.HttpHeaders;
import com.wechat.pay.java.core.http.HttpMethod;
import com.wechat.pay.java.core.http.HttpRequest;
import com.wechat.pay.java.core.http.HttpResponse;
import com.wechat.pay.java.core.http.JsonRequestBody;
import com.wechat.pay.java.core.http.MediaType;
import com.wechat.pay.java.core.http.QueryParameter;
import com.wechat.pay.java.core.http.RequestBody;
import com.wechat.pay.java.service.ecommercerefund.model.CreateRefundRequest;
import com.wechat.pay.java.service.ecommercerefund.model.CreateReturnAdvanceRequest;
import com.wechat.pay.java.service.ecommercerefund.model.QueryRefundByOutRefundNoRequest;
import com.wechat.pay.java.service.ecommercerefund.model.QueryRefundRequest;
import com.wechat.pay.java.service.ecommercerefund.model.QueryReturnAdvanceRequest;
import com.wechat.pay.java.service.ecommercerefund.model.Refund;
import com.wechat.pay.java.service.ecommercerefund.model.Refund4Create;
import com.wechat.pay.java.service.ecommercerefund.model.ReturnAdvance;
/** EcommerceRefundService服务 */
public class EcommerceRefundService {
private final HttpClient httpClient;
private final HostName hostName;
private EcommerceRefundService(HttpClient httpClient, HostName hostName) {
this.httpClient = requireNonNull(httpClient);
this.hostName = hostName;
}
/** EcommerceRefundService构造器 */
public static class Builder {
private HttpClient httpClient;
private HostName hostName;
public Builder config(Config config) {
this.httpClient = new DefaultHttpClientBuilder().config(config).build();
return this;
}
public Builder hostName(HostName hostName) {
this.hostName = hostName;
return this;
}
public Builder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
public EcommerceRefundService build() {
return new EcommerceRefundService(httpClient, hostName);
}
}
/**
* 申请退款
*
* @param request 请求参数
* @return Refund4Create
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public Refund4Create createRefund(CreateRefundRequest request) {
String requestPath = "https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply";
CreateRefundRequest realRequest = request;
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest =
new HttpRequest.Builder()
.httpMethod(HttpMethod.POST)
.url(requestPath)
.headers(headers)
.body(createRequestBody(realRequest))
.build();
HttpResponse httpResponse = httpClient.execute(httpRequest, Refund4Create.class);
return httpResponse.getServiceResponse();
}
/**
* 垫付退款回补
*
* @param request 请求参数
* @return ReturnAdvance
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public ReturnAdvance createReturnAdvance(CreateReturnAdvanceRequest request) {
String requestPath =
"https://api.mch.weixin.qq.com/v3/ecommerce/refunds/{refund_id}/return-advance";
CreateReturnAdvanceRequest realRequest = request;
// 添加 path param
requestPath =
requestPath.replace("{" + "refund_id" + "}", urlEncode(realRequest.getRefundId()));
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest =
new HttpRequest.Builder()
.httpMethod(HttpMethod.POST)
.url(requestPath)
.headers(headers)
.body(createRequestBody(realRequest))
.build();
HttpResponse httpResponse = httpClient.execute(httpRequest, ReturnAdvance.class);
return httpResponse.getServiceResponse();
}
/**
* 查询单笔退款(按微信支付退款单号)
*
* @param request 请求参数
* @return Refund
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public Refund queryRefund(QueryRefundRequest request) {
String requestPath = "https://api.mch.weixin.qq.com/v3/ecommerce/refunds/id/{refund_id}";
QueryRefundRequest realRequest = request;
// 添加 path param
requestPath =
requestPath.replace("{" + "refund_id" + "}", urlEncode(realRequest.getRefundId()));
// 添加 query param
QueryParameter queryParameter = new QueryParameter();
if (realRequest.getSubMchid() != null) {
queryParameter.add("sub_mchid", urlEncode(realRequest.getSubMchid()));
}
requestPath += queryParameter.getQueryStr();
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest =
new HttpRequest.Builder()
.httpMethod(HttpMethod.GET)
.url(requestPath)
.headers(headers)
.build();
HttpResponse httpResponse = httpClient.execute(httpRequest, Refund.class);
return httpResponse.getServiceResponse();
}
/**
* 查询单笔退款(按商户退款单号)
*
* @param request 请求参数
* @return Refund
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public Refund queryRefundByOutRefundNo(QueryRefundByOutRefundNoRequest request) {
String requestPath =
"https://api.mch.weixin.qq.com/v3/ecommerce/refunds/out-refund-no/{out_refund_no}";
QueryRefundByOutRefundNoRequest realRequest = request;
// 添加 path param
requestPath =
requestPath.replace("{" + "out_refund_no" + "}", urlEncode(realRequest.getOutRefundNo()));
// 添加 query param
QueryParameter queryParameter = new QueryParameter();
if (realRequest.getSubMchid() != null) {
queryParameter.add("sub_mchid", urlEncode(realRequest.getSubMchid()));
}
requestPath += queryParameter.getQueryStr();
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest =
new HttpRequest.Builder()
.httpMethod(HttpMethod.GET)
.url(requestPath)
.headers(headers)
.build();
HttpResponse httpResponse = httpClient.execute(httpRequest, Refund.class);
return httpResponse.getServiceResponse();
}
/**
* 查询垫付回补结果
*
* @param request 请求参数
* @return ReturnAdvance
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public ReturnAdvance queryReturnAdvance(QueryReturnAdvanceRequest request) {
String requestPath =
"https://api.mch.weixin.qq.com/v3/ecommerce/refunds/{refund_id}/return-advance";
QueryReturnAdvanceRequest realRequest = request;
// 添加 path param
requestPath =
requestPath.replace("{" + "refund_id" + "}", urlEncode(realRequest.getRefundId()));
// 添加 query param
QueryParameter queryParameter = new QueryParameter();
if (realRequest.getSubMchid() != null) {
queryParameter.add("sub_mchid", urlEncode(realRequest.getSubMchid()));
}
requestPath += queryParameter.getQueryStr();
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest =
new HttpRequest.Builder()
.httpMethod(HttpMethod.GET)
.url(requestPath)
.headers(headers)
.build();
HttpResponse httpResponse = httpClient.execute(httpRequest, ReturnAdvance.class);
return httpResponse.getServiceResponse();
}
private RequestBody createRequestBody(Object request) {
return new JsonRequestBody.Builder().body(toJson(request)).build();
}
}