Skip to content

Commit 096fc49

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # pom.xml # src/main/java/com/alipay/global/api/model/risk/PaymentMethodMetaData.java
2 parents 30dbddc + c124226 commit 096fc49

File tree

148 files changed

+1631
-4502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+1631
-4502
lines changed

CHANGE.log

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ add wallet paymentMethod
9393
add easypay params
9494

9595
26、Version:2.0.21
96+
add subscription params and constans
97+
98+
27、Version:2.0.21
9699
add subscription params and constans

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ https://mvnrepository.com/artifact/com.alipay.global.sdk/global-open-sdk-java
1212
<dependency>
1313
<groupId>com.alipay.global.sdk</groupId>
1414
<artifactId>global-open-sdk-java</artifactId>
15-
<version>2.0.21</version>
15+
<version>2.0.26</version>
1616
</dependency>
1717
```
1818

@@ -77,7 +77,7 @@ order.setOrderAmount(orderAmount);
7777
Env env = new Env();
7878
env.setTerminalType(TerminalType.APP);
7979
env.setOsType(OsType.IOS);
80-
order.setEnv(env);
80+
alipayPayRequest.setEnv(env);
8181
8282
alipayPayRequest.setOrder(order);
8383

pom.xml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.alipay.global.sdk</groupId>
55
<artifactId>global-open-sdk-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.0.21</version>
7+
<version>2.0.26</version>
88
<name>global-open-sdk-java</name>
99
<url>https://github.com/alipay/global-open-sdk-java</url>
1010
<description>
@@ -69,10 +69,12 @@
6969
<artifactId>fastjson</artifactId>
7070
<version>1.2.83</version>
7171
</dependency>
72+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
7273
<dependency>
73-
<groupId>javax.validation</groupId>
74-
<artifactId>validation-api</artifactId>
75-
<version>2.0.1.Final</version>
74+
<groupId>org.projectlombok</groupId>
75+
<artifactId>lombok</artifactId>
76+
<version>1.18.30</version>
77+
<scope>provided</scope>
7678
</dependency>
7779
</dependencies>
7880

@@ -83,8 +85,8 @@
8385
<artifactId>maven-compiler-plugin</artifactId>
8486
<version>2.3.2</version>
8587
<configuration>
86-
<source>8</source>
87-
<target>8</target>
88+
<source>1.6</source>
89+
<target>1.6</target>
8890
<encoding>UTF-8</encoding>
8991
</configuration>
9092
</plugin>

src/main/java/com/alipay/global/api/BaseAlipayClient.java

Lines changed: 89 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
import com.alipay.global.api.request.AlipayRequest;
88
import com.alipay.global.api.response.AlipayResponse;
99
import com.alipay.global.api.tools.Constants;
10-
import com.alipay.global.api.tools.SignatureTool;
1110
import com.alipay.global.api.tools.DateTool;
11+
import com.alipay.global.api.tools.SignatureTool;
1212
import org.apache.commons.lang3.StringUtils;
1313

1414
import java.util.HashMap;
1515
import java.util.Map;
1616

17-
public abstract class BaseAlipayClient implements AlipayClient{
17+
public abstract class BaseAlipayClient implements AlipayClient {
1818

1919
private static final Integer DEFAULT_KEY_VERSION = 1;
2020
/**
21-
* eg:https://open-na.alipay.com
21+
* eg: https://open-na.alipay.com
2222
*/
23-
private String gatewayUrl;
23+
private String gatewayUrl;
2424
/**
2525
* merchants private key
2626
*/
@@ -29,26 +29,53 @@ public abstract class BaseAlipayClient implements AlipayClient{
2929
* alipay public key
3030
*/
3131
private String alipayPublicKey;
32+
/**
33+
* client id
34+
*/
35+
private String clientId;
36+
/**
37+
* is sandbox mode
38+
*/
39+
private boolean isSandboxMode = false;
3240

33-
public BaseAlipayClient(){
41+
public BaseAlipayClient() {
3442
}
3543

36-
public BaseAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey ){
44+
public BaseAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey) {
3745
this.gatewayUrl = gatewayUrl;
3846
this.merchantPrivateKey = merchantPrivateKey;
3947
this.alipayPublicKey = alipayPublicKey;
4048
}
4149

50+
public BaseAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey, String clientId) {
51+
this.gatewayUrl = gatewayUrl;
52+
this.merchantPrivateKey = merchantPrivateKey;
53+
this.alipayPublicKey = alipayPublicKey;
54+
this.clientId = clientId;
55+
56+
// if client id starts with SANDBOX_, set to sandbox mode
57+
if (clientId.startsWith("SANDBOX_")) {
58+
this.isSandboxMode = true;
59+
}
60+
}
61+
4262
public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) throws AlipayApiException {
4363

44-
checkRequestParam(alipayRequest);
64+
// compatible with old version which clientId does not exist in BaseAlipayClient
65+
alipayRequest.setClientId(alipayRequest.getClientId() == null ? this.clientId : alipayRequest.getClientId());
4566

46-
String clientId = alipayRequest.getClientId();
47-
String httpMethod = alipayRequest.getHttpMethod();
48-
String path = alipayRequest.getPath();
49-
Integer keyVersion = alipayRequest.getKeyVersion();
50-
String reqTime = DateTool.getCurrentTimeMillis();
51-
String reqBody = JSON.toJSONString(alipayRequest);
67+
// replace with sandbox url if needed
68+
adjustSandboxUrl(alipayRequest);
69+
70+
// check request params
71+
checkRequestParams(alipayRequest);
72+
73+
String clientId = alipayRequest.getClientId();
74+
String httpMethod = alipayRequest.getHttpMethod();
75+
String path = alipayRequest.getPath();
76+
Integer keyVersion = alipayRequest.getKeyVersion();
77+
String reqTime = DateTool.getCurrentTimeMillis();
78+
String reqBody = JSON.toJSONString(alipayRequest);
5279

5380
/**
5481
* 对内容加签(Sign the content)
@@ -58,9 +85,9 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
5885
/**
5986
* 生成必要header(Generate required headers)
6087
*/
61-
Map<String, String> header = buildBaseHeader(reqTime, clientId, keyVersion, signValue);
88+
Map<String, String> header = buildBaseHeader(reqTime, clientId, keyVersion, signValue);
6289
Map<String, String> customHeader = buildCustomHeader();
63-
if(customHeader != null && customHeader.size() > 0){
90+
if (customHeader != null && !customHeader.isEmpty()) {
6491
header.putAll(customHeader);
6592
}
6693

@@ -70,33 +97,33 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
7097
*/
7198
HttpRpcResult rsp = sendRequest(requestUrl, httpMethod, header, reqBody);
7299

73-
if(rsp == null){
100+
if (rsp == null) {
74101
throw new AlipayApiException("HttpRpcResult is null.");
75102
}
76103

77-
int httpRespCode = rsp.getRspCode();
78-
String rspBody = rsp.getRspBody();
79-
if(httpRespCode != Constants.HTTP_SUCCESS_CODE){
104+
int httpRespCode = rsp.getRspCode();
105+
String rspBody = rsp.getRspBody();
106+
if (httpRespCode != Constants.HTTP_SUCCESS_CODE) {
80107
throw new AlipayApiException("Response data error, rspBody:" + rspBody);
81108
}
82-
Class<T> responseClass = alipayRequest.getResponseClass();
83-
T alipayResponse = JSON.parseObject(rspBody, responseClass);
84-
Result result = alipayResponse.getResult();
85-
if(result == null){
109+
Class<T> responseClass = alipayRequest.getResponseClass();
110+
T alipayResponse = JSON.parseObject(rspBody, responseClass);
111+
Result result = alipayResponse.getResult();
112+
if (result == null) {
86113
throw new AlipayApiException("Response data error, result field is null, rspBody:" + rspBody);
87114
}
88115

89116
String rspSignValue = rsp.getRspSign();
90-
String rspTime = rsp.getResponseTime();
91-
if(null == rspSignValue || rspSignValue.isEmpty() || null == rspTime || rspTime.isEmpty()){
117+
String rspTime = rsp.getResponseTime();
118+
if (null == rspSignValue || rspSignValue.isEmpty() || null == rspTime || rspTime.isEmpty()) {
92119
return alipayResponse;
93120
}
94121

95122
/**
96123
* 对返回结果验签(Verify the result signature)
97124
*/
98125
boolean isVerifySuccess = checkRspSign(httpMethod, path, clientId, rspTime, rspBody, rspSignValue);
99-
if(!isVerifySuccess){
126+
if (!isVerifySuccess) {
100127
throw new AlipayApiException("Response signature verify fail.");
101128
}
102129

@@ -108,89 +135,98 @@ private String genSignValue(String httpMethod, String path, String clientId, Str
108135
try {
109136
signatureValue = SignatureTool.sign(httpMethod, path, clientId, requestTime, reqBody, merchantPrivateKey);
110137
} catch (Exception e) {
111-
throw new AlipayApiException(e);
138+
throw new AlipayApiException("generate signature error", e);
112139
}
113140
return signatureValue;
114141
}
115142

116-
private boolean checkRspSign(String httpMethod, String path, String clientId, String responseTime, String rspBody, String rspSignValue) throws AlipayApiException{
117-
try{
118-
boolean isVerify = SignatureTool.verify(httpMethod, path, clientId, responseTime, rspBody, rspSignValue, alipayPublicKey);
119-
return isVerify;
120-
} catch(Exception e){
121-
throw new AlipayApiException(e);
143+
private boolean checkRspSign(String httpMethod, String path, String clientId, String responseTime, String rspBody, String rspSignValue) throws AlipayApiException {
144+
try {
145+
return SignatureTool.verify(httpMethod, path, clientId, responseTime, rspBody, rspSignValue, alipayPublicKey);
146+
} catch (Exception e) {
147+
throw new AlipayApiException("verify signature error", e);
122148
}
123-
124149
}
125150

126-
private void checkRequestParam(AlipayRequest alipayRequest) throws AlipayApiException{
127-
if(alipayRequest == null){
151+
private void checkRequestParams(AlipayRequest alipayRequest) throws AlipayApiException {
152+
if (alipayRequest == null) {
128153
throw new AlipayApiException("alipayRequest can't null");
129154
}
130155

131156
String clientId = alipayRequest.getClientId();
132-
String httpMehod = alipayRequest.getHttpMethod();
157+
String httpMethod = alipayRequest.getHttpMethod();
133158
String path = alipayRequest.getPath();
134159

135-
if(StringUtils.isBlank(gatewayUrl)){
160+
if (StringUtils.isBlank(gatewayUrl)) {
136161
throw new AlipayApiException("gatewayUrl can't null");
137162
}
138163

139-
if(StringUtils.isBlank(clientId)){
164+
if (StringUtils.isBlank(clientId)) {
140165
throw new AlipayApiException("clientId can't null");
141166
}
142167

143-
if(StringUtils.isBlank(httpMehod)){
144-
throw new AlipayApiException("httpMehod can't null");
168+
if (StringUtils.isBlank(httpMethod)) {
169+
throw new AlipayApiException("httpMethod can't null");
145170
}
146171

147-
if(StringUtils.isBlank(path)){
172+
if (StringUtils.isBlank(path)) {
148173
throw new AlipayApiException("path can't null");
149174
}
150175

151-
if(!path.startsWith("/")){
176+
if (!path.startsWith("/")) {
152177
throw new AlipayApiException("path must start with /");
153178
}
154179

155180
}
156181

157-
private String genRequestUrl(String path){
158-
if(!gatewayUrl.startsWith("http://") && !gatewayUrl.startsWith("https://")){
182+
private String genRequestUrl(String path) {
183+
if (!gatewayUrl.startsWith("http://") && !gatewayUrl.startsWith("https://")) {
159184
gatewayUrl = "https://" + gatewayUrl;
160185
}
161-
if(gatewayUrl.endsWith("/")){
186+
if (gatewayUrl.endsWith("/")) {
162187
int len = gatewayUrl.length();
163188
gatewayUrl = gatewayUrl.substring(0, len - 1);
164189
}
165-
String requestUrl = gatewayUrl + path;
166-
return requestUrl;
190+
return gatewayUrl + path;
167191

168192
}
169193

194+
/**
195+
* If is sandbox mode, modify the path
196+
*
197+
* @param alipayRequest
198+
*/
199+
private void adjustSandboxUrl(AlipayRequest alipayRequest) {
200+
if (isSandboxMode) {
201+
String originPath = alipayRequest.getPath();
202+
alipayRequest.setPath(originPath.replaceFirst("/ams/api", "/ams/sandbox/api"));
203+
}
204+
}
205+
170206
/**
171207
* Generate required headers
208+
*
172209
* @param requestTime
173210
* @param clientId
174211
* @param keyVersion
175212
* @param signatureValue
176213
* @return
177214
*/
178-
private Map<String,String> buildBaseHeader(String requestTime, String clientId, Integer keyVersion, String signatureValue){
215+
private Map<String, String> buildBaseHeader(String requestTime, String clientId, Integer keyVersion, String signatureValue) {
179216
Map<String, String> header = new HashMap<String, String>();
180217
header.put(Constants.CONTENT_TYPE_HEADER, "application/json; charset=UTF-8");
181218
header.put(Constants.REQ_TIME_HEADER, requestTime);
182219
header.put(Constants.CLIENT_ID_HEADER, clientId);
183-
if(keyVersion == null){
220+
if (keyVersion == null) {
184221
keyVersion = DEFAULT_KEY_VERSION;
185222
}
186223
String signatureHeader = "algorithm=RSA256,keyVersion=" + keyVersion + ",signature=" + signatureValue;
187224
header.put(Constants.REQ_SIGN_HEADER, signatureHeader);
188225
return header;
189226
}
190227

191-
public abstract Map<String,String> buildCustomHeader();
192-
193-
public abstract HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody)throws AlipayApiException;
228+
public abstract Map<String, String> buildCustomHeader();
194229

230+
public abstract HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody) throws AlipayApiException;
195231

196232
}

src/main/java/com/alipay/global/api/DefaultAlipayClient.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66

77
import java.util.Map;
88

9-
public class DefaultAlipayClient extends BaseAlipayClient{
9+
public class DefaultAlipayClient extends BaseAlipayClient {
1010

11-
public DefaultAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey ){
11+
public DefaultAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey) {
1212
super(gatewayUrl, merchantPrivateKey, alipayPublicKey);
1313
}
1414

15+
public DefaultAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey, String clientId) {
16+
super(gatewayUrl, merchantPrivateKey, alipayPublicKey, clientId);
17+
}
18+
1519
@Override
1620
public Map<String, String> buildCustomHeader() {
1721
return null;
1822
}
1923

20-
public HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody)throws AlipayApiException {
24+
public HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody) throws AlipayApiException {
2125
HttpRpcResult httpRpcResult;
2226
try {
2327
httpRpcResult = DefaultHttpRPC.doPost(requestUrl, header, reqBody);
24-
} catch (Exception e){
28+
} catch (Exception e) {
2529
throw new AlipayApiException(e);
2630
}
2731
return httpRpcResult;

0 commit comments

Comments
 (0)