Skip to content

Commit

Permalink
收付通合并支付相关接口对接实现(wechatpay-apiv3#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
liulipeng committed Jun 28, 2024
1 parent 7ab7bea commit 714b590
Show file tree
Hide file tree
Showing 46 changed files with 496 additions and 1,417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ public static void main(String[] args) {
// 初始化服务
service = new AppService.Builder().config(config).build();
// ... 调用接口
try {
closeOrder();
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** APP支付下单 */
Expand All @@ -66,12 +48,4 @@ public static PrepayResponse prepay() {
return service.prepay(request);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ public static void main(String[] args) {
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** APP支付下单,并返回APP调起支付数据 */
public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {
// 微信开放平台审核通过的移动应用appid
Expand All @@ -66,12 +57,4 @@ public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {
return service.prepayWithRequestPayment(request, requestPaymentAppid);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ public static void main(String[] args) {
// 初始化服务
service = new H5Service.Builder().config(config).build();
// ... 调用接口
try {
closeOrder();
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** H5支付下单 */
Expand All @@ -66,12 +48,4 @@ public static PrepayResponse prepay() {
return service.prepay(request);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,8 @@ public static void main(String[] args) {
// 初始化服务
service = new JsapiService.Builder().config(config).build();
// ... 调用接口
try {
closeOrder();
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** JSAPI支付下单 */
public static PrepayResponse prepay() {
Expand All @@ -66,12 +49,4 @@ public static PrepayResponse prepay() {
return service.prepay(request);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ public static void main(String[] args) {
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** JSAPI支付下单,并返回JSAPI调起支付数据 */
public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {
// 商户申请的公众号对应的appid,由微信支付生成,可在公众号后台查看
Expand All @@ -70,13 +61,4 @@ public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {
return service.prepayWithRequestPayment(request, requestPaymentAppid);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ public static void main(String[] args) {
// 初始化服务
service = new NativePayService.Builder().config(config).build();
// ... 调用接口
try {
closeOrder();
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** Native支付预下单 */
Expand All @@ -66,13 +48,5 @@ public static PrepayResponse prepay() {
return service.prepay(request);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.wechat.pay.java.service.ecommercecombinepayments.order;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
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.service.ecommercecombinepayments.order.model.CloseOrderRequest;
import com.wechat.pay.java.service.ecommercecombinepayments.order.model.QueryOrderByCombineOutTradeNoRequest;
import com.wechat.pay.java.service.ecommercecombinepayments.order.model.Transaction;

/** OrderService使用示例 */
public class OrderServiceExample {

/** 商户号 */
public static String merchantId = "190000****";

/** 商户API私钥路径 */
public static String privateKeyPath = "/Users/yourname/your/path/apiclient_key.pem";

/** 商户证书序列号 */
public static String merchantSerialNumber = "5157F09EFDC096DE15EBE81A47057A72********";

/** 商户APIV3密钥 */
public static String apiV3Key = "...";

public static OrderService service;

public static void main(String[] args) {
// 初始化商户配置
Config config =
new RSAAutoCertificateConfig.Builder()
.merchantId(merchantId)
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
.privateKeyFromPath(privateKeyPath)
.merchantSerialNumber(merchantSerialNumber)
.apiV3Key(apiV3Key)
.build();

// 初始化服务
service = new OrderService.Builder().config(config).build();
// ... 调用接口
try {
closeOrder();
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
}
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByCombineOutTradeNo() {

QueryOrderByCombineOutTradeNoRequest request = new QueryOrderByCombineOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByCombineOutTradeNo(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.wechat.pay.java.core.http.*;
import com.wechat.pay.java.service.ecommercecombinepayments.app.model.*;

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;

Expand Down Expand Up @@ -84,40 +83,6 @@ public AppService build() {
}
}

/**
* 关闭订单
*
* @param request 请求参数
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public void closeOrder(CloseOrderRequest request) {
String requestPath =
"https://api.mch.weixin.qq.com/v3/combine-transactions/out-trade-no/{combine_out_trade_no}/close";

CloseOrderRequest realRequest = request;
// 添加 path param
requestPath =
requestPath.replace("{" + "combine_out_trade_no" + "}", urlEncode(realRequest.getCombineOutTradeNo()));

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();
httpClient.execute(httpRequest, null);
}

/**
* APP支付下单
*
Expand Down Expand Up @@ -149,41 +114,6 @@ public PrepayResponse prepay(PrepayRequest request) {
return httpResponse.getServiceResponse();
}

/**
* 商户订单号查询订单
*
* @param request 请求参数
* @return Transaction
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public Transaction queryOrderByCombineOutTradeNo(QueryOrderByCombineOutTradeNoRequest request) {
String requestPath =
"https://api.mch.weixin.qq.com/v3/combine-transactions/out-trade-no/{combine_out_trade_no}";

QueryOrderByCombineOutTradeNoRequest realRequest = request;
// 添加 path param
requestPath =
requestPath.replace("{" + "combine_out_trade_no" + "}", urlEncode(realRequest.getCombineOutTradeNo()));

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<Transaction> httpResponse = httpClient.execute(httpRequest, Transaction.class);
return httpResponse.getServiceResponse();
}

private RequestBody createRequestBody(Object request) {
return new JsonRequestBody.Builder().body(toJson(request)).build();
}
Expand Down
Loading

0 comments on commit 714b590

Please sign in to comment.