-
Notifications
You must be signed in to change notification settings - Fork 389
Migration guide for v17
Version 17 of stripe-java contains a number of changes. This guide will help you update your Stripe integration so that it keeps working as expected after you upgrade to v17.
The library can now automatically retry certain failed requests (on timeouts, or if the API returns an unexpected error). This behavior is disabled by default. You can enable it in one of two ways:
- per request, by calling
setMaxNetworkRetries()onRequestOptionsBuilderand passing the resultingRequestOptionsinstance to your request method calls - globally, by setting
Stripe.setMaxNetworkRetries()
The former takes precedence over the latter.
We don't recommend setting this higher than 2 or 3.
The core of the library has been redesigned to make it much easier to write custom HTTP clients, allowing you to use your favorite HTTP library or mock requests easily.
In order to use a custom HTTP client, you would need to:
-
write a concrete class that extends the
com.stripe.net.HttpClientabstract class (you can look at the source code forcom.stripe.net.HttpURLConnectionClientfor inspiration) -
instruct the stripe-java library to use your custom client like so:
import com.stripe.net.ApiResource; import com.stripe.net.HttpClient; import com.stripe.net.LiveStripeResponseGetter; import com.stripe.net.StripeResponseGetter; HttpClient myClient = new MyCustomClient(); StripeResponseGetter srg = new LiveStripeResponseGetter(myClient); ApiResource.setStripeResponseGetter(srg);
The new design of the library also makes it much easier to use mocking libraries to mock requests without having to write a custom implementation. For instance, using Mockito, you can now do something like this:
HttpClient httpClient = Mockito.mock(
HttpClient.class,
withSettings().useConstructor().defaultAnswer(Mockito.CALLS_REAL_METHODS));
StripeResponseGetter srg = new LiveStripeResponseGetter(myClient);
ApiResource.setStripeResponseGetter(srg);
HttpHeaders emptyHeaders = HttpHeaders.of(Collections.emptyMap());
String json = "{\"id\": \"ch_123\", \"object\": \"charge\"}";
Mockito.when(this.client.request(any(StripeRequest.class)))
.thenReturn(new StripeResponse(200, emptyHeaders, json));
Charge charge = Charge.retrieve("ch_123");Many deprecated methods were removed:
| Class | Removed method | Replacement |
|---|---|---|
Collection |
getCount() |
None |
Collection |
getTotalCount() |
None |
Event |
getUserId() |
getAccount() |
issuing.Card |
getPhone() |
None |
SetupIntentCreateParams.Builder |
setUsage(String) |
setUsage(Usage) |
issuing.CardCreateParams.Builder |
setType(String) |
setType(Type) |
issuing.CardholderCreateParams.Builder |
setName(String) |
None |
issuing.CardholderUpdateParams.Builder |
setName(String) |
None |
terminal.ConnectionTokenCreateParams.Builder |
setOperatorAccount(String) |
None |
terminal.LocationCreateParams.Builder |
setOperatorAccount(String) |
None |
terminal.LocationListParams.Builder |
setOperatorAccount(String) |
None |
terminal.LocationRetrieveParams.Builder |
setOperatorAccount(String) |
None |
terminal.LocationUpdateParams.Builder |
setOperatorAccount(String) |
None |
terminal.ReaderCreateParams.Builder |
setOperatorAccount(String) |
None |
terminal.ReaderListParams.Builder |
setDeviceType(String) |
setDeviceType(DeviceType) |
terminal.ReaderListParams.Builder |
setOperatorAccount(String) |
None |
terminal.ReaderListParams.Builder |
setStatus(String) |
setStatus(Status) |
terminal.ReaderRetrieveParams.Builder |
setOperatorAccount(String) |
None |
terminal.ReaderUpdateParams.Builder |
setOperatorAccount(String) |
None |
The following classes were removed:
com.stripe.net.ClientTelemetryPayloadcom.stripe.net.RequestMetricscom.stripe.net.StripeHeaders
The following enum was removed:
com.stripe.net.ApiResource.RequestType
The StripeResponse class was updated to be more consistent with the rest of the codebase:
-
the existing constructors were removed. The class now has a single constructor with the following signature:
StripeResponse(int code, HttpHeaders headers, String body)
Both
headersandbodymust not benull. If you're manually creatingStripeResponses, you can create an empty headers object withHttpHeaders.of(Collections.emptyMap())and pass an empty string""forbody. -
headers()now returns acom.stripe.net.HttpHeaders(previously was acom.stripe.net.StripeHeaders). The API ofcom.stripe.net.HttpHeadersis close to that of thejava.net.http.HttpHeadersclass introduced in Java 11 (which stripe-java can't use, because it will support Java 8 for the foreseeable future). If you're accessing custom headers, you should be able to replace most calls to:stripeResponse.headers().get("My-Header")
with:
stripeResponse.headers().firstValue("My-Header").orElse(null)
-
ApiResource.CHARSETis now aCharset(was aString) -
MultipartProcessor's constructor now accepts aCharsetas its last parameter (previously was aString)
Previously, the library would try to disable the DNS cache by setting the networkaddress.cache.ttl property to 0.
While disabling the DNS cache can be helpful in some rare situations, we don't think a library should be changing a global setting like this, so we've removed this behavior entirely. If you need to disable the DNS cache for any reason, you'll now have to do it from your own code:
java.security.Security.setProperty("networkaddress.cache.ttl", "0");Previously, the library would allow for setting a custom URLStreamHandler implementation by setting the com.stripe.net.customURLStreamHandler property to the name of the custom class.
Support for this has been removed entirely. If you need to provide a custom URLStreamHandler, you should also write a custom URLStreamHandlerFactory and register the factory using URL.setURLStreamHandlerFactory() in your application's startup code.