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
*/
@@ -30,10 +30,10 @@ public abstract class BaseAlipayClient implements AlipayClient{
30
30
*/
31
31
private String alipayPublicKey ;
32
32
33
- public BaseAlipayClient (){
33
+ public BaseAlipayClient () {
34
34
}
35
35
36
- public BaseAlipayClient (String gatewayUrl , String merchantPrivateKey , String alipayPublicKey ) {
36
+ public BaseAlipayClient (String gatewayUrl , String merchantPrivateKey , String alipayPublicKey ) {
37
37
this .gatewayUrl = gatewayUrl ;
38
38
this .merchantPrivateKey = merchantPrivateKey ;
39
39
this .alipayPublicKey = alipayPublicKey ;
@@ -43,12 +43,12 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
43
43
44
44
checkRequestParam (alipayRequest );
45
45
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 );
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 );
52
52
53
53
/**
54
54
* 对内容加签(Sign the content)
@@ -58,9 +58,9 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
58
58
/**
59
59
* 生成必要header(Generate required headers)
60
60
*/
61
- Map <String , String > header = buildBaseHeader (reqTime , clientId , keyVersion , signValue );
61
+ Map <String , String > header = buildBaseHeader (reqTime , clientId , keyVersion , signValue );
62
62
Map <String , String > customHeader = buildCustomHeader ();
63
- if (customHeader != null && customHeader .size () > 0 ) {
63
+ if (customHeader != null && ! customHeader .isEmpty ()) {
64
64
header .putAll (customHeader );
65
65
}
66
66
@@ -70,33 +70,33 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
70
70
*/
71
71
HttpRpcResult rsp = sendRequest (requestUrl , httpMethod , header , reqBody );
72
72
73
- if (rsp == null ){
73
+ if (rsp == null ) {
74
74
throw new AlipayApiException ("HttpRpcResult is null." );
75
75
}
76
76
77
- int httpRespCode = rsp .getRspCode ();
78
- String rspBody = rsp .getRspBody ();
79
- if (httpRespCode != Constants .HTTP_SUCCESS_CODE ){
77
+ int httpRespCode = rsp .getRspCode ();
78
+ String rspBody = rsp .getRspBody ();
79
+ if (httpRespCode != Constants .HTTP_SUCCESS_CODE ) {
80
80
throw new AlipayApiException ("Response data error, rspBody:" + rspBody );
81
81
}
82
- Class <T > responseClass = alipayRequest .getResponseClass ();
83
- T alipayResponse = JSON .parseObject (rspBody , responseClass );
84
- Result result = alipayResponse .getResult ();
85
- if (result == null ){
82
+ Class <T > responseClass = alipayRequest .getResponseClass ();
83
+ T alipayResponse = JSON .parseObject (rspBody , responseClass );
84
+ Result result = alipayResponse .getResult ();
85
+ if (result == null ) {
86
86
throw new AlipayApiException ("Response data error, result field is null, rspBody:" + rspBody );
87
87
}
88
88
89
89
String rspSignValue = rsp .getRspSign ();
90
- String rspTime = rsp .getResponseTime ();
91
- if (null == rspSignValue || rspSignValue .isEmpty () || null == rspTime || rspTime .isEmpty ()){
90
+ String rspTime = rsp .getResponseTime ();
91
+ if (null == rspSignValue || rspSignValue .isEmpty () || null == rspTime || rspTime .isEmpty ()) {
92
92
return alipayResponse ;
93
93
}
94
94
95
95
/**
96
96
* 对返回结果验签(Verify the result signature)
97
97
*/
98
98
boolean isVerifySuccess = checkRspSign (httpMethod , path , clientId , rspTime , rspBody , rspSignValue );
99
- if (!isVerifySuccess ){
99
+ if (!isVerifySuccess ) {
100
100
throw new AlipayApiException ("Response signature verify fail." );
101
101
}
102
102
@@ -113,84 +113,83 @@ private String genSignValue(String httpMethod, String path, String clientId, Str
113
113
return signatureValue ;
114
114
}
115
115
116
- private boolean checkRspSign (String httpMethod , String path , String clientId , String responseTime , String rspBody , String rspSignValue ) throws AlipayApiException {
117
- try {
116
+ private boolean checkRspSign (String httpMethod , String path , String clientId , String responseTime , String rspBody , String rspSignValue ) throws AlipayApiException {
117
+ try {
118
118
boolean isVerify = SignatureTool .verify (httpMethod , path , clientId , responseTime , rspBody , rspSignValue , alipayPublicKey );
119
119
return isVerify ;
120
- } catch (Exception e ){
120
+ } catch (Exception e ) {
121
121
throw new AlipayApiException (e );
122
122
}
123
123
124
124
}
125
125
126
- private void checkRequestParam (AlipayRequest alipayRequest ) throws AlipayApiException {
127
- if (alipayRequest == null ){
126
+ private void checkRequestParam (AlipayRequest alipayRequest ) throws AlipayApiException {
127
+ if (alipayRequest == null ) {
128
128
throw new AlipayApiException ("alipayRequest can't null" );
129
129
}
130
130
131
131
String clientId = alipayRequest .getClientId ();
132
- String httpMehod = alipayRequest .getHttpMethod ();
132
+ String httpMethod = alipayRequest .getHttpMethod ();
133
133
String path = alipayRequest .getPath ();
134
134
135
- if (StringUtils .isBlank (gatewayUrl )){
135
+ if (StringUtils .isBlank (gatewayUrl )) {
136
136
throw new AlipayApiException ("gatewayUrl can't null" );
137
137
}
138
138
139
- if (StringUtils .isBlank (clientId )){
139
+ if (StringUtils .isBlank (clientId )) {
140
140
throw new AlipayApiException ("clientId can't null" );
141
141
}
142
142
143
- if (StringUtils .isBlank (httpMehod )) {
143
+ if (StringUtils .isBlank (httpMethod )) {
144
144
throw new AlipayApiException ("httpMehod can't null" );
145
145
}
146
146
147
- if (StringUtils .isBlank (path )){
147
+ if (StringUtils .isBlank (path )) {
148
148
throw new AlipayApiException ("path can't null" );
149
149
}
150
150
151
- if (!path .startsWith ("/" )){
151
+ if (!path .startsWith ("/" )) {
152
152
throw new AlipayApiException ("path must start with /" );
153
153
}
154
154
155
155
}
156
156
157
- private String genRequestUrl (String path ){
158
- if (!gatewayUrl .startsWith ("http://" ) && !gatewayUrl .startsWith ("https://" )){
157
+ private String genRequestUrl (String path ) {
158
+ if (!gatewayUrl .startsWith ("http://" ) && !gatewayUrl .startsWith ("https://" )) {
159
159
gatewayUrl = "https://" + gatewayUrl ;
160
160
}
161
- if (gatewayUrl .endsWith ("/" )){
161
+ if (gatewayUrl .endsWith ("/" )) {
162
162
int len = gatewayUrl .length ();
163
163
gatewayUrl = gatewayUrl .substring (0 , len - 1 );
164
164
}
165
- String requestUrl = gatewayUrl + path ;
166
- return requestUrl ;
165
+ return gatewayUrl + path ;
167
166
168
167
}
169
168
170
169
/**
171
170
* Generate required headers
171
+ *
172
172
* @param requestTime
173
173
* @param clientId
174
174
* @param keyVersion
175
175
* @param signatureValue
176
176
* @return
177
177
*/
178
- private Map <String ,String > buildBaseHeader (String requestTime , String clientId , Integer keyVersion , String signatureValue ){
178
+ private Map <String , String > buildBaseHeader (String requestTime , String clientId , Integer keyVersion , String signatureValue ) {
179
179
Map <String , String > header = new HashMap <String , String >();
180
180
header .put (Constants .CONTENT_TYPE_HEADER , "application/json; charset=UTF-8" );
181
181
header .put (Constants .REQ_TIME_HEADER , requestTime );
182
182
header .put (Constants .CLIENT_ID_HEADER , clientId );
183
- if (keyVersion == null ){
183
+ if (keyVersion == null ) {
184
184
keyVersion = DEFAULT_KEY_VERSION ;
185
185
}
186
186
String signatureHeader = "algorithm=RSA256,keyVersion=" + keyVersion + ",signature=" + signatureValue ;
187
187
header .put (Constants .REQ_SIGN_HEADER , signatureHeader );
188
188
return header ;
189
189
}
190
190
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 ;
191
+ public abstract Map <String , String > buildCustomHeader ();
194
192
193
+ public abstract HttpRpcResult sendRequest (String requestUrl , String httpMethod , Map <String , String > header , String reqBody ) throws AlipayApiException ;
195
194
196
195
}
0 commit comments