Class 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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Asks the terminal to abandon the operation in progress.Takes money a pre-authorization reserved.collect(TerminalPaymentRequest request) Takes a payment: a sale, or a pre-authorization when the request says so.Finds out what became of a payment -- the way to settle aTerminalOutcomeUnknownException.refund(TerminalRefundRequest request) Returns money to a card.Cancels a payment that has not settled yet, so it never reaches the customer's statement.protected voidRunstaskon the EDT afterdelaymilliseconds.voidsetApiBase(String apiBase) Sends requests to another base URL, such as a server of yours that adds the access token.voidsetLive(boolean live) Switches between Square's sandbox (the default) and production.voidsetMaxWait(long maxWait) How long the device waits for the customer before the checkout times out.voidsetPollInterval(int pollInterval) How often the checkout is polled, in milliseconds.voidsetTransport(TerminalTransport transport) Replaces the HTTP transport.booleansupports(TerminalOperation operation) Whether this terminal can performoperation.Methods inherited from class PaymentTerminal
failed, missingCapability, reconcile, unsupported
-
Constructor Details
-
SquareTerminal
-
-
Method Details
-
setLive
public void setLive(boolean live) Switches between Square's sandbox (the default) and production.
Parameters
live: true for production
-
setApiBase
Sends requests to another base URL, such as a server of yours that adds the access token.
Parameters
apiBase: the base URL, with no trailing slash
-
setTransport
Replaces the HTTP transport.
Parameters
transport: the transport
-
setPollInterval
public void setPollInterval(int pollInterval) How often the checkout is polled, in milliseconds. Defaults to one second.
Parameters
pollInterval: 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: the limit in milliseconds
-
supports
Description copied from class:PaymentTerminalWhether this terminal can perform
operation.Parameters
operation: the operation to ask about
Returns
true when
PaymentTerminal.collect(TerminalPaymentRequest)or the matching method can carry it out- Specified by:
supportsin classPaymentTerminal
-
collect
Description copied from class:PaymentTerminalTakes a payment: a sale, or a pre-authorization when the request says so.
Parameters
request: what to charge
Returns
the outcome, delivered on the EDT
- Specified by:
collectin classPaymentTerminal
-
cancel
public void cancel()Description copied from class:PaymentTerminalAsks 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.CANCELEDwhen the cancel won. Does nothing when nothing is in progress.- Specified by:
cancelin classPaymentTerminal
-
capture
Description copied from class:PaymentTerminalTakes money a pre-authorization reserved. Needs
TerminalOperation.CAPTURE.Parameters
-
transactionId: the pre-authorization'sTerminalResult.getTransactionId() -
amount: the amount to take, in the minor unit; at most the reserved amount on most acquirers -
currency: the ISO 4217 code
Returns
the outcome, delivered on the EDT
- Overrides:
capturein classPaymentTerminal
-
-
reverse
Description copied from class:PaymentTerminalCancels a payment that has not settled yet, so it never reaches the customer's statement. Once settled, use
PaymentTerminal.refund(TerminalRefundRequest). NeedsTerminalOperation.REVERSAL.Parameters
transactionId: the payment'sTerminalResult.getTransactionId()
Returns
the outcome, delivered on the EDT
- Overrides:
reversein classPaymentTerminal
-
refund
Description copied from class:PaymentTerminalReturns money to a card. Needs
TerminalOperation.REFUND, orTerminalOperation.UNLINKED_REFUNDwhen the request names no payment.Parameters
request: what to return, and to which payment
Returns
the outcome, delivered on the EDT
- Overrides:
refundin classPaymentTerminal
-
lookup
Description copied from class:PaymentTerminalFinds out what became of a payment -- the way to settle a
TerminalOutcomeUnknownException. NeedsTerminalOperation.LOOKUP.Parameters
reference: the payment'sTerminalPaymentRequest.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
- Overrides:
lookupin classPaymentTerminal
-
schedule
Runs
taskon the EDT afterdelaymilliseconds. Overridable so a subclass can drive the polling from its own clock.Parameters
-
delay: the delay in milliseconds -
task: what to run
-
-