Skip to content

Commit b514b4e

Browse files
Merge changes from stripe/stripe-java master
2 parents 95043d0 + cb4d131 commit b514b4e

3 files changed

Lines changed: 123 additions & 1 deletion

File tree

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,32 @@ String secondaryValue =
231231
.getAsString();
232232
```
233233

234+
> [!NOTE]
235+
> `.getRawJsonObject()` is only available on the top-level object returned by an API call. For most requests (like `.retrieve()` or `.create()`) you'll get the object itself. But for `.list()` calls, the top level object is a `List<T>`, so you can only access the raw json of an individual object by going through the list itself.
236+
>
237+
> ```java
238+
> var cards = stripeClient
239+
> .v1()
240+
> .issuing()
241+
> .cards()
242+
> .list(params);
243+
>
244+
> // doesn't work:
245+
> cards
246+
> .getData()
247+
> .get(0)
248+
> .getRawJsonObject(); // null
249+
>
250+
> // instead, go through the list:
251+
> cards
252+
> .getRawJsonObject()
253+
> .getAsJsonArray("data")
254+
> .get(0)
255+
> .getAsJsonObject()
256+
> .getAsJsonPrimitive("undocumented-val")
257+
> .getAsString(); // "some-val"
258+
> ```
259+
234260
### Writing a plugin
235261
236262
If you're writing a plugin that uses the library, we'd appreciate it if you
@@ -259,7 +285,7 @@ Stripe.enableTelemetry = false;
259285
Stripe has features in the [public preview phase](https://docs.stripe.com/release-phases) that can be accessed via versions of this package that have the `-beta.X` suffix like `25.2.0-beta.2`.
260286
We would love for you to try these as we incrementally release new features and improve them based on your feedback.
261287

262-
To install, pick the latest version with the `beta` suffix by reviewing the [releases page](https://github.com/stripe/stripe-java/releases/) and then use it [installation steps above](#installation).
288+
To install, pick the latest version with the `beta` suffix by reviewing the [releases page](https://github.com/stripe/stripe-java/releases/) and then use it [installation steps above](#installation).
263289

264290
> **Note**
265291
> There can be breaking changes between two versions of the public preview SDKs without a bump in the major version. Therefore we recommend pinning the package version to a specific version. This way you can install the same version each time without breaking changes unless you are intentionally looking for the latest public preview SDK.
@@ -269,6 +295,7 @@ Some preview features require a name and version to be set in the `Stripe-Versio
269295
```java
270296
Stripe.addBetaVersion("feature_beta", "v3");
271297
```
298+
272299
### Private Preview SDKs
273300

274301
Stripe has features in the [private preview phase](https://docs.stripe.com/release-phases) that can be accessed via versions of this package that have the `-alpha.X` suffix like `25.2.0-alpha.2`. These are invite-only features. Once invited, you can install the private preview SDKs by following the same instructions as for the [public preview SDKs](https://github.com/stripe/stripe-java?tab=readme-ov-file#public-preview-sdks) above and replacing the term `beta` with `alpha`.

src/main/java/com/stripe/net/LiveStripeResponseGetter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import java.util.Map;
2323
import java.util.Optional;
2424
import java.util.function.Function;
25+
import java.util.logging.Logger;
2526

2627
public class LiveStripeResponseGetter implements StripeResponseGetter {
28+
private static final Logger logger = Logger.getLogger("Stripe");
29+
2730
private final HttpClient httpClient;
2831
private final StripeResponseGetterOptions options;
2932

@@ -151,6 +154,8 @@ public <T extends StripeObject> T request(ApiRequest apiRequest, Type typeToken)
151154
StripeResponse response =
152155
sendWithTelemetry(request, apiRequest.getUsage(), r -> httpClient.requestWithRetries(r));
153156

157+
maybeEmitStripeNotice(response.headers());
158+
154159
int responseCode = response.code();
155160
String responseBody = response.body();
156161
String requestId = response.requestId();
@@ -194,6 +199,8 @@ public InputStream requestStream(ApiRequest apiRequest) throws StripeException {
194199
sendWithTelemetry(
195200
request, apiRequest.getUsage(), r -> httpClient.requestStreamWithRetries(r));
196201

202+
maybeEmitStripeNotice(responseStream.headers());
203+
197204
int responseCode = responseStream.code();
198205

199206
if (responseCode < 200 || responseCode >= 300) {
@@ -275,6 +282,10 @@ public InputStream requestStream(
275282
return this.requestStream(new ApiRequest(baseAddress, method, path, params, options));
276283
}
277284

285+
private static void maybeEmitStripeNotice(HttpHeaders headers) {
286+
headers.firstValue("Stripe-Notice").ifPresent(logger::warning);
287+
}
288+
278289
private static HttpClient buildDefaultHttpClient() {
279290
return new HttpURLConnectionClient();
280291
}

src/test/java/com/stripe/functional/LiveStripeResponseGetterTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.stripe.functional;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertNotNull;
56
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
68

9+
import com.google.common.collect.ImmutableList;
10+
import com.google.common.collect.ImmutableMap;
711
import com.google.gson.JsonSyntaxException;
812
import com.stripe.BaseStripeTest;
913
import com.stripe.exception.ApiException;
@@ -18,12 +22,92 @@
1822
import com.stripe.net.StripeRequest;
1923
import com.stripe.net.StripeResponse;
2024
import com.stripe.net.StripeResponseGetter;
25+
import java.util.ArrayList;
2126
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.logging.Handler;
29+
import java.util.logging.Level;
30+
import java.util.logging.LogRecord;
31+
import java.util.logging.Logger;
2232
import org.hamcrest.CoreMatchers;
33+
import org.junit.jupiter.api.AfterEach;
34+
import org.junit.jupiter.api.BeforeEach;
2335
import org.junit.jupiter.api.Test;
2436
import org.mockito.Mockito;
2537

2638
public class LiveStripeResponseGetterTest extends BaseStripeTest {
39+
private Logger stripeLogger;
40+
private CapturingHandler logHandler;
41+
42+
private static class CapturingHandler extends Handler {
43+
final List<LogRecord> records = new ArrayList<>();
44+
45+
@Override
46+
public void publish(LogRecord record) {
47+
records.add(record);
48+
}
49+
50+
@Override
51+
public void flush() {}
52+
53+
@Override
54+
public void close() {}
55+
}
56+
57+
@BeforeEach
58+
public void setUpLogger() {
59+
stripeLogger = Logger.getLogger("Stripe");
60+
logHandler = new CapturingHandler();
61+
logHandler.setLevel(Level.ALL);
62+
stripeLogger.addHandler(logHandler);
63+
stripeLogger.setLevel(Level.ALL);
64+
stripeLogger.setUseParentHandlers(false);
65+
}
66+
67+
@AfterEach
68+
public void tearDownLogger() {
69+
stripeLogger.removeHandler(logHandler);
70+
stripeLogger.setUseParentHandlers(true);
71+
}
72+
73+
@Test
74+
public void testStripeNoticeHeaderEmitsWarning() throws StripeException {
75+
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
76+
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
77+
ApiResource.setGlobalResponseGetter(srg);
78+
StripeResponse response =
79+
new StripeResponse(
80+
200,
81+
HttpHeaders.of(
82+
ImmutableMap.of(
83+
"Stripe-Notice",
84+
ImmutableList.of("This API version will be deprecated soon."))),
85+
"{\"id\": \"sub_123\", \"object\": \"subscription\"}");
86+
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());
87+
Subscription.retrieve("sub_123");
88+
89+
assertEquals(1, logHandler.records.size());
90+
assertEquals(Level.WARNING, logHandler.records.get(0).getLevel());
91+
assertEquals(
92+
"This API version will be deprecated soon.", logHandler.records.get(0).getMessage());
93+
}
94+
95+
@Test
96+
public void testNoStripeNoticeHeaderEmitsNoWarning() throws StripeException {
97+
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
98+
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
99+
ApiResource.setGlobalResponseGetter(srg);
100+
StripeResponse response =
101+
new StripeResponse(
102+
200,
103+
HttpHeaders.of(Collections.emptyMap()),
104+
"{\"id\": \"sub_123\", \"object\": \"subscription\"}");
105+
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());
106+
Subscription.retrieve("sub_123");
107+
108+
assertTrue(logHandler.records.isEmpty());
109+
}
110+
27111
@Test
28112
public void testInvalidJson() throws StripeException {
29113
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());

0 commit comments

Comments
 (0)