Skip to content

Commit 40cc899

Browse files
committed
🌿 Add .fernignore and static files
1 parent fe0a431 commit 40cc899

File tree

1,092 files changed

+256201
-5
lines changed

Some content is hidden

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

1,092 files changed

+256201
-5
lines changed

.fernignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
# Specify files that shouldn't be modified by Fern
2+
README.md
3+
settings.gradle
4+
legacy-sdk
5+
src/test/java/com/squareup/square/integration
6+
src/main/java/com/squareup/square/core/SquareApiException.java
7+
src/main/java/com/squareup/square/utilities/WebhooksHelper.java
8+
src/test/resources/testdata

README.md

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
# Square Java Library
2+
3+
[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-SDK%20generated%20by%20Fern-brightgreen)](https://github.com/fern-api/fern)
4+
5+
The Square Java library provides convenient access to the Square API from Java.
6+
7+
## Requirements
8+
9+
Use of the Square Java SDK requires:
10+
11+
- Java 8+
12+
13+
## Installation
14+
15+
### Gradle
16+
17+
Add the dependency in your `build.gradle`:
18+
19+
```groovy
20+
dependencies {
21+
implementation 'com.squareup:square:44.0.0.20250319'
22+
}
23+
```
24+
25+
### Maven
26+
27+
Add the dependency in your `pom.xml`:
28+
29+
```xml
30+
<dependency>
31+
<groupId>com.squareup</groupId>
32+
<artifactId>square</artifactId>
33+
<version>44.0.0.20250319</version>
34+
</dependency>
35+
```
36+
37+
## Usage
38+
39+
Instantiate and use the client with the following:
40+
41+
```java
42+
package com.square.examples;
43+
44+
import com.squareup.square.SquareClient;
45+
import com.squareup.square.types.CashPaymentDetails;
46+
import com.squareup.square.types.CreatePaymentRequest;
47+
import com.squareup.square.types.CreatePaymentResponse;
48+
import com.squareup.square.types.Currency;
49+
import com.squareup.square.types.Money;
50+
51+
public class QuickStart {
52+
53+
public static void main(String[] args) {
54+
SquareClient square = SquareClient.builder().build();
55+
CreatePaymentResponse response = square.payments()
56+
.create(CreatePaymentRequest.builder()
57+
.sourceId("CASH")
58+
.idempotencyKey("4935a656-a929-4792-b97c-8848be85c27c")
59+
.amountMoney(Money.builder()
60+
.amount(100L)
61+
.currency(Currency.USD)
62+
.build())
63+
.tipMoney(Money.builder()
64+
.amount(50L)
65+
.currency(Currency.USD)
66+
.build())
67+
.cashDetails(CashPaymentDetails.builder()
68+
.buyerSuppliedMoney(Money.builder()
69+
.amount(200L)
70+
.currency(Currency.USD)
71+
.build())
72+
.build())
73+
.build());
74+
}
75+
}
76+
```
77+
78+
## Instantiation
79+
80+
To get started with the Square SDK, instantiate the `SquareClient` class as follows:
81+
82+
```java
83+
import com.squareup.square.SquareClient;
84+
85+
SquareClient square = SquareClient.builder().token("SQUARE_TOKEN").build();
86+
```
87+
88+
Alternatively, you can omit the token when constructing the client. In this case, the SDK will automatically read the
89+
token from the `SQUARE_TOKEN` environment variable:
90+
91+
```java
92+
import com.squareup.square.SquareClient;
93+
94+
SquareClient square = SquareClient.builder().build();
95+
```
96+
97+
### Environment and Custom URLs
98+
99+
This SDK allows you to configure different environments or custom URLs for API requests. You can either use the
100+
predefined environments or specify your own custom URL.
101+
102+
**Environments**
103+
104+
```java
105+
import com.squareup.square.SquareClient;
106+
import com.squareup.square.core.Environment;
107+
108+
SquareClient square = SquareClient.builder().environment(Environment.PRODUCTION).build();
109+
```
110+
111+
**Custom URL**
112+
113+
```java
114+
import com.squareup.square.SquareClient;
115+
116+
SquareClient square = SquareClient.builder().url("https://custom-staging.com").build();
117+
```
118+
119+
## Enums
120+
121+
This SDK wraps enums for forward compatibility. We define enum properties as constant type instances with `String` properties
122+
and use `valueOf` to specify custom enum types that may not yet be included as constants.
123+
124+
### Example Usage
125+
126+
**Supported Property**
127+
128+
```java
129+
import com.squareup.square.types.InvoicePaymentRequest;
130+
import com.squareup.square.types.InvoiceRequestType;
131+
132+
InvoicePaymentRequest paymentRequest = InvoicePaymentRequest.builder()
133+
.requestType(InvoiceRequestType.BALANCE)
134+
.build();
135+
```
136+
137+
**Custom Property**
138+
139+
```java
140+
import com.squareup.square.types.InvoicePaymentRequest;
141+
import com.squareup.square.types.InvoiceRequestType;
142+
143+
InvoicePaymentRequest paymentRequest = InvoicePaymentRequest.builder()
144+
.requestType(InvoiceRequestType.valueOf("CUSTOM"))
145+
.build();
146+
```
147+
148+
## Versioning
149+
By default, the SDK is pinned to the version 2025-03-19. If you would like to
150+
override this version you can simply pass in a request option.
151+
152+
```java
153+
client.cards().create(..., RequestOptions.builder()
154+
.version("2024-05-04") // override the version used
155+
.build());
156+
```
157+
158+
## Automatic Pagination
159+
160+
Paginated requests will return an `Iterable<T>`, which can be used to loop through the underlying items.
161+
162+
```java
163+
import com.squareup.square.SquareClient;
164+
import com.squareup.square.core.SyncPagingIterable;
165+
import com.squareup.square.types.Payment;
166+
import com.squareup.square.types.PaymentsListRequest;
167+
168+
SquareClient square = SquareClient.builder().token("YOUR_TOKEN").build();
169+
170+
SyncPagingIterable<Payment> payments =
171+
square.payments().list(PaymentsListRequest.builder().total(100L).build());
172+
173+
for (Payment payment : payments) {
174+
System.out.printf(
175+
"payment: ID: %s Created at: %s, Updated at: %s\n",
176+
payment.getId(), payment.getCreatedAt(), payment.getUpdatedAt());
177+
}
178+
```
179+
180+
or stream them:
181+
182+
```java
183+
square.payments()
184+
.list(PaymentsListRequest.builder().total(100L).build())
185+
.streamItems()
186+
.map(item -> ...);
187+
```
188+
189+
or calling `nextPage()` to perform the pagination manually:
190+
191+
```java
192+
// First page
193+
List<Payment> pagePayments = payments.getItems();
194+
for (Payment payment : pagePayments) {
195+
// ...
196+
}
197+
198+
// Remaining pages
199+
while (payments.hasNext()) {
200+
pagePayments = payments.nextPage().getItems();
201+
for (Payment payment : pagePayments) {
202+
// ...
203+
}
204+
}
205+
```
206+
207+
## Retries
208+
209+
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
210+
as the request is deemed retriable and the number of retry attempts has not grown larger than the configured
211+
retry limit (default: 2).
212+
213+
A request is deemed retriable when any of the following HTTP status codes is returned:
214+
215+
- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
216+
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
217+
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
218+
219+
Use the `maxRetries` request option to configure this behavior.
220+
221+
```java
222+
square.cards().create(..., RequestOptions.builder()
223+
.maxRetries(1)
224+
.build());
225+
```
226+
227+
## Timeouts
228+
229+
The SDK defaults to a 60 second timeout. You can configure this with a timeout
230+
option at the client or request level.
231+
232+
```java
233+
square.cards().create(..., RequestOptions.builder()
234+
.timeout(10)
235+
.build());
236+
```
237+
238+
## Exception Handling
239+
240+
When the API returns a non-success status code (`4xx` or `5xx` response), a `SquareApiException` will be thrown.
241+
242+
```java
243+
import com.squareup.square.SquareClient;
244+
import com.squareup.square.core.SquareApiException;
245+
246+
try {
247+
square.payments().create(...);
248+
} catch (SquareApiException e) {
249+
System.out.println("Square API exception occurred: " + e.getMessage());
250+
System.out.println("Status code: " + e.statusCode());
251+
System.out.println("Response body: " + e.body());
252+
}
253+
```
254+
255+
## Webhook Signature Verification
256+
257+
The SDK provides utility methods that allow you to verify webhook signatures and ensure that all webhook events
258+
originate from Square. The `WebhooksHelper.verifySignature` method can be used to easily verify the signature like so:
259+
260+
```java
261+
import com.squareup.square.utilities.WebhooksHelper;
262+
263+
boolean isValid = WebhooksHelper.verifySignature(
264+
requestBody,
265+
headers.get("x-square-hmacsha256-signature"),
266+
"YOUR_SIGNATURE_KEY",
267+
"https://example.com/webhook" // The URL where event notifications are sent.
268+
);
269+
```
270+
271+
## Legacy SDK
272+
273+
While the new SDK has a lot of improvements, we at Square understand that it takes time to upgrade when there are breaking changes.
274+
To make the migration easier, the new SDK also exports the legacy SDK as `com.squareup.square.legacy...`. Here's an example of how you
275+
can use the legacy SDK alongside the new SDK inside a single file:
276+
277+
```java
278+
import com.squareup.square.SquareClient;
279+
import com.squareup.square.core.Environment;
280+
281+
SquareClient square =
282+
SquareClient.builder()
283+
.environment(Environment.PRODUCTION)
284+
.token("YOUR_TOKEN")
285+
.build();
286+
287+
com.squareup.square.legacy.SquareClient legacyClient =
288+
new com.squareup.square.legacy.SquareClient.Builder()
289+
.environment(com.squareup.square.legacy.Environment.PRODUCTION)
290+
.accessToken("YOUR_TOKEN")
291+
.build();
292+
```
293+
294+
We recommend migrating to the new SDK using the following steps:
295+
296+
1. Include the following dependencies in your project
297+
298+
Gradle:
299+
300+
```groovy
301+
dependencies {
302+
implementation 'com.squareup:square:44.0.0.20250319'
303+
implementation 'com.squareup:square-legacy:44.0.0.20250319'
304+
}
305+
```
306+
307+
Maven:
308+
309+
```xml
310+
<dependency>
311+
<groupId>com.squareup</groupId>
312+
<artifactId>square</artifactId>
313+
<version>44.0.0.20250319</version>
314+
</dependency>
315+
<dependency>
316+
<groupId>com.squareup</groupId>
317+
<artifactId>square-legacy</artifactId>
318+
<version>44.0.0.20250319</version>
319+
</dependency>
320+
```
321+
322+
2. Search and replace all imports from `com.squareup.square` to `com.squareup.square.legacy`
323+
3. Gradually move over to use the new SDK by importing it from the `com.squareup.square` import
324+
## Advanced
325+
326+
### Custom HTTP Client
327+
328+
This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one. However, you can pass your own client like so:
329+
330+
```java
331+
import com.squareup.square.SquareClient;
332+
import com.squareup.square.core.Environment;
333+
import okhttp3.OkHttpClient;
334+
335+
OkHttpClient customClient = ... ;
336+
337+
SquareClient square =
338+
SquareClient.builder()
339+
.environment(Environment.PRODUCTION)
340+
.token("YOUR_TOKEN")
341+
.httpClient(customClient)
342+
.build();
343+
```
344+
345+
### Receive Additional Properties
346+
347+
Every response type includes the `getAdditionalProperties` method, which returns an array that contains any properties in the JSON response that
348+
were not specified in the returned class. Similar to the use case for sending additional parameters, this can be useful for API features not present in the SDK yet.
349+
350+
You can access the additional properties like so:
351+
352+
```java
353+
CreatePaymentResponse payments = square.payments().create(...);
354+
Map<String, Object> additionalProperties = payments.getAdditionalProperties();
355+
```
356+
357+
## Contributing
358+
359+
While we value open-source contributions to this SDK, this library is generated programmatically.
360+
Additions made directly to this library would have to be moved over to our generation code,
361+
otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
362+
a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
363+
an issue first to discuss with us!
364+
365+
On the other hand, contributions to the README are always very welcome!

0 commit comments

Comments
 (0)