Skip to content

Commit e222d03

Browse files
committed
feat"平台收付通(商户进件)
1 parent c3ddd87 commit e222d03

File tree

4 files changed

+1070
-0
lines changed

4 files changed

+1070
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.wechat.pay.java.service.partnerpayments.applyments;
2+
3+
import com.wechat.pay.java.core.Config;
4+
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
5+
import com.wechat.pay.java.core.RSAPublicKeyConfig;
6+
import com.wechat.pay.java.core.exception.HttpException;
7+
import com.wechat.pay.java.core.exception.MalformedMessageException;
8+
import com.wechat.pay.java.core.exception.ServiceException;
9+
import com.wechat.pay.java.core.exception.ValidationException;
10+
import com.wechat.pay.java.service.partnerpayments.app.AppService;
11+
import com.wechat.pay.java.service.partnerpayments.app.model.*;
12+
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsRequest;
13+
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsResponse;
14+
15+
/** AppService使用示例 */
16+
public class ApplymentsExample {
17+
18+
/** 商户号 */
19+
public static String merchantId = "190000****";
20+
21+
/** 商户API私钥路径 */
22+
public static String privateKeyPath = "/Users/yourname/your/path/apiclient_key.pem";
23+
public static String publicKeyFormPath = "/pub_key.pem";
24+
/** 商户API证书序列号 */
25+
public static String publicKeyId = "PUB_KEY_ID_01XXX";
26+
27+
/** 商户证书序列号 */
28+
public static String merchantSerialNumber = "XXX";
29+
30+
/** 商户APIV3密钥 */
31+
public static String apiV3Key = "";
32+
33+
34+
public static ApplymentsService service;
35+
36+
public static void main(String[] args) {
37+
try {
38+
// 初始化商户配置
39+
System.out.println("开始初始化商户配置...");
40+
Config config =
41+
new RSAPublicKeyConfig.Builder()
42+
.merchantId(merchantId)
43+
.publicKeyFromPath(publicKeyFormPath)
44+
.publicKeyId(publicKeyId)
45+
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
46+
.privateKeyFromPath(privateKeyPath)
47+
.merchantSerialNumber(merchantSerialNumber)
48+
.apiV3Key(apiV3Key)
49+
.build();
50+
51+
// 初始化服务
52+
System.out.println("开始初始化服务...");
53+
service = new ApplymentsService.Builder().config(config).build();
54+
55+
// 调用接口
56+
System.out.println("开始调用接口...");
57+
ApplymentsResponse response = applyments();
58+
System.out.println("接口调用成功,返回结果:" + response);
59+
60+
} catch (HttpException e) {
61+
System.err.println("发送HTTP请求失败:" + e.getMessage());
62+
System.err.println("请求信息:" + e.getHttpRequest());
63+
e.printStackTrace();
64+
} catch (ServiceException e) {
65+
System.err.println("服务返回异常:" + e.getMessage());
66+
System.err.println("返回体:" + e.getResponseBody());
67+
e.printStackTrace();
68+
} catch (MalformedMessageException e) {
69+
System.err.println("解析返回体失败:" + e.getMessage());
70+
e.printStackTrace();
71+
} catch (ValidationException e) {
72+
System.err.println("验证签名失败:" + e.getMessage());
73+
e.printStackTrace();
74+
} catch (Exception e) {
75+
System.err.println("发生未知错误:" + e.getMessage());
76+
e.printStackTrace();
77+
}
78+
}
79+
80+
81+
/** APP支付下单 */
82+
public static ApplymentsResponse applyments() {
83+
ApplymentsRequest request = new ApplymentsRequest();
84+
85+
86+
// 调用接口
87+
return service.applyments(request, publicKeyId);
88+
}
89+
90+
91+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2021 Tencent Inc. All rights reserved.
2+
//
3+
// APP支付
4+
//
5+
// APP支付API
6+
//
7+
// API version: 1.2.3
8+
9+
// Code generated by WechatPay APIv3 Generator based on [OpenAPI
10+
// Generator](https://openapi-generator.tech); DO NOT EDIT.
11+
12+
package com.wechat.pay.java.service.partnerpayments.applyments;
13+
14+
import com.wechat.pay.java.core.Config;
15+
import com.wechat.pay.java.core.exception.HttpException;
16+
import com.wechat.pay.java.core.exception.MalformedMessageException;
17+
import com.wechat.pay.java.core.exception.ServiceException;
18+
import com.wechat.pay.java.core.exception.ValidationException;
19+
import com.wechat.pay.java.core.http.*;
20+
import com.wechat.pay.java.service.partnerpayments.app.model.*;
21+
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsRequest;
22+
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsResponse;
23+
24+
import static com.wechat.pay.java.core.http.UrlEncoder.urlEncode;
25+
import static com.wechat.pay.java.core.util.GsonUtil.toJson;
26+
import static java.util.Objects.requireNonNull;
27+
28+
/** AppService服务 */
29+
public class ApplymentsService {
30+
31+
private final HttpClient httpClient;
32+
private final HostName hostName;
33+
34+
private ApplymentsService(HttpClient httpClient, HostName hostName) {
35+
this.httpClient = requireNonNull(httpClient);
36+
this.hostName = hostName;
37+
}
38+
39+
/** AppService构造器 */
40+
public static class Builder {
41+
42+
private HttpClient httpClient;
43+
private HostName hostName;
44+
45+
/**
46+
* 设置请求配置,以该配置构造默认的httpClient,若未调用httpClient()方法,则必须调用该方法
47+
*
48+
* @param config 请求配置
49+
* @return Builder
50+
*/
51+
public Builder config(Config config) {
52+
this.httpClient = new DefaultHttpClientBuilder().config(config).build();
53+
54+
return this;
55+
}
56+
57+
/**
58+
* 设置微信支付域名,可选,默认为api.mch.weixin.qq.com
59+
*
60+
* @param hostName 微信支付域名
61+
* @return Builder
62+
*/
63+
public Builder hostName(HostName hostName) {
64+
this.hostName = hostName;
65+
return this;
66+
}
67+
68+
/**
69+
* 设置自定义httpClient,若未调用config(),则必须调用该方法
70+
*
71+
* @param httpClient httpClient
72+
* @return Builder
73+
*/
74+
public Builder httpClient(HttpClient httpClient) {
75+
this.httpClient = httpClient;
76+
return this;
77+
}
78+
79+
/**
80+
* 构造服务
81+
*
82+
* @return AppService
83+
*/
84+
public ApplymentsService build() {
85+
return new ApplymentsService(httpClient, hostName);
86+
}
87+
}
88+
89+
/**
90+
* 平台收付通(商户进件)
91+
* 注意部分参数需要加密
92+
*
93+
* @param request 请求参数
94+
* @param wechatpaySerial 【微信支付公钥ID】或【微信支付平台证书序列号】请求参数中的敏感字段,需要使用微信支付公钥加密(推荐),请参考获取微信支付公钥ID说明以及微信支付公钥加密敏感信息指引,也可以使用微信支付平台证书公钥加密,参考获取平台证书序列号、平台证书加密敏感信息指引
95+
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
96+
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
97+
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
98+
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
99+
*/
100+
public ApplymentsResponse applyments(ApplymentsRequest request,String wechatpaySerial) {
101+
String requestPath =
102+
"https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";
103+
104+
ApplymentsRequest realRequest = request;
105+
if (this.hostName != null) {
106+
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
107+
}
108+
HttpHeaders headers = new HttpHeaders();
109+
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
110+
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
111+
headers.addHeader(Constant.WECHAT_PAY_SERIAL, wechatpaySerial);
112+
HttpRequest httpRequest =
113+
new HttpRequest.Builder()
114+
.httpMethod(HttpMethod.POST)
115+
.url(requestPath)
116+
.headers(headers)
117+
.body(createRequestBody(realRequest))
118+
.build();
119+
httpClient.execute(httpRequest, ApplymentsResponse.class);
120+
HttpResponse<ApplymentsResponse> httpResponse =
121+
httpClient.execute(httpRequest, ApplymentsResponse.class);
122+
return httpResponse.getServiceResponse();
123+
}
124+
125+
126+
127+
private RequestBody createRequestBody(Object request) {
128+
return new JsonRequestBody.Builder().body(toJson(request)).build();
129+
}
130+
}

0 commit comments

Comments
 (0)