Skip to content

Commit e81cb11

Browse files
Merge pull request #2189 from stripe/xavdid/merge-java-beta
Merge to beta
2 parents e3ff4a9 + 4791250 commit e81cb11

18 files changed

+293
-151
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
- "25"
7373
# non-LTS versions
7474
# we should periodically add the latest non-LTS version here to test against
75-
# as of 2025-09-29, the latest version of java (25) is LTS, so this section is empty. In March 2026, we'll add "26".
75+
- "26"
7676

7777
steps:
7878
- uses: extractions/setup-just@v2
@@ -110,8 +110,7 @@ jobs:
110110
publish:
111111
if: >-
112112
((github.event_name == 'workflow_dispatch') || (github.event_name == 'push')) &&
113-
startsWith(github.ref, 'refs/tags/v') &&
114-
endsWith(github.actor, '-stripe')
113+
startsWith(github.ref, 'refs/tags/v')
115114
needs: [build, test]
116115
runs-on: "ubuntu-24.04"
117116
permissions:
@@ -153,12 +152,11 @@ jobs:
153152
((github.event_name == 'workflow_dispatch') || (github.event_name == 'push')) &&
154153
startsWith(github.ref, 'refs/tags/v') &&
155154
!contains(github.ref, 'beta') &&
156-
!contains(github.ref, 'alpha') &&
157-
endsWith(github.actor, '-stripe')
155+
!contains(github.ref, 'alpha')
158156
needs: [build, test]
159157
runs-on: "ubuntu-24.04"
160158
permissions:
161-
contents: write
159+
contents: write
162160
steps:
163161
- uses: actions/checkout@master
164162
- name: Setup Java

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
[![JavaDoc](http://img.shields.io/badge/javadoc-reference-blue.svg)](https://stripe.dev/stripe-java)
55
[![Build Status](https://github.com/stripe/stripe-java/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/stripe/stripe-java/actions?query=branch%3Amaster)
66

7+
> [!TIP]
8+
> Want to chat live with Stripe engineers? Join us on our [Discord server](https://stripe.com/go/discord/java).
9+
710
The official [Stripe][stripe] Java client library.
811

912
## Installation

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jar {
100100
"Implementation-Vendor": VENDOR_NAME,
101101
"Bundle-SymbolicName": POM_ARTIFACT_ID,
102102
"Export-Package": "com.stripe.*",
103-
"Automatic-Module-Name": "stripe.java")
103+
"Automatic-Module-Name": "com.stripe")
104104

105105
archiveVersion = VERSION_NAME
106106
}

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import? '../sdk-codegen/utils.just'
55
_default:
66
just --list --unsorted
77

8+
# ⭐ run format and tests to prepare for CI
9+
[no-exit-message]
10+
prepare: format test
11+
812
# ⭐ run the whole test suite
913
[no-exit-message]
1014
test *args:

src/main/java/com/stripe/StripeClient.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ public static final class StripeClientBuilder {
11971197
private String meterEventsBase = Stripe.METER_EVENTS_API_BASE;
11981198
private String stripeAccount;
11991199
private String stripeContext;
1200+
private HttpClient httpClient;
12001201

12011202
/**
12021203
* Constructs a request options builder with the global parameters (API key and client ID) as
@@ -1208,19 +1209,29 @@ public Authenticator getAuthenticator() {
12081209
return this.authenticator;
12091210
}
12101211

1212+
/**
1213+
* Sets the authenticator used to authorize requests. Use this for custom authentication
1214+
* strategies. For standard API key authentication, prefer {@link #setApiKey(String)}.
1215+
*
1216+
* <p>This shares a backing field with {@link #setApiKey(String)} — calling one overwrites the
1217+
* other.
1218+
*
1219+
* @param authenticator the authenticator to use
1220+
*/
12111221
public StripeClientBuilder setAuthenticator(Authenticator authenticator) {
12121222
this.authenticator = authenticator;
12131223
return this;
12141224
}
12151225

1216-
public String getApiKey() {
1217-
if (authenticator instanceof BearerTokenAuthenticator) {
1218-
return ((BearerTokenAuthenticator) authenticator).getApiKey();
1219-
}
1220-
1221-
return null;
1222-
}
1223-
1226+
/**
1227+
* Sets the API key for bearer token authentication. This is a convenience method equivalent to
1228+
* calling {@code setAuthenticator(new BearerTokenAuthenticator(apiKey))}.
1229+
*
1230+
* <p>This shares a backing field with {@link #setAuthenticator(Authenticator)} — calling one
1231+
* overwrites the other.
1232+
*
1233+
* @param apiKey the API key; if null, clears the authenticator
1234+
*/
12241235
public StripeClientBuilder setApiKey(String apiKey) {
12251236
if (apiKey == null) {
12261237
this.authenticator = null;
@@ -1328,7 +1339,8 @@ public StripeClientBuilder setProxyCredential(PasswordAuthentication proxyCreden
13281339
* Set the base URL for the Stripe API. By default this is "https://api.stripe.com".
13291340
*
13301341
* <p>This only affects requests made with a {@link com.stripe.net.BaseAddress} of API. Use
1331-
* {@link setFilesBase} or {@link setConnectBase} to interpect requests with other bases.
1342+
* {@link #setFilesBase}, {@link #setConnectBase} or {@link #setMeterEventsBase} to interpect
1343+
* requests with other bases.
13321344
*/
13331345
public StripeClientBuilder setApiBase(String address) {
13341346
this.apiBase = address;
@@ -1405,9 +1417,23 @@ public String getStripeContext() {
14051417
return this.stripeContext;
14061418
}
14071419

1420+
/**
1421+
* Sets the HTTP client to use for making requests to the Stripe API. If not set, a default
1422+
* {@link HttpURLConnectionClient} will be created.
1423+
*
1424+
* <p>This is useful for providing a custom HTTP client implementation, e.g. for testing or for
1425+
* using a different HTTP library.
1426+
*
1427+
* @param httpClient the HTTP client to use
1428+
*/
1429+
public StripeClientBuilder setHttpClient(HttpClient httpClient) {
1430+
this.httpClient = httpClient;
1431+
return this;
1432+
}
1433+
14081434
/** Constructs a {@link StripeResponseGetterOptions} with the specified values. */
14091435
public StripeClient build() {
1410-
return new StripeClient(new LiveStripeResponseGetter(buildOptions(), null));
1436+
return new StripeClient(new LiveStripeResponseGetter(buildOptions(), this.httpClient));
14111437
}
14121438

14131439
StripeResponseGetterOptions buildOptions() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.stripe.model;
2+
3+
import com.google.gson.TypeAdapter;
4+
import com.google.gson.stream.JsonReader;
5+
import com.google.gson.stream.JsonToken;
6+
import com.google.gson.stream.JsonWriter;
7+
import java.io.IOException;
8+
9+
public class StringInt64TypeAdapter extends TypeAdapter<Long> {
10+
/** Serializes Long values as JSON strings and deserializes string-encoded integers. */
11+
@Override
12+
public void write(JsonWriter out, Long value) throws IOException {
13+
if (value == null) {
14+
out.nullValue();
15+
return;
16+
}
17+
18+
out.value(value.toString());
19+
}
20+
21+
@Override
22+
public Long read(JsonReader in) throws IOException {
23+
JsonToken token = in.peek();
24+
if (token == JsonToken.NULL) {
25+
in.nextNull();
26+
return null;
27+
}
28+
29+
return Long.valueOf(in.nextString());
30+
}
31+
}

src/main/java/com/stripe/model/v2/core/Event.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected StripeObject fetchRelatedObject(RelatedObject relatedObject) throws St
9999
}
100100

101101
RequestOptions.RequestOptionsBuilder optsBuilder = new RequestOptions.RequestOptionsBuilder();
102-
// optsBuilder.setStripeRequestTrigger("event=" + id); // TODO https://go/j/DEVSDK-3018
102+
optsBuilder.setStripeRequestTrigger("event=" + id);
103103

104104
if (context != null) {
105105
optsBuilder.setStripeAccount(context);

src/main/java/com/stripe/model/v2/core/EventNotification.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ public static EventNotification fromJson(String payload, StripeClient client) {
106106
}
107107

108108
private RawRequestOptions getRequestOptions() {
109-
if (context == null) {
110-
return null;
109+
RawRequestOptions.RawRequestOptionsBuilder builder =
110+
RawRequestOptions.builder().setStripeRequestTrigger("event=" + id);
111+
if (context != null) {
112+
builder.setStripeContext(context.toString());
111113
}
112-
return new RawRequestOptions.RawRequestOptionsBuilder()
113-
.setStripeContext(context.toString())
114-
.build();
114+
return builder.build();
115115
}
116116

117117
/* retrieves the full payload for an event. Protected because individual push classes use it, but type it correctly */

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
import com.stripe.exception.StripeException;
44

5-
/** * Represents a request authentication mechanism. */
5+
/**
6+
* Interface for applying authentication to a request. This is used by {@link StripeResponseGetter}
7+
* to apply authentication to requests before they are sent. Implementations of this interface
8+
* should not modify the request in-place, but should instead return a new request with the
9+
* appropriate authentication headers applied. The default implementation of this interface is
10+
* {@link BearerTokenAuthenticator}, which applies the API key as a Bearer token in the
11+
* Authorization header. Users can also implement this interface to provide custom authentication
12+
* logic, often by wrapping the BearerTokenAuthenticator and applying additional headers or logic as
13+
* needed.
14+
*/
615
public interface Authenticator {
716
/**
817
* * Authenticate the request

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

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,9 @@
88
public class RawRequestOptions extends RequestOptions {
99
private Map<String, String> additionalHeaders;
1010

11-
public RawRequestOptions(
12-
Authenticator authenticator,
13-
String clientId,
14-
String idempotencyKey,
15-
String stripeContext,
16-
String stripeAccount,
17-
String stripeVersionOverride,
18-
String baseUrl,
19-
Integer connectTimeout,
20-
Integer readTimeout,
21-
Integer maxNetworkRetries,
22-
Proxy connectionProxy,
23-
PasswordAuthentication proxyCredential,
24-
Map<String, String> additionalHeaders) {
25-
super(
26-
authenticator,
27-
clientId,
28-
idempotencyKey,
29-
stripeContext,
30-
stripeAccount,
31-
stripeVersionOverride,
32-
baseUrl,
33-
connectTimeout,
34-
readTimeout,
35-
maxNetworkRetries,
36-
connectionProxy,
37-
proxyCredential);
38-
this.additionalHeaders = additionalHeaders;
11+
private RawRequestOptions(RawRequestOptionsBuilder builder) {
12+
super(builder);
13+
this.additionalHeaders = builder.additionalHeaders;
3914
}
4015

4116
public Map<String, String> getAdditionalHeaders() {
@@ -88,6 +63,12 @@ public RawRequestOptionsBuilder setStripeContext(StripeContext stripeContext) {
8863
return this;
8964
}
9065

66+
@Override
67+
public RawRequestOptionsBuilder setStripeRequestTrigger(String stripeRequestTrigger) {
68+
super.setStripeRequestTrigger(stripeRequestTrigger);
69+
return this;
70+
}
71+
9172
@Override
9273
public RawRequestOptionsBuilder setStripeAccount(String stripeAccount) {
9374
super.setStripeAccount(stripeAccount);
@@ -132,20 +113,7 @@ public RawRequestOptionsBuilder setProxyCredential(PasswordAuthentication proxyC
132113

133114
@Override
134115
public RawRequestOptions build() {
135-
return new RawRequestOptions(
136-
authenticator,
137-
normalizeClientId(this.clientId),
138-
normalizeIdempotencyKey(this.idempotencyKey),
139-
normalizeStripeContext(this.stripeContext),
140-
normalizeStripeAccount(this.stripeAccount),
141-
normalizeStripeVersion(this.stripeVersionOverride),
142-
normalizeBaseUrl(this.baseUrl),
143-
connectTimeout,
144-
readTimeout,
145-
maxNetworkRetries,
146-
connectionProxy,
147-
proxyCredential,
148-
additionalHeaders);
116+
return new RawRequestOptions(this);
149117
}
150118
}
151119
}

0 commit comments

Comments
 (0)