|
| 1 | +![Square logo] |
| 2 | + |
| 3 | +# Square Java SDK |
| 4 | + |
| 5 | +[](https://travis-ci.org/square/square-java-sdk) |
| 6 | +[](https://maven-badges.herokuapp.com/maven-central/com.squareup/square) |
| 7 | +[](https://www.apache.org/licenses/LICENSE-2.0) |
| 8 | + |
| 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. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +### Install with Maven |
| 21 | + |
| 22 | +Install the API client library to your local Maven repository: |
| 23 | + |
| 24 | +``` |
| 25 | +mvn install -DskipTests |
| 26 | +``` |
| 27 | + |
| 28 | +**OR** |
| 29 | + |
| 30 | +Install the client dynamically by adding a dependency to the POM for your project: |
| 31 | + |
| 32 | +``` |
| 33 | +<dependency> |
| 34 | + <groupId>com.squareup</groupId> |
| 35 | + <artifactId>square</artifactId> |
| 36 | + <version>4.0.0.20191217</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: |
| 43 | + |
| 44 | +``` |
| 45 | +implementation "com.squareup:square:4.0.0.20191217" |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +## API documentation |
| 50 | + |
| 51 | +### Take Payments |
| 52 | + |
| 53 | +* [Payments] |
| 54 | +* [Checkout] |
| 55 | + |
| 56 | +### More Square APIs |
| 57 | + |
| 58 | +* [Catalog] |
| 59 | +* [Customers] |
| 60 | +* [Employees] |
| 61 | +* [Inventory] |
| 62 | +* [Labor] |
| 63 | +* [Locations] |
| 64 | +* [Merchants] |
| 65 | +* [Orders] |
| 66 | +* [Apple Pay] |
| 67 | +* [Refunds] |
| 68 | +* [Reporting] |
| 69 | + |
| 70 | +### Authorization APIs |
| 71 | + |
| 72 | +* [Mobile Authorization] |
| 73 | +* [O Auth] |
| 74 | + |
| 75 | +### Deprecated APIs |
| 76 | + |
| 77 | +* [V1 Locations] |
| 78 | +* [V1 Employees] |
| 79 | +* [V1 Transactions] |
| 80 | +* [V1 Items] |
| 81 | +* [Transactions] |
| 82 | + |
| 83 | +## Usage |
| 84 | + |
| 85 | +First time using Square? Here’s how to get started: |
| 86 | + |
| 87 | +1. **Create a Square account.** If you don’t have one already, [sign up for a developer account]. |
| 88 | +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. |
| 89 | +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. |
| 90 | + |
| 91 | +Now let’s call your first Square API. |
| 92 | + |
| 93 | +```java |
| 94 | +import java.util.List; |
| 95 | +import java.io.IOException; |
| 96 | + |
| 97 | +import com.squareup.square.Environment; |
| 98 | +import com.squareup.square.SquareClient; |
| 99 | +import com.squareup.square.exceptions.ApiException; |
| 100 | +import com.squareup.square.http.client.HttpContext; |
| 101 | +import com.squareup.square.api.LocationsApi; |
| 102 | +import com.squareup.square.models.Location; |
| 103 | +import com.squareup.square.models.Error; |
| 104 | + |
| 105 | +public class Example { |
| 106 | + public static void main(String[] args) { |
| 107 | + SquareClient client = new SquareClient.Builder() |
| 108 | + .environment(Environment.SANDBOX) |
| 109 | + .accessToken("YOUR_SANDBOX_ACCESS_TOKEN") |
| 110 | + .build(); |
| 111 | + |
| 112 | + LocationsApi api = client.getLocationsApi(); |
| 113 | + |
| 114 | + try { |
| 115 | + List<Location> locations = api.listLocations().getLocations(); |
| 116 | + // Your business logic code |
| 117 | + System.out.println("calling listLocations successfully"); |
| 118 | + } catch (ApiException e) { |
| 119 | + List<Error> errors = e.getErrors(); |
| 120 | + int statusCode = e.getResponseCode(); |
| 121 | + HttpContext httpContext = e.getHttpContext(); |
| 122 | + |
| 123 | + // Your error handling code |
| 124 | + System.err.println("ApiException when calling API"); |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + catch (IOException e) { |
| 128 | + // Your error handling code |
| 129 | + System.err.println("IOException when calling API"); |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +Next, get an access token and reference it in your code: |
| 137 | + |
| 138 | +1. Open the Developer Dashboard and select your application. The **Credentials** page for your app opens by default. |
| 139 | +1. Set the dashboard mode to **Sandbox Settings** for a sandbox access token. |
| 140 | +1. Copy the Access Token in the Credentials section of the page and replace `YOUR_SANDBOX_ACCESS_TOKEN` with the token. |
| 141 | + |
| 142 | +**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. |
| 143 | + |
| 144 | +## SDK patterns |
| 145 | +If you know a few patterns, you’ll be able to call any API in the SDK. Here are some important ones: |
| 146 | + |
| 147 | +### Get an access token |
| 148 | + |
| 149 | +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. |
| 150 | +Use an access token that is appropriate for your use case. There are two options: |
| 151 | + |
| 152 | +- To manage the resources for your own Square account, use the **Personal Access Token** for the application created in your Square account. |
| 153 | +- 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. |
| 154 | + |
| 155 | +**Important** For both use cases, make sure you store and access the tokens securely. |
| 156 | + |
| 157 | +### Import and Instantiate the Client Class |
| 158 | + |
| 159 | +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: |
| 160 | + |
| 161 | +- Initialize the `SquareClient` with environment set to sandbox: |
| 162 | + |
| 163 | +```java |
| 164 | +SquareClient client = new SquareClient.Builder() |
| 165 | + .environment(Environment.SANDBOX) |
| 166 | + .accessToken("SANDBOX ACCESS TOKEN HERE") |
| 167 | + .build(); |
| 168 | +``` |
| 169 | + |
| 170 | +- To access production resources, set environment to production: |
| 171 | + |
| 172 | +```java |
| 173 | +SquareClient client = new SquareClient.Builder() |
| 174 | + .environment(Environment.PRODUCTION) |
| 175 | + .accessToken("ACCESS TOKEN HERE") |
| 176 | + .build(); |
| 177 | +``` |
| 178 | + |
| 179 | +### Get an Instance of an API object and call its methods |
| 180 | + |
| 181 | +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: |
| 182 | + |
| 183 | +- 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: |
| 184 | + |
| 185 | +```java |
| 186 | +CustomersApi api = client.getCustomersApi(); |
| 187 | +ListCustomersResponse listCustomersRes = api.listCustomers(null, null, null); |
| 188 | +``` |
| 189 | + |
| 190 | +See the SDK documentation for the list of methods for each API class. |
| 191 | + |
| 192 | +- 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: |
| 193 | + |
| 194 | +```java |
| 195 | +CustomersApi api = client.getCustomersApi(); |
| 196 | + |
| 197 | +Address address = new Address.Builder() |
| 198 | + .addressLine1("1455 Market St") |
| 199 | + .addressLine2("San Francisco, CA 94103") |
| 200 | + .build(); |
| 201 | + |
| 202 | +// Create a unique key(idempotency) for this creation operation so you don't accidentally |
| 203 | +// create the customer multiple times if you need to retry this operation. |
| 204 | +// For the purpose of example, we mark it as `unique_idempotency_key` |
| 205 | +CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest.Builder() |
| 206 | + .idempotencyKey("unique_idempotency_key") |
| 207 | + .givenName("John") |
| 208 | + .familyName("Smith") |
| 209 | + .address(address) |
| 210 | + .build(); |
| 211 | + |
| 212 | +// Call createCustomer method to create a new customer in this Square account |
| 213 | +try { |
| 214 | + CreateCustomerResponse response = api.createCustomer(createCustomerRequest); |
| 215 | +} catch (ApiException e) { |
| 216 | + List<Error> errors = e.getErrors(); |
| 217 | + int statusCode = e.getResponseCode(); |
| 218 | + HttpContext httpContext = e.getHttpContext(); |
| 219 | + |
| 220 | + // Your error handling code |
| 221 | + System.err.println("ApiException when calling API"); |
| 222 | + e.printStackTrace(); |
| 223 | +} |
| 224 | + |
| 225 | +``` |
| 226 | + |
| 227 | +- 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. |
| 228 | +- 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. |
| 229 | + |
| 230 | +### Handle the response |
| 231 | + |
| 232 | +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`: |
| 233 | + |
| 234 | +```java |
| 235 | +try { |
| 236 | + List<Location> locations = api.listLocations().getLocations(); |
| 237 | +} catch (ApiException e) { |
| 238 | + List<Error> errors = e.getErrors(); |
| 239 | + int statusCode = e.getResponseCode(); |
| 240 | + HttpContext httpContext = e.getHttpContext(); |
| 241 | + |
| 242 | + // Your error handling code |
| 243 | + System.err.println("ApiException when calling API"); |
| 244 | + e.printStackTrace(); |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +## Tests |
| 249 | + |
| 250 | +First, clone the repo locally and `cd` into the directory. |
| 251 | + |
| 252 | +```sh |
| 253 | +git clone https://github.com/square/square-java-sdk.git |
| 254 | +cd square-java-sdk |
| 255 | +``` |
| 256 | + |
| 257 | +Before running the tests, find a sandbox token in your [Developer Dashboard] and set a `SQUARE_ACCESS_TOKEN` environment variable. |
| 258 | + |
| 259 | +```sh |
| 260 | +export SQUARE_ENVIRONMENT=sandbox |
| 261 | +export SQUARE_ACCESS_TOKEN="YOUR_SANDBOX_ACCESS_TOKEN" |
| 262 | +``` |
| 263 | + |
| 264 | +If you are using Maven, run the tests with below command |
| 265 | + |
| 266 | +```sh |
| 267 | +mvn test |
| 268 | +``` |
| 269 | + |
| 270 | +## Learn more |
| 271 | + |
| 272 | +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. |
| 273 | + |
| 274 | +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). |
| 275 | + |
| 276 | + |
| 277 | +[//]: # "Link anchor definitions" |
| 278 | +[Square Logo]: https://docs.connect.squareup.com/images/github/github-square-logo.svg |
| 279 | +[Developer Dashboard]: https://developer.squareup.com/apps |
| 280 | +[Square API]: https://squareup.com/developers |
| 281 | +[sign up for a developer account]: https://squareup.com/signup?v=developers |
| 282 | +[Client]: doc/client.md |
| 283 | +[Payments]: doc/payments.md |
| 284 | +[Checkout]: doc/checkout.md |
| 285 | +[Catalog]: doc/catalog.md |
| 286 | +[Customers]: doc/customers.md |
| 287 | +[Employees]: doc/employees.md |
| 288 | +[Inventory]: doc/inventory.md |
| 289 | +[Labor]: doc/labor.md |
| 290 | +[Locations]: doc/locations.md |
| 291 | +[Merchants]: doc/merchants.md |
| 292 | +[Orders]: doc/orders.md |
| 293 | +[Apple Pay]: doc/apple-pay.md |
| 294 | +[Refunds]: doc/refunds.md |
| 295 | +[Reporting]: doc/reporting.md |
| 296 | +[Mobile Authorization]: doc/mobile-authorization.md |
| 297 | +[O Auth]: doc/o-auth.md |
| 298 | +[V1 Locations]: doc/v1-locations.md |
| 299 | +[V1 Employees]: doc/v1-employees.md |
| 300 | +[V1 Transactions]: doc/v1-transactions.md |
| 301 | +[V1 Items]: doc/v1-items.md |
| 302 | +[Transactions]: doc/transactions.md |
0 commit comments