Skip to content

Commit ec82d87

Browse files
stripe-java v17 (#869)
* Refactor form encoding * Refactor request telemetry * Move HTTP request methods into new `HttpClient` class * Add `StripeRequest` object * Add `HttpClient` abstract class * Stop disabling the DNS cache * Fix deprecation warnings (#895) * Add HttpContent class (#896) * Add Stopwatch class (#897) * Move all request properties in `StripeRequest` (#898) * Remove ApiResource.RequestType (#899) * Add support for automatic request retries (#900) * Minor fixes (#902) * `StringUtils` class & better API key validation (#928) * Remove support for custom `URLStreamHandler` (#927) * Refactor HTTP headers handling (#931) * Add `CaseInsensitiveMap` class * Add `HttpHeaders` class * Use `HttpHeaders` in `StripeRequest` * Use `HttpHeaders` in `StripeResponse` * Address review comments * Modernize `StripeResponse` (#932) * Add `maxNetworkRetries` as a global and per-request setting (#934) * Add `StreamUtils` class (#935) * Remove support for `count` and `total_count` in list objects (#936) * Codegen for openapi e07de1a (#938) * Update README (#939) Co-authored-by: remi-stripe <[email protected]>
1 parent ea509d4 commit ec82d87

Some content is hidden

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

63 files changed

+3430
-1771
lines changed

Diff for: README.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class StripeExample {
6868
Map<String, Object> chargeMap = new HashMap<String, Object>();
6969
chargeMap.put("amount", 100);
7070
chargeMap.put("currency", "usd");
71-
chargeMap.put("source", "tok_1234"); // obtained via Stripe.js
71+
chargeMap.put("source", "tok_visa"); // obtained via Stripe.js
7272

7373
try {
7474
Charge charge = Charge.create(chargeMap);
@@ -84,21 +84,45 @@ See the project's [functional tests](https://github.com/stripe/stripe-java/blob/
8484

8585
### Per-request Configuration
8686

87-
For apps that need to use multiple keys during the lifetime of a process, like
88-
one that uses [Stripe Connect][connect], it's also possible to set a
89-
per-request key and/or account:
87+
All of the request methods accept an optional `RequestOptions` object. This is
88+
used if you want to set an [idempotency key][idempotency-keys], if you are
89+
using [Stripe Connect][connect-auth], or if you want to pass the secret API
90+
key on each method.
9091

9192
```java
9293
RequestOptions requestOptions = new RequestOptionsBuilder()
9394
.setApiKey("sk_test_...")
95+
.setIdempotencyKey("a1b2c3...")
9496
.setStripeAccount("acct_...")
9597
.build();
9698

9799
Charge.list(null, requestOptions);
98100

99-
Charge.retrieve("ch_18atAXCdGbJFKhCuBAa4532Z", requestOptions);
101+
Charge.retrieve("ch_123", requestOptions);
100102
```
101103

104+
### Configuring automatic retries
105+
106+
The library can be configured to automatically retry requests that fail due to
107+
an intermittent network problem or other knowingly non-deterministic errors.
108+
This can be enabled globally:
109+
110+
```java
111+
Stripe.setMaxNetworkRetries(2);
112+
```
113+
114+
Or on a finer grain level using `RequestOptions`:
115+
116+
```java
117+
RequestOptions options = RequestOptions.builder()
118+
.setMaxNetworkRetries(2)
119+
.build();
120+
Charge.create(params, options);
121+
```
122+
123+
[Idempotency keys][idempotency-keys] are added to requests to guarantee that
124+
retries are safe.
125+
102126
### Configuring Timeouts
103127

104128
Connect and read timeouts can be configured globally:
@@ -181,8 +205,9 @@ with:
181205

182206
The library uses [Project Lombok][lombok]. While it is not a requirement, you might want to install a [plugin][lombok-plugins] for your favorite IDE to facilitate development.
183207

184-
[connect]: https://stripe.com/connect
208+
[connect-auth]: https://stripe.com/docs/connect/authentication#stripe-account-header
185209
[google-java-format]: https://github.com/google/google-java-format
210+
[idempotency-keys]: https://stripe.com/docs/api/idempotent_requests?lang=java
186211
[lombok]: https://projectlombok.org
187212
[lombok-plugins]: https://projectlombok.org/setup/overview
188213
[spotless]: https://github.com/diffplug/spotless

Diff for: src/main/java/com/stripe/Stripe.java

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public abstract class Stripe {
2626
private static volatile int connectTimeout = -1;
2727
private static volatile int readTimeout = -1;
2828

29+
private static volatile int maxNetworkRetries = 0;
30+
2931
private static volatile String apiBase = LIVE_API_BASE;
3032
private static volatile String connectBase = CONNECT_API_BASE;
3133
private static volatile String uploadBase = UPLOAD_API_BASE;
@@ -130,6 +132,24 @@ public static void setReadTimeout(final int timeout) {
130132
readTimeout = timeout;
131133
}
132134

135+
/**
136+
* Returns the maximum number of times requests will be retried.
137+
*
138+
* @return the maximum number of times requests will be retried
139+
*/
140+
public static int getMaxNetworkRetries() {
141+
return maxNetworkRetries;
142+
}
143+
144+
/**
145+
* Sets the maximum number of times requests will be retried.
146+
*
147+
* @param numRetries the maximum number of times requests will be retried
148+
*/
149+
public static void setMaxNetworkRetries(final int numRetries) {
150+
maxNetworkRetries = numRetries;
151+
}
152+
133153
/**
134154
* Provide credential for proxy authorization if required.
135155
*

Diff for: src/main/java/com/stripe/model/Event.java

-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ public class Event extends ApiResource implements HasId {
6969
@SerializedName("type")
7070
String type;
7171

72-
/** @deprecated Use {@link #account} instead (https://stripe.com/docs/upgrades#2017-05-25). */
73-
@Deprecated
74-
@SerializedName("user_id")
75-
String userId;
76-
7772
/**
7873
* List events, going back up to 30 days. Each event data is rendered according to Stripe API
7974
* version at its creation time, specified in <a href="/docs/api/events/object">event object</a>

Diff for: src/main/java/com/stripe/model/File.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static File create(FileCreateParams params, RequestOptions options)
9999
*/
100100
public static File create(Map<String, Object> params, RequestOptions options)
101101
throws StripeException {
102-
return multipartRequest(
102+
return request(
103103
RequestMethod.POST,
104104
classUrl(File.class, Stripe.getUploadBase()),
105105
params,

Diff for: src/main/java/com/stripe/model/StripeCollection.java

-18
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,6 @@ public abstract class StripeCollection<T extends HasId> extends StripeObject
4848
@Getter(onMethod_ = {@Override})
4949
String url;
5050

51-
/**
52-
* The {@code count} attribute.
53-
*
54-
* @deprecated Use pagination parameters instead.
55-
* @see <a href="https://stripe.com/docs/api/java#pagination">Pagination</a>
56-
*/
57-
@Deprecated Long count;
58-
59-
/**
60-
* The {@code total_count} attribute.
61-
*
62-
* @deprecated Use pagination parameters instead.
63-
* @see <a href="https://stripe.com/docs/api/java#pagination">Pagination</a>
64-
*/
65-
@Deprecated
66-
@Getter(onMethod_ = {@Override})
67-
Long totalCount;
68-
6951
@Getter(onMethod_ = {@Override})
7052
@Setter(onMethod = @__({@Override}))
7153
private RequestOptions requestOptions;

Diff for: src/main/java/com/stripe/model/StripeCollectionInterface.java

-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ public interface StripeCollectionInterface<T> {
1111

1212
String getUrl();
1313

14-
/**
15-
* The {@code total_count} attribute.
16-
*
17-
* @deprecated Use pagination parameters instead.
18-
* @see <a href="https://stripe.com/docs/api/java#pagination">Pagination</a>
19-
*/
20-
@Deprecated
21-
Long getTotalCount();
22-
2314
/**
2415
* Get request options that were used to fetch the collection. This is useful for purposes of
2516
* pagination.

Diff for: src/main/java/com/stripe/model/issuing/Card.java

-7
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,6 @@ public static class Shipping extends StripeObject {
424424
@SerializedName("name")
425425
String name;
426426

427-
/**
428-
* Deprecated field. It always return null and will be removed in the next client library major
429-
* version
430-
*/
431-
@SerializedName("phone")
432-
String phone;
433-
434427
/**
435428
* The delivery status of the card.
436429
*

Diff for: src/main/java/com/stripe/model/terminal/Location.java

-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.stripe.net.ApiResource;
1010
import com.stripe.net.RequestOptions;
1111
import com.stripe.param.terminal.LocationCreateParams;
12-
import com.stripe.param.terminal.LocationDeleteParams;
1312
import com.stripe.param.terminal.LocationListParams;
1413
import com.stripe.param.terminal.LocationRetrieveParams;
1514
import com.stripe.param.terminal.LocationUpdateParams;
@@ -218,21 +217,4 @@ public Location delete(Map<String, Object> params, RequestOptions options)
218217
return ApiResource.request(
219218
ApiResource.RequestMethod.DELETE, url, params, Location.class, options);
220219
}
221-
222-
/** Deletes a <code>Location</code> object. */
223-
public Location delete(LocationDeleteParams params) throws StripeException {
224-
return delete(params, (RequestOptions) null);
225-
}
226-
227-
/** Deletes a <code>Location</code> object. */
228-
public Location delete(LocationDeleteParams params, RequestOptions options)
229-
throws StripeException {
230-
String url =
231-
String.format(
232-
"%s%s",
233-
Stripe.getApiBase(),
234-
String.format("/v1/terminal/locations/%s", ApiResource.urlEncodeId(this.getId())));
235-
return ApiResource.request(
236-
ApiResource.RequestMethod.DELETE, url, params, Location.class, options);
237-
}
238220
}

Diff for: src/main/java/com/stripe/model/terminal/Reader.java

-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.stripe.net.ApiResource;
99
import com.stripe.net.RequestOptions;
1010
import com.stripe.param.terminal.ReaderCreateParams;
11-
import com.stripe.param.terminal.ReaderDeleteParams;
1211
import com.stripe.param.terminal.ReaderListParams;
1312
import com.stripe.param.terminal.ReaderRetrieveParams;
1413
import com.stripe.param.terminal.ReaderUpdateParams;
@@ -230,20 +229,4 @@ public Reader delete(Map<String, Object> params, RequestOptions options) throws
230229
return ApiResource.request(
231230
ApiResource.RequestMethod.DELETE, url, params, Reader.class, options);
232231
}
233-
234-
/** Deletes a <code>Reader</code> object. */
235-
public Reader delete(ReaderDeleteParams params) throws StripeException {
236-
return delete(params, (RequestOptions) null);
237-
}
238-
239-
/** Deletes a <code>Reader</code> object. */
240-
public Reader delete(ReaderDeleteParams params, RequestOptions options) throws StripeException {
241-
String url =
242-
String.format(
243-
"%s%s",
244-
Stripe.getApiBase(),
245-
String.format("/v1/terminal/readers/%s", ApiResource.urlEncodeId(this.getId())));
246-
return ApiResource.request(
247-
ApiResource.RequestMethod.DELETE, url, params, Reader.class, options);
248-
}
249232
}

Diff for: src/main/java/com/stripe/net/ApiResource.java

+6-26
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
import com.stripe.model.StripeObject;
2121
import com.stripe.model.StripeRawJsonObject;
2222
import com.stripe.model.StripeRawJsonObjectDeserializer;
23+
import com.stripe.util.StringUtils;
2324
import java.io.UnsupportedEncodingException;
2425
import java.net.URLEncoder;
26+
import java.nio.charset.Charset;
2527
import java.nio.charset.StandardCharsets;
2628
import java.util.Map;
2729
import java.util.Objects;
2830

2931
public abstract class ApiResource extends StripeObject {
30-
public static final String CHARSET = StandardCharsets.UTF_8.name();
32+
public static final Charset CHARSET = StandardCharsets.UTF_8;
3133

3234
private static StripeResponseGetter stripeResponseGetter = new LiveStripeResponseGetter();
3335

@@ -55,12 +57,7 @@ private static Gson createGson() {
5557

5658
private static String className(Class<?> clazz) {
5759
// Convert CamelCase to snake_case
58-
String className =
59-
clazz
60-
.getSimpleName()
61-
.replaceAll("(.)([A-Z][a-z]+)", "$1_$2")
62-
.replaceAll("([a-z0-9])([A-Z])", "$1_$2")
63-
.toLowerCase();
60+
String className = StringUtils.toSnakeCase(clazz.getSimpleName());
6461

6562
// Handle namespaced resources by checking if the class is in a sub-package, and if so prepend
6663
// it to the class name
@@ -123,11 +120,6 @@ public enum RequestMethod {
123120
DELETE
124121
}
125122

126-
public enum RequestType {
127-
NORMAL,
128-
MULTIPART
129-
}
130-
131123
/** URL-encodes a string. */
132124
public static String urlEncode(String str) {
133125
// Preserve original behavior that passing null for an object id will lead
@@ -140,7 +132,7 @@ public static String urlEncode(String str) {
140132
// Don't use strict form encoding by changing the square bracket control
141133
// characters back to their literals. This is fine by the server, and
142134
// makes these parameter strings easier to read.
143-
return URLEncoder.encode(str, CHARSET).replaceAll("%5B", "[").replaceAll("%5D", "]");
135+
return URLEncoder.encode(str, CHARSET.name()).replaceAll("%5B", "[").replaceAll("%5D", "]");
144136
} catch (UnsupportedEncodingException e) {
145137
// This can literally never happen, and lets us avoid having to catch
146138
// UnsupportedEncodingException in callers.
@@ -165,17 +157,6 @@ public static String urlEncodeId(String id) throws InvalidRequestException {
165157
return urlEncode(id);
166158
}
167159

168-
public static <T> T multipartRequest(
169-
ApiResource.RequestMethod method,
170-
String url,
171-
Map<String, Object> params,
172-
Class<T> clazz,
173-
RequestOptions options)
174-
throws StripeException {
175-
return ApiResource.stripeResponseGetter.request(
176-
method, url, params, clazz, ApiResource.RequestType.MULTIPART, options);
177-
}
178-
179160
public static <T> T request(
180161
ApiResource.RequestMethod method,
181162
String url,
@@ -194,8 +175,7 @@ public static <T> T request(
194175
Class<T> clazz,
195176
RequestOptions options)
196177
throws StripeException {
197-
return ApiResource.stripeResponseGetter.request(
198-
method, url, params, clazz, ApiResource.RequestType.NORMAL, options);
178+
return ApiResource.stripeResponseGetter.request(method, url, params, clazz, options);
199179
}
200180

201181
public static <T extends StripeCollectionInterface<?>> T requestCollection(

Diff for: src/main/java/com/stripe/net/ClientTelemetryPayload.java

-8
This file was deleted.

0 commit comments

Comments
 (0)