7
7
import com .alipay .global .api .request .AlipayRequest ;
8
8
import com .alipay .global .api .response .AlipayResponse ;
9
9
import com .alipay .global .api .tools .Constants ;
10
- import com .alipay .global .api .tools .SignatureTool ;
11
10
import com .alipay .global .api .tools .DateTool ;
11
+ import com .alipay .global .api .tools .SignatureTool ;
12
12
import org .apache .commons .lang3 .StringUtils ;
13
13
14
14
import java .util .HashMap ;
15
15
import java .util .Map ;
16
16
17
- public abstract class BaseAlipayClient implements AlipayClient {
17
+ public abstract class BaseAlipayClient implements AlipayClient {
18
18
19
19
private static final Integer DEFAULT_KEY_VERSION = 1 ;
20
20
/**
21
- * eg:https://open-na.alipay.com
21
+ * eg: https://open-na.alipay.com
22
22
*/
23
- private String gatewayUrl ;
23
+ private String gatewayUrl ;
24
24
/**
25
25
* merchants private key
26
26
*/
@@ -29,26 +29,53 @@ public abstract class BaseAlipayClient implements AlipayClient{
29
29
* alipay public key
30
30
*/
31
31
private String alipayPublicKey ;
32
+ /**
33
+ * client id
34
+ */
35
+ private String clientId ;
36
+ /**
37
+ * is sandbox mode
38
+ */
39
+ private boolean isSandboxMode = false ;
32
40
33
- public BaseAlipayClient (){
41
+ public BaseAlipayClient () {
34
42
}
35
43
36
- public BaseAlipayClient (String gatewayUrl , String merchantPrivateKey , String alipayPublicKey ) {
44
+ public BaseAlipayClient (String gatewayUrl , String merchantPrivateKey , String alipayPublicKey ) {
37
45
this .gatewayUrl = gatewayUrl ;
38
46
this .merchantPrivateKey = merchantPrivateKey ;
39
47
this .alipayPublicKey = alipayPublicKey ;
40
48
}
41
49
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
+
42
62
public <T extends AlipayResponse > T execute (AlipayRequest <T > alipayRequest ) throws AlipayApiException {
43
63
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 ());
45
66
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 );
52
79
53
80
/**
54
81
* 对内容加签(Sign the content)
@@ -58,9 +85,9 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
58
85
/**
59
86
* 生成必要header(Generate required headers)
60
87
*/
61
- Map <String , String > header = buildBaseHeader (reqTime , clientId , keyVersion , signValue );
88
+ Map <String , String > header = buildBaseHeader (reqTime , clientId , keyVersion , signValue );
62
89
Map <String , String > customHeader = buildCustomHeader ();
63
- if (customHeader != null && customHeader .size () > 0 ) {
90
+ if (customHeader != null && ! customHeader .isEmpty ()) {
64
91
header .putAll (customHeader );
65
92
}
66
93
@@ -70,33 +97,33 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
70
97
*/
71
98
HttpRpcResult rsp = sendRequest (requestUrl , httpMethod , header , reqBody );
72
99
73
- if (rsp == null ){
100
+ if (rsp == null ) {
74
101
throw new AlipayApiException ("HttpRpcResult is null." );
75
102
}
76
103
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 ) {
80
107
throw new AlipayApiException ("Response data error, rspBody:" + rspBody );
81
108
}
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 ) {
86
113
throw new AlipayApiException ("Response data error, result field is null, rspBody:" + rspBody );
87
114
}
88
115
89
116
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 ()) {
92
119
return alipayResponse ;
93
120
}
94
121
95
122
/**
96
123
* 对返回结果验签(Verify the result signature)
97
124
*/
98
125
boolean isVerifySuccess = checkRspSign (httpMethod , path , clientId , rspTime , rspBody , rspSignValue );
99
- if (!isVerifySuccess ){
126
+ if (!isVerifySuccess ) {
100
127
throw new AlipayApiException ("Response signature verify fail." );
101
128
}
102
129
@@ -108,89 +135,98 @@ private String genSignValue(String httpMethod, String path, String clientId, Str
108
135
try {
109
136
signatureValue = SignatureTool .sign (httpMethod , path , clientId , requestTime , reqBody , merchantPrivateKey );
110
137
} catch (Exception e ) {
111
- throw new AlipayApiException (e );
138
+ throw new AlipayApiException ("generate signature error" , e );
112
139
}
113
140
return signatureValue ;
114
141
}
115
142
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 );
122
148
}
123
-
124
149
}
125
150
126
- private void checkRequestParam (AlipayRequest alipayRequest ) throws AlipayApiException {
127
- if (alipayRequest == null ){
151
+ private void checkRequestParams (AlipayRequest alipayRequest ) throws AlipayApiException {
152
+ if (alipayRequest == null ) {
128
153
throw new AlipayApiException ("alipayRequest can't null" );
129
154
}
130
155
131
156
String clientId = alipayRequest .getClientId ();
132
- String httpMehod = alipayRequest .getHttpMethod ();
157
+ String httpMethod = alipayRequest .getHttpMethod ();
133
158
String path = alipayRequest .getPath ();
134
159
135
- if (StringUtils .isBlank (gatewayUrl )){
160
+ if (StringUtils .isBlank (gatewayUrl )) {
136
161
throw new AlipayApiException ("gatewayUrl can't null" );
137
162
}
138
163
139
- if (StringUtils .isBlank (clientId )){
164
+ if (StringUtils .isBlank (clientId )) {
140
165
throw new AlipayApiException ("clientId can't null" );
141
166
}
142
167
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" );
145
170
}
146
171
147
- if (StringUtils .isBlank (path )){
172
+ if (StringUtils .isBlank (path )) {
148
173
throw new AlipayApiException ("path can't null" );
149
174
}
150
175
151
- if (!path .startsWith ("/" )){
176
+ if (!path .startsWith ("/" )) {
152
177
throw new AlipayApiException ("path must start with /" );
153
178
}
154
179
155
180
}
156
181
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://" )) {
159
184
gatewayUrl = "https://" + gatewayUrl ;
160
185
}
161
- if (gatewayUrl .endsWith ("/" )){
186
+ if (gatewayUrl .endsWith ("/" )) {
162
187
int len = gatewayUrl .length ();
163
188
gatewayUrl = gatewayUrl .substring (0 , len - 1 );
164
189
}
165
- String requestUrl = gatewayUrl + path ;
166
- return requestUrl ;
190
+ return gatewayUrl + path ;
167
191
168
192
}
169
193
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
+
170
206
/**
171
207
* Generate required headers
208
+ *
172
209
* @param requestTime
173
210
* @param clientId
174
211
* @param keyVersion
175
212
* @param signatureValue
176
213
* @return
177
214
*/
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 ) {
179
216
Map <String , String > header = new HashMap <String , String >();
180
217
header .put (Constants .CONTENT_TYPE_HEADER , "application/json; charset=UTF-8" );
181
218
header .put (Constants .REQ_TIME_HEADER , requestTime );
182
219
header .put (Constants .CLIENT_ID_HEADER , clientId );
183
- if (keyVersion == null ){
220
+ if (keyVersion == null ) {
184
221
keyVersion = DEFAULT_KEY_VERSION ;
185
222
}
186
223
String signatureHeader = "algorithm=RSA256,keyVersion=" + keyVersion + ",signature=" + signatureValue ;
187
224
header .put (Constants .REQ_SIGN_HEADER , signatureHeader );
188
225
return header ;
189
226
}
190
227
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 ();
194
229
230
+ public abstract HttpRpcResult sendRequest (String requestUrl , String httpMethod , Map <String , String > header , String reqBody ) throws AlipayApiException ;
195
231
196
232
}
0 commit comments