Skip to content

Commit b8946da

Browse files
Update README.md
restore java readme
1 parent 361113c commit b8946da

File tree

1 file changed

+148
-146
lines changed

1 file changed

+148
-146
lines changed

README.md

Lines changed: 148 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
![Square logo]
22

3-
# Square Ruby SDK
3+
# Square Java SDK
44

5-
[![Travis status](https://travis-ci.org/square/square-ruby-sdk.svg?branch=master)](https://travis-ci.org/square/square-ruby-sdk)
6-
[![Gem version](https://badge.fury.io/rb/square.rb.svg?new)](https://badge.fury.io/rb/square.rb)
5+
[![Travis status](https://travis-ci.com/square/square-java-sdk.svg?branch=master)](https://travis-ci.com/square/square-java-sdk)
6+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.squareup/square/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.squareup/square)
77
[![Apache-2 license](https://img.shields.io/badge/license-Apache2-brightgreen.svg)](https://www.apache.org/licenses/LICENSE-2.0)
88

9-
Use this gem to integrate Square payments into your app and grow your business with Square APIs including Catalog, Customers, Employees, Inventory, Labor, Locations, and Orders.
9+
Use this library to integrate Square payments into your app and grow your business with Square APIs including Catalog, Customers, Employees, Inventory, Labor, Locations, and Orders.
10+
11+
## Requirements
12+
13+
Use of the Square Java SDK requires:
14+
15+
* Java 8 or better
16+
* Maven or Gradle to build and install the SDK.
1017

1118
## Installation
1219

13-
Install the gem from the command line:
20+
### Install with Maven
1421

15-
```ruby
16-
gem install square.rb
22+
Install the API client library to your local Maven repository:
23+
24+
```
25+
mvn install -DskipTests
1726
```
1827

19-
Or add the gem to your Gemfile and `bundle`:
28+
**OR**
29+
30+
Install the client dynamically by adding a dependency to the POM for your project:
2031

21-
```ruby
22-
gem 'square.rb'
2332
```
33+
<dependency>
34+
<groupId>com.squareup</groupId>
35+
<artifactId>square</artifactId>
36+
<version>6.0.0.20200625</version>
37+
</dependency>
38+
```
39+
40+
### Install with Gradle
41+
42+
Install the client by adding the following dependency to the build file for your project:
2443

25-
### API Client
26-
* [Client]
44+
```
45+
implementation "com.squareup:square:6.0.0.20200625"
46+
```
2747

2848
## API documentation
2949

@@ -56,7 +76,6 @@ gem 'square.rb'
5676
* [Devices]
5777

5878
### Team
59-
* [Team]
6079
* [Employees]
6180
* [Labor]
6281
* [Cash Drawers]
@@ -82,199 +101,184 @@ First time using Square? Here’s how to get started:
82101

83102
1. **Create a Square account.** If you don’t have one already, [sign up for a developer account].
84103
1. **Create an application.** Go to your [Developer Dashboard] and create your first application. All you need to do is give it a name. When you’re doing this for your production application, enter the name as you would want a customer to see it.
85-
1. **Make your first API call.** Almost all Square API calls require a location ID. You’ll make your first call to #list_locations, which happens to be one of the API calls that don’t require a location ID. For more information about locations, see the [Locations] API documentation.
86-
87-
Now let’s call your first Square API. Open your favorite text editor, create a new file called `locations.rb`, and copy the following code into that file:
88-
89-
```ruby
90-
require 'square'
91-
92-
# Create an instance of the API Client and initialize it with the credentials
93-
# for the Square account whose assets you want to manage.
94-
95-
client = Square::Client.new(
96-
access_token: 'YOUR SANDBOX ACCESS TOKEN HERE',
97-
environment: 'sandbox'
98-
)
99-
100-
# Call list_locations method to get all locations in this Square account
101-
result = client.locations.list_locations
102-
103-
# Call the #success? method to see if the call succeeded
104-
if result.success?
105-
# The #data Struct contains a list of locations
106-
locations = result.data.locations
107-
108-
# Iterate over the list
109-
locations.each do |location|
110-
# Each location is represented as a Hash
111-
location.each do |key, value|
112-
puts "#{key}: #{value}"
113-
end
114-
end
115-
else
116-
# Handle the case that the result is an error.
117-
warn 'Error calling LocationsApi.listlocations ...'
118-
119-
# The #errors method returns an Array of error Hashes
120-
result.errors.each do |key, value|
121-
warn "#{key}: #{value}"
122-
end
123-
end
124-
```
125-
126-
Next, get an access token and reference it in your code. Go back to your application in the Developer Dashboard, in the Sandbox section click Show in the Sandbox Access Token box, copy that access token, and replace `'YOUR SANDBOX ACCESS TOKEN HERE'` with that token.
127-
128-
**Important** When you eventually switch from trying things out on sandbox to actually working with your real production resources, you should not embed the access token in your code. Make sure you store and access your production access tokens securely.
129-
130-
Now save `locations.rb` and run it:
131-
132-
```sh
133-
ruby locations.rb
134-
```
135-
136-
If your call is successful, you’ll get a response that looks like this:
137-
104+
1. **Make your first API call.** Almost all Square API calls require a location ID. You’ll make your first call to `listLocations`, which happens to be one of the API calls that don’t require a location ID. For more information about locations, see the [Locations] API documentation.
105+
106+
Now let’s call your first Square API.
107+
108+
```java
109+
import java.util.List;
110+
import java.io.IOException;
111+
import com.squareup.square.Environment;
112+
import com.squareup.square.SquareClient;
113+
import com.squareup.square.exceptions.ApiException;
114+
import com.squareup.square.http.client.HttpContext;
115+
import com.squareup.square.api.LocationsApi;
116+
import com.squareup.square.models.Location;
117+
import com.squareup.square.models.Error;
118+
public class Example {
119+
public static void main(String[] args) {
120+
SquareClient client = new SquareClient.Builder()
121+
.environment(Environment.SANDBOX)
122+
.accessToken("YOUR_SANDBOX_ACCESS_TOKEN")
123+
.build();
124+
LocationsApi api = client.getLocationsApi();
125+
try {
126+
List<Location> locations = api.listLocations().getLocations();
127+
// Your business logic code
128+
System.out.println("calling listLocations successfully");
129+
} catch (ApiException e) {
130+
List<Error> errors = e.getErrors();
131+
int statusCode = e.getResponseCode();
132+
HttpContext httpContext = e.getHttpContext();
133+
// Your error handling code
134+
System.err.println("ApiException when calling API");
135+
e.printStackTrace();
136+
}
137+
catch (IOException e) {
138+
// Your error handling code
139+
System.err.println("IOException when calling API");
140+
e.printStackTrace();
141+
}
142+
}
143+
}
138144
```
139-
address : {'address_line_1': '1455 Market Street', 'administrative_district_level_1': 'CA', 'country': 'US', 'locality': 'San Francisco', 'postal_code': '94103'}
140-
# ...
141-
```
142-
143-
Yay! You successfully made your first call. If you didn’t, you would see an error message that looks something like this:
144145

145-
```
146-
Error calling LocationsApi.listlocations
147-
category : AUTHENTICATION_ERROR
148-
code : UNAUTHORIZED
149-
detail : This request could not be authorized.
150-
```
146+
Next, get an access token and reference it in your code:
151147

152-
This error was returned when an invalid token was used to call the API.
148+
1. Open the Developer Dashboard and select your application. The **Credentials** page for your app opens by default.
149+
1. Set the dashboard mode to **Sandbox Settings** for a sandbox access token.
150+
1. Copy the Access Token in the Credentials section of the page and replace `YOUR_SANDBOX_ACCESS_TOKEN` with the token.
153151

154-
After you’ve tried out the Square APIs and tested your application using sandbox, you will want to switch to your production credentials so that you can manage real Square resources. Don't forget to switch your access token from sandbox to production for real data.
152+
**Important** When you eventually switch from trying things out on sandbox to actually working with your real production resources, you should not embed the access token in your code. Make sure you store and access your production access tokens securely.
155153

156154
## SDK patterns
157155
If you know a few patterns, you’ll be able to call any API in the SDK. Here are some important ones:
158156

159157
### Get an access token
160158

161-
To use the Square API to manage the resources (such as payments, orders, customers, etc.) of a Square account, you need to create an application (or use an existing one) in the Developer Dashboard and get an access token.
162-
163-
When you call a Square API, you call it using an access key. An access key has specific permissions to resources in a specific Square account that can be accessed by a specific application in a specific developer account.
159+
To use the Square API to manage the resources of a Square account (payments, orders, customers, etc.), you need to create an application (or use an existing one) in the Developer Dashboard and get an access token. Access tokens have specific permissions to resources in a specific Square account that can be accessed by a specific application in a specific developer account.
164160
Use an access token that is appropriate for your use case. There are two options:
165161

166-
- To manage the resources for your own Square account, use the Personal Access Token for the application created in your Square account.
167-
- To manage resources for other Square accounts, use OAuth to ask owners of the accounts you want to manage so that you can work on their behalf. When you implement OAuth, you ask the Square account holder for permission to manage resources in their account (you can define the specific resources to access) and get an OAuth access token and refresh token for their account.
162+
- To manage the resources for your own Square account, use the **Personal Access Token** for the application created in your Square account.
163+
- To manage resources for other Square accounts, use OAuth to ask owners of the accounts you want to manage so that you can work on their behalf. When you implement OAuth, you ask the Square account holder for permission to manage resources in their account and get an OAuth access token and refresh token for their account. You define the specific resources you want to access as part of the OAuth call.
168164

169165
**Important** For both use cases, make sure you store and access the tokens securely.
170166

171167
### Import and Instantiate the Client Class
172168

173169
To use the Square API, you import the Client class, instantiate a Client object, and initialize it with the appropriate access token. Here’s how:
174170

175-
- Instantiate a `Square::Client` object with the access token for the Square account whose resources you want to manage. To access sandbox resources, initialize the `Square::Client` with environment set to sandbox:
171+
- Initialize the `SquareClient` with environment set to sandbox:
176172

177-
```ruby
178-
client = Square::Client.new(
179-
access_token: 'SANDBOX ACCESS TOKEN HERE',
180-
environment: 'sandbox'
181-
)
173+
```java
174+
SquareClient client = new SquareClient.Builder()
175+
.environment(Environment.SANDBOX)
176+
.accessToken("SANDBOX ACCESS TOKEN HERE")
177+
.build();
182178
```
183179

184180
- To access production resources, set environment to production:
185181

186-
```ruby
187-
client = Square::Client.new(
188-
access_token: 'ACCESS TOKEN HERE',
189-
environment: 'production'
190-
)
182+
```java
183+
SquareClient client = new SquareClient.Builder()
184+
.environment(Environment.PRODUCTION)
185+
.accessToken("ACCESS TOKEN HERE")
186+
.build();
191187
```
192-
188+
193189
### Get an Instance of an API object and call its methods
194190

195191
Each API is implemented as a class. The Client object instantiates every API class and exposes them as properties so you can easily start using any Square API. You work with an API by calling methods on an instance of an API class. Here’s how:
196192

197-
- Work with an API by calling the methods on the API object. For example, you would call list_customers to get a list of all customers in the Square account:
193+
- Work with an API by calling the methods on the API object. For example, you would call listCustomers to get a list of all customers in the Square account:
198194

199-
```ruby
200-
result = client.customers.list_customers
195+
```java
196+
CustomersApi api = client.getCustomersApi();
197+
ListCustomersResponse listCustomersRes = api.listCustomers(null, null, null);
201198
```
202199

203200
See the SDK documentation for the list of methods for each API class.
204201

205-
Pass complex parameters (such as create, update, search, etc.) as a Hash. For example, you would pass a Hash containing the values used to create a new customer using create_customer:
206-
207-
```ruby
208-
# Create a unique key for this creation operation so you don't accidentally
209-
# create the customer multiple times if you need to retry this operation.
210-
require 'securerandom'
211-
212-
idempotency_key = SecureRandom.uuid
213-
214-
# To create a customer, you'll need to specify at least a few required fields.
215-
request_body = {idempotency_key: idempotency_key, given_name: 'Amelia', family_name: 'Earhardt'}
216-
217-
# Call create_customer method to create a new customer in this Square account
218-
result = client.customers.create_customer(request_body)
219-
```
220-
221-
If your call succeeds, you’ll see a response that looks like this:
222-
```
223-
{'customer': {'created_at': '2019-06-28T21:23:05.126Z', 'creation_source': 'THIRD_PARTY', 'family_name': 'Earhardt', 'given_name': 'Amelia', 'id': 'CBASEDwl3El91nohQ2FLEk4aBfcgAQ', 'preferences': {'email_unsubscribed': False}, 'updated_at': '2019-06-28T21:23:05.126Z'}}
202+
- Pass complex parameters such as create, update, or search as a model. For example, you would pass a model containing the values used to create a new customer using create_customer:
203+
204+
```java
205+
CustomersApi api = client.getCustomersApi();
206+
Address address = new Address.Builder()
207+
.addressLine1("1455 Market St")
208+
.addressLine2("San Francisco, CA 94103")
209+
.build();
210+
// Create a unique key(idempotency) for this creation operation so you don't accidentally
211+
// create the customer multiple times if you need to retry this operation.
212+
// For the purpose of example, we mark it as `unique_idempotency_key`
213+
CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest.Builder()
214+
.idempotencyKey("unique_idempotency_key")
215+
.givenName("John")
216+
.familyName("Smith")
217+
.address(address)
218+
.build();
219+
// Call createCustomer method to create a new customer in this Square account
220+
try {
221+
CreateCustomerResponse response = api.createCustomer(createCustomerRequest);
222+
} catch (ApiException e) {
223+
List<Error> errors = e.getErrors();
224+
int statusCode = e.getResponseCode();
225+
HttpContext httpContext = e.getHttpContext();
226+
// Your error handling code
227+
System.err.println("ApiException when calling API");
228+
e.printStackTrace();
229+
}
224230
```
225231

226232
- Use idempotency for create, update, or other calls that you want to avoid calling twice. To make an idempotent API call, you add the idempotency_key with a unique value in the Hash for the API call’s request.
227233
- Specify a location ID for APIs such as Transactions, Orders, and Checkout that deal with payments. When a payment or order is created in Square, it is always associated with a location.
228234

229235
### Handle the response
230236

231-
API calls return a response object that contains properties that describe both the request (headers and request) and the response (status_code, reason_phrase, text, errors, body, and cursor). The response also has #success? and #error? helper methods so you can easily determine the success or failure of a call:
232-
233-
```ruby
234-
if result.success?
235-
p result.data
236-
elsif result.error?
237-
warn result.errors.inspect
238-
end
237+
If your API call succeeds, Square API returns a response object containing an `HttpContext` that describe both the request and the response. Otherwise, the API throws an `ApiException`:
238+
239+
```java
240+
try {
241+
List<Location> locations = api.listLocations().getLocations();
242+
} catch (ApiException e) {
243+
List<Error> errors = e.getErrors();
244+
int statusCode = e.getResponseCode();
245+
HttpContext httpContext = e.getHttpContext();
246+
// Your error handling code
247+
System.err.println("ApiException when calling API");
248+
e.printStackTrace();
249+
}
239250
```
240251

241-
- Read the response payload. The response payload is returned as a Struct from the #data method. For retrieve calls, a Struct containing a single item is returned with a key name that is the name of the object (for example, customer). For list calls, a Struct containing a Array of objects is returned with a key name that is the plural of the object name (for example, customers).
242-
- Make sure you get all items returned in a list call by checking the cursor value returned in the API response. When you call a list API the first time, set the cursor to an empty String or omit it from the API request. If the API response contains a cursor with a value, you call the API again to get the next page of items and continue to call that API again until the cursor is an empty String.
243-
244252
## Tests
245253

246-
First, clone the gem locally and `cd` into the directory.
254+
First, clone the repo locally and `cd` into the directory.
247255

248256
```sh
249-
git clone https://github.com/square/square-ruby-sdk.git
250-
cd square-ruby-sdk
257+
git clone https://github.com/square/square-java-sdk.git
258+
cd square-java-sdk
251259
```
252260

253-
Next, make sure Bundler is installed and install the development dependencies.
261+
Before running the tests, find a sandbox token in your [Developer Dashboard] and set a `SQUARE_ACCESS_TOKEN` environment variable.
254262

255263
```sh
256-
gem install bundler
257-
bundle
264+
export SQUARE_ENVIRONMENT=sandbox
265+
export SQUARE_ACCESS_TOKEN="YOUR_SANDBOX_ACCESS_TOKEN"
258266
```
259267

260-
Before running the tests, find a sandbox token in your [Developer Dashboard] and set a `SQUARE_SANDBOX_TOKEN` environment variable.
268+
If you are using Maven, run the tests with below command
261269

262270
```sh
263-
export SQUARE_SANDBOX_TOKEN="YOUR SANDBOX TOKEN HERE"
264-
```
265-
266-
And run the tests.
267-
268-
```sh
269-
rake
271+
mvn test
270272
```
271273

272274
## Learn more
273275

274-
The Square Platform is built on the [Square API]. Square has a number of other SDKs that enable you to securely handle credit card information on both mobile and web so that you can process payments via the Square API.
276+
The Square Platform is built on the [Square API]. Square has a number of other SDKs that enable you to securely handle credit card information on both mobile and web so that you can process payments via the Square API.
275277

276278
You can also use the Square API to create applications or services that work with payments, orders, inventory, etc. that have been created and managed in Square’s in-person hardware products (Square Point of Sale and Square Register).
277279

280+
281+
[//]: # "Link anchor definitions"
278282
[Square Logo]: https://docs.connect.squareup.com/images/github/github-square-logo.svg
279283
[Developer Dashboard]: https://developer.squareup.com/apps
280284
[Square API]: https://squareup.com/developers
@@ -283,7 +287,6 @@ You can also use the Square API to create applications or services that work wit
283287
[Devices]: doc/devices.md
284288
[Disputes]: doc/disputes.md
285289
[Terminal]: doc/terminal.md
286-
[Team]: doc/team.md
287290
[Cash Drawers]: doc/cash-drawers.md
288291
[Customer Groups]: doc/customer-groups.md
289292
[Customer Segments]: doc/customer-segments.md
@@ -308,4 +311,3 @@ You can also use the Square API to create applications or services that work wit
308311
[V1 Employees]: doc/v1-employees.md
309312
[V1 Transactions]: doc/v1-transactions.md
310313
[V1 Items]: doc/v1-items.md
311-
[Transactions]: doc/transactions.md

0 commit comments

Comments
 (0)