-
Notifications
You must be signed in to change notification settings - Fork 355
/
Stripe.java
265 lines (230 loc) · 8.54 KB
/
Stripe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.stripe;
import com.google.gson.JsonObject;
import com.stripe.exception.*;
import com.stripe.model.*;
import com.stripe.net.*;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;
public abstract class Stripe {
public static final int DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
public static final int DEFAULT_READ_TIMEOUT = 80 * 1000;
public static final String API_VERSION = ApiVersion.CURRENT;
public static final String PREVIEW_API_VERSION = ApiVersion.PREVIEW_CURRENT;
public static final String CONNECT_API_BASE = "https://connect.stripe.com";
public static final String LIVE_API_BASE = "https://api.stripe.com";
public static final String UPLOAD_API_BASE = "https://files.stripe.com";
public static final String VERSION = "23.11.0-beta.1";
public static volatile String apiKey;
public static volatile String clientId;
public static volatile boolean enableTelemetry = true;
public static volatile String partnerId;
/**
* Stripe API version which is sent by default on requests. This can be updated to include beta
* headers.
*
* <p>Pointing to different API versions than {@code API_VERSION} can lead to deserialziation
* errors and should be avoided.
*/
public static volatile String stripeVersion = API_VERSION;
// Note that URLConnection reserves the value of 0 to mean "infinite
// timeout", so we use -1 here to represent an unset value which should
// fall back to a default.
private static volatile int connectTimeout = -1;
private static volatile int readTimeout = -1;
private static volatile int maxNetworkRetries = 0;
private static volatile String apiBase = LIVE_API_BASE;
private static volatile String connectBase = CONNECT_API_BASE;
private static volatile String uploadBase = UPLOAD_API_BASE;
private static volatile Proxy connectionProxy = null;
private static volatile PasswordAuthentication proxyCredential = null;
private static volatile Map<String, String> appInfo = null;
/**
* (FOR TESTING ONLY) If you'd like your API requests to hit your own (mocked) server, you can set
* this up here by overriding the base api URL.
*/
public static void overrideApiBase(final String overriddenApiBase) {
apiBase = overriddenApiBase;
}
public static String getApiBase() {
return apiBase;
}
/**
* (FOR TESTING ONLY) If you'd like your OAuth requests to hit your own (mocked) server, you can
* set this up here by overriding the base Connect URL.
*/
public static void overrideConnectBase(final String overriddenConnectBase) {
connectBase = overriddenConnectBase;
}
public static String getConnectBase() {
return connectBase;
}
/**
* (FOR TESTING ONLY) If you'd like your upload requests to hit your own (mocked) server, you can
* set this up here by overriding the base api URL.
*/
public static void overrideUploadBase(final String overriddenUploadBase) {
uploadBase = overriddenUploadBase;
}
public static String getUploadBase() {
return uploadBase;
}
/**
* Set proxy to tunnel all Stripe connections.
*
* @param proxy proxy host and port setting
*/
public static void setConnectionProxy(final Proxy proxy) {
connectionProxy = proxy;
}
public static Proxy getConnectionProxy() {
return connectionProxy;
}
/**
* Returns the connection timeout.
*
* @return timeout value in milliseconds
*/
public static int getConnectTimeout() {
if (connectTimeout == -1) {
return DEFAULT_CONNECT_TIMEOUT;
}
return connectTimeout;
}
/**
* Sets the timeout value that will be used for making new connections to the Stripe API (in
* milliseconds).
*
* @param timeout timeout value in milliseconds
*/
public static void setConnectTimeout(final int timeout) {
connectTimeout = timeout;
}
/**
* Returns the read timeout.
*
* @return timeout value in milliseconds
*/
public static int getReadTimeout() {
if (readTimeout == -1) {
return DEFAULT_READ_TIMEOUT;
}
return readTimeout;
}
/**
* Sets the timeout value that will be used when reading data from an established connection to
* the Stripe API (in milliseconds).
*
* <p>Note that this value should be set conservatively because some API requests can take time
* and a short timeout increases the likelihood of causing a problem in the backend.
*
* @param timeout timeout value in milliseconds
*/
public static void setReadTimeout(final int timeout) {
readTimeout = timeout;
}
/**
* Returns the maximum number of times requests will be retried.
*
* @return the maximum number of times requests will be retried
*/
public static int getMaxNetworkRetries() {
return maxNetworkRetries;
}
/**
* Sets the maximum number of times requests will be retried.
*
* @param numRetries the maximum number of times requests will be retried
*/
public static void setMaxNetworkRetries(final int numRetries) {
maxNetworkRetries = numRetries;
}
/**
* Provide credential for proxy authorization if required.
*
* @param auth proxy required userName and password
*/
public static void setProxyCredential(final PasswordAuthentication auth) {
proxyCredential = auth;
}
public static PasswordAuthentication getProxyCredential() {
return proxyCredential;
}
public static void setAppInfo(String name) {
setAppInfo(name, null, null, null);
}
public static void setAppInfo(String name, String version) {
setAppInfo(name, version, null, null);
}
public static void setAppInfo(String name, String version, String url) {
setAppInfo(name, version, url, null);
}
/**
* Sets information about your application. The information is passed along to Stripe.
*
* @param name Name of your application (e.g. "MyAwesomeApp")
* @param version Version of your application (e.g. "1.2.34")
* @param url Website for your application (e.g. "https://myawesomeapp.info")
* @param partnerId Your Stripe Partner ID (e.g. "pp_partner_1234")
*/
public static void setAppInfo(String name, String version, String url, String partnerId) {
if (appInfo == null) {
appInfo = new HashMap<String, String>();
}
appInfo.put("name", name);
appInfo.put("version", version);
appInfo.put("url", url);
appInfo.put("partner_id", partnerId);
}
public static Map<String, String> getAppInfo() {
return appInfo;
}
/**
* Send raw request to Stripe API. This is the lowest level method for interacting with the Stripe
* API. This method is useful for interacting with endpoints that are not covered yet in
* stripe-java.
*
* @param method the HTTP method
* @param relativeUrl the relative URL of the request, e.g. "/v1/charges"
* @param content the body of the request as a string
* @return the JSON response as a string
*/
public static StripeResponse rawRequest(
final ApiResource.RequestMethod method, final String relativeUrl, final String content)
throws StripeException {
RawRequestOptions options = RawRequestOptions.builder().build();
return rawRequest(method, relativeUrl, content, options);
}
/**
* Send raw request to Stripe API. This is the lowest level method for interacting with the Stripe
* API. This method is useful for interacting with endpoints that are not covered yet in
* stripe-java.
*
* @param method the HTTP method
* @param relativeUrl the relative URL of the request, e.g. "/v1/charges"
* @param content the body of the request as a string
* @param options the special modifiers of the request
* @return the JSON response as a string
*/
public static StripeResponse rawRequest(
final ApiResource.RequestMethod method,
final String relativeUrl,
final String content,
final RawRequestOptions options)
throws StripeException {
if (method != ApiResource.RequestMethod.POST && content != null && !content.equals("")) {
throw new IllegalArgumentException(
"content is not allowed for non-POST requests. Please pass null and add request parameters to the query string of the URL.");
}
return ApiResource.rawRequest(method, relativeUrl, content, options);
}
/** Deserializes StripeResponse returned by rawRequest into a similar class. */
public static StripeObject deserialize(String rawJson) throws StripeException {
if (rawJson == null) {
throw new IllegalArgumentException("rawJson cannot be null");
}
return StripeObject.deserializeStripeObject(
ApiResource.GSON.fromJson(rawJson, JsonObject.class));
}
}