Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class SquareTerminal

  1. Object
  2. PaymentTerminal
  3. SquareTerminal

A Square Terminal, driven through Square’s Terminal API.

Each sale creates a terminal checkout for the paired device and polls it until the customer has paid or it is canceled. It is plain HTTPS and JSON, so it works on every platform with no native SDK.

Credentials

Square’s API takes an access token, which must not ship in an app a customer can install. On a till you control, use a token limited to the PAYMENTS_WRITE and PAYMENTS_READ permissions. Otherwise point setApiBase(String) at your own server, which adds the token and forwards the same paths to Square.

String accessToken = null; // null: your server adds the token
SquareTerminal terminal = new SquareTerminal("121CS145A5000029", accessToken);
terminal.collect(new TerminalPaymentRequest(1250, "USD").setReference("order-7"))
        .ready(result -> System.out.println(result));

What it does

  • Sales, with tipping on the device when the request asks for it.
  • Pre-authorizations (the checkout does not complete the payment), then capture(String, long, String) completes it – at the authorized amount, which Square cannot lower on completion.
  • reverse(String) cancels a payment that is not completed yet.
  • refund(TerminalRefundRequest) refunds a payment by its ID; Square needs no card for it, and refunds no card that is not linked to one.
  • lookup(String) reads a payment by its ID.

Every request that moves money carries an idempotency key and is repeated with the same key when its answer is lost, so a retry can never charge twice. Starts against Square’s sandbox; call setLive(boolean) for production.

Constructors

public SquareTerminal(String deviceId, String accessToken)

Methods

public void setLive(boolean live)Switches between Square’s sandbox (the default) and production.
public void setApiBase(String apiBase)Sends requests to another base URL, such as a server of yours that adds the access token.
public void setTransport(TerminalTransport transport)Replaces the HTTP transport.
public void setPollInterval(int pollInterval)How often the checkout is polled, in milliseconds.
public void setMaxWait(long maxWait)How long the device waits for the customer before the checkout times out.
public boolean supports(TerminalOperation operation)Whether this terminal can perform operation.
public AsyncResource<TerminalResult> collect(TerminalPaymentRequest request)Takes a payment: a sale, or a pre-authorization when the request says so.
public void cancel()Asks the terminal to abandon the operation in progress.
public AsyncResource<TerminalResult> capture(String transactionId, long amount, String currency)Takes money a pre-authorization reserved.
public AsyncResource<TerminalResult> reverse(String transactionId)Cancels a payment that has not settled yet, so it never reaches the customer’s statement.
public AsyncResource<TerminalResult> refund(TerminalRefundRequest request)Returns money to a card.
public AsyncResource<TerminalResult> lookup(String reference)Finds out what became of a payment – the way to settle a TerminalOutcomeUnknownException.
protected void schedule(int delay, Runnable task)Runs task on the EDT after delay milliseconds.

Inherited methods

Constructor details

SquareTerminal

public SquareTerminal(String deviceId, String accessToken)

Parameters

deviceId String
the paired Square Terminal’s device ID
accessToken String
sent as a bearer token; may be null when your own server adds it

Method details

setLive

public void setLive(boolean live)
Switches between Square’s sandbox (the default) and production.

Parameters

live boolean
true for production

setApiBase

public void setApiBase(String apiBase)
Sends requests to another base URL, such as a server of yours that adds the access token.

Parameters

apiBase String
the base URL, with no trailing slash

setTransport

public void setTransport(TerminalTransport transport)
Replaces the HTTP transport.

Parameters

transport TerminalTransport
the transport

setPollInterval

public void setPollInterval(int pollInterval)
How often the checkout is polled, in milliseconds. Defaults to one second.

Parameters

pollInterval int
the interval in milliseconds

setMaxWait

public void setMaxWait(long maxWait)
How long the device waits for the customer before the checkout times out. Defaults to five minutes; Square accepts one to five.

Parameters

maxWait long
the limit in milliseconds

supports

public boolean supports(TerminalOperation operation)
Whether this terminal can perform operation.

Parameters

operation TerminalOperation
the operation to ask about

Returns

true when collect(TerminalPaymentRequest) or the matching method can carry it out

collect

public AsyncResource<TerminalResult> collect(TerminalPaymentRequest request)
Takes a payment: a sale, or a pre-authorization when the request says so.

Parameters

request TerminalPaymentRequest
what to charge

Returns

the outcome, delivered on the EDT

cancel

public void cancel()

Asks the terminal to abandon the operation in progress.

Best effort: a card that was already read may still be approved, so the outcome is whatever the operation delivers, TerminalResult.Status.CANCELED when the cancel won. Does nothing when nothing is in progress.

capture

public AsyncResource<TerminalResult> capture(String transactionId, long amount, String currency)
Takes money a pre-authorization reserved. Needs TerminalOperation.CAPTURE.

Parameters

transactionId String
the pre-authorization’s TerminalResult.getTransactionId()
amount long
the amount to take, in the minor unit; at most the reserved amount on most acquirers
currency String
the ISO 4217 code

Returns

the outcome, delivered on the EDT

reverse

public AsyncResource<TerminalResult> reverse(String transactionId)
Cancels a payment that has not settled yet, so it never reaches the customer’s statement. Once settled, use refund(TerminalRefundRequest). Needs TerminalOperation.REVERSAL.

Parameters

transactionId String
the payment’s TerminalResult.getTransactionId()

Returns

the outcome, delivered on the EDT

refund

public AsyncResource<TerminalResult> refund(TerminalRefundRequest request)
Returns money to a card. Needs TerminalOperation.REFUND, or TerminalOperation.UNLINKED_REFUND when the request names no payment.

Parameters

request TerminalRefundRequest
what to return, and to which payment

Returns

the outcome, delivered on the EDT

lookup

public AsyncResource<TerminalResult> lookup(String reference)
Finds out what became of a payment – the way to settle a TerminalOutcomeUnknownException. Needs TerminalOperation.LOOKUP.

Parameters

reference String
the payment’s TerminalPaymentRequest.getReference(), or its transaction ID where the terminal accepts one

Returns

the payment’s result as the terminal or provider recorded it; completes with an error when it has no such payment

schedule

protected void schedule(int delay, Runnable task)
Runs task on the EDT after delay milliseconds. Overridable so a subclass can drive the polling from its own clock.

Parameters

delay int
the delay in milliseconds
task Runnable
what to run