sorianog / square-java-sdk

Java client library for the Square API

Home Page:https://developer.squareup.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Square logo

Square Java SDK

Travis status Maven Central Apache-2 license

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.

Requirements

Use of the Square Java SDK requires:

  • Java 8 or better
  • Maven or Gradle to build and install the SDK.

Installation

Install with Maven

Install the API client library to your local Maven repository:

mvn install -DskipTests

OR

Install the client dynamically by adding a dependency to the POM for your project:

<dependency>
    <groupId>com.squareup</groupId>
    <artifactId>square</artifactId>
    <version>18.0.0.20211215</version>
</dependency>

Install with Gradle

Install the client by adding the following dependency to the build file for your project:

implementation "com.squareup:square:18.0.0.20211215"

API documentation

Payments

Terminal

Orders

Subscriptions

Invoices

Items

Customers

Loyalty

Gift Cards

Bookings

Business

Team

Financials

Online

Authorization APIs

Deprecated APIs

Usage Notes for V1 Transactions

The Square API supersedes the legacy Connect V1 APIs. Square strongly discourages using Connect V1 for most use cases. However, you must still use Connect V1 for listing settlements and listing payments. For more information, see When to Use Connect V1.

In the Square Java SDK, the V1TransactionsApi class provides ListSettlements and ListPayments for accessing these Connect V1 endpoints. However, Square has identified an issue that prevents these methods from returning results. There is no workaround for this issue.

If you need to use List settlements or List payments, you should send HTTP GET requests directly to these Connect V1 endpoints. The response body consists of a JSON list of objects, which you can process as needed.

You'll also need to provide logic to handle paginated results. For more information, see Pagination in Connect V1.

Usage

First time using Square? Here’s how to get started:

  1. Create a Square account. If you don’t have one already, sign up for a developer account.
  2. 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.
  3. 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.

Now let’s call your first Square API.

import java.util.List;
import java.io.IOException;

import com.squareup.square.Environment;
import com.squareup.square.SquareClient;
import com.squareup.square.exceptions.ApiException;
import com.squareup.square.http.client.HttpContext;
import com.squareup.square.api.LocationsApi;
import com.squareup.square.models.Location;
import com.squareup.square.models.Error;

public class Example {
    public static void main(String[] args)  {
        SquareClient client = new SquareClient.Builder()
            .environment(Environment.SANDBOX)
            .accessToken("YOUR_SANDBOX_ACCESS_TOKEN")
            .build();

        LocationsApi api = client.getLocationsApi();

        try {
            List<Location> locations = api.listLocations().getLocations();
            // Your business logic code
            System.out.println("calling listLocations successfully");
        } catch (ApiException e) {
            List<Error> errors = e.getErrors();
            int statusCode = e.getResponseCode();
            HttpContext httpContext = e.getHttpContext();

            // Your error handling code
            System.err.println("ApiException when calling API");
            e.printStackTrace();
        }
        catch (IOException e) {
            // Your error handling code
            System.err.println("IOException when calling API");
            e.printStackTrace();
        }
    }
}

Next, get an access token and reference it in your code:

  1. Open the Developer Dashboard and select your application. The Credentials page for your app opens by default.
  2. Set the dashboard mode to Sandbox Settings for a sandbox access token.
  3. Copy the Access Token in the Credentials section of the page and replace YOUR_SANDBOX_ACCESS_TOKEN with the token.

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.

SDK patterns

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

Get an access token

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. Use an access token that is appropriate for your use case. There are two options:

  • To manage the resources for your own Square account, use the Personal Access Token for the application created in your Square account.
  • 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.

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

Import and Instantiate the Client Class

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:

  • Initialize the SquareClient with environment set to sandbox:
SquareClient client = new SquareClient.Builder()
    .environment(Environment.SANDBOX)
    .accessToken("SANDBOX ACCESS TOKEN HERE")
    .build();
  • To access production resources, set environment to production:
SquareClient client = new SquareClient.Builder()
    .environment(Environment.PRODUCTION)
    .accessToken("ACCESS TOKEN HERE")
    .build();
  • To set a custom environment provide a customUrl, and set the environment to Environment.CUSTOM:
SquareClient client = new SquareClient.Builder()
    .environment(Environment.CUSTOM)
    .customUrl("https://your.customdomain.com")
    .accessToken("ACCESS TOKEN HERE")
    .build();

Get an Instance of an API object and call its methods

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:

  • 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:
CustomersApi api = client.getCustomersApi();
ListCustomersResponse listCustomersRes = api.listCustomers(null, null, null);

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

  • 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:
CustomersApi api = client.getCustomersApi();

Address address = new Address.Builder()
    .addressLine1("1455 Market St")
    .addressLine2("San Francisco, CA 94103")
    .build();

// Create a unique key(idempotency) for this creation operation so you don't accidentally
// create the customer multiple times if you need to retry this operation.
// For the purpose of example, we mark it as `unique_idempotency_key`
CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest.Builder()
    .idempotencyKey("unique_idempotency_key")
    .givenName("John")
    .familyName("Smith")
    .address(address)
    .build();

// Call createCustomer method to create a new customer in this Square account
try {
    CreateCustomerResponse response = api.createCustomer(createCustomerRequest);
} catch (ApiException e) {
    List<Error> errors = e.getErrors();
    int statusCode = e.getResponseCode();
    HttpContext httpContext = e.getHttpContext();

    // Your error handling code
    System.err.println("ApiException when calling API");
    e.printStackTrace();
}
  • 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.
  • 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.

Handle the response

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:

try {
    List<Location> locations = api.listLocations().getLocations();
} catch (ApiException e) {
    List<Error> errors = e.getErrors();
    int statusCode = e.getResponseCode();
    HttpContext httpContext = e.getHttpContext();

    // Your error handling code
    System.err.println("ApiException when calling API");
    e.printStackTrace();
}

Tests

First, clone the repo locally and cd into the directory.

git clone https://github.com/square/square-java-sdk.git
cd square-java-sdk

Before running the tests, find a sandbox token in your Developer Dashboard and set a SQUARE_ACCESS_TOKEN environment variable.

export SQUARE_ENVIRONMENT=sandbox
export SQUARE_ACCESS_TOKEN="YOUR_SANDBOX_ACCESS_TOKEN"

If you are using Maven, run the tests with below command

mvn test

Learn more

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.

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).

About

Java client library for the Square API

https://developer.squareup.com

License:Other


Languages

Language:Java 100.0%