Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class SquareTerminal
- Object
- PaymentTerminal
- 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
Inherited methods
Constructor details
SquareTerminal
public SquareTerminal(String deviceId, String accessToken)Parameters
deviceIdString- the paired Square Terminal’s device ID
accessTokenString- sent as a bearer token; may be null when your own server adds it
Method details
setLive
public void setLive(boolean live)Parameters
liveboolean- true for production
setApiBase
public void setApiBase(String apiBase)Parameters
apiBaseString- the base URL, with no trailing slash
setTransport
public void setTransport(TerminalTransport transport)Parameters
transportTerminalTransport- the transport
setPollInterval
public void setPollInterval(int pollInterval)Parameters
pollIntervalint- the interval in milliseconds
setMaxWait
public void setMaxWait(long maxWait)Parameters
maxWaitlong- the limit in milliseconds
supports
public boolean supports(TerminalOperation operation)operation.Parameters
operationTerminalOperation- the operation to ask about
Returns
collect(TerminalPaymentRequest) or the matching method
can carry it outcollect
public AsyncResource<TerminalResult> collect(TerminalPaymentRequest request)Parameters
requestTerminalPaymentRequest- what to charge
Returns
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)TerminalOperation.CAPTURE.Parameters
transactionIdString- the pre-authorization’s
TerminalResult.getTransactionId() amountlong- the amount to take, in the minor unit; at most the reserved amount on most acquirers
currencyString- the ISO 4217 code
Returns
reverse
public AsyncResource<TerminalResult> reverse(String transactionId)refund(TerminalRefundRequest). Needs TerminalOperation.REVERSAL.Parameters
transactionIdString- the payment’s
TerminalResult.getTransactionId()
Returns
refund
public AsyncResource<TerminalResult> refund(TerminalRefundRequest request)TerminalOperation.REFUND, or
TerminalOperation.UNLINKED_REFUND when the request names no
payment.Parameters
requestTerminalRefundRequest- what to return, and to which payment
Returns
lookup
public AsyncResource<TerminalResult> lookup(String reference)TerminalOutcomeUnknownException. Needs TerminalOperation.LOOKUP.Parameters
referenceString- the payment’s
TerminalPaymentRequest.getReference(), or its transaction ID where the terminal accepts one
Returns
schedule
protected void schedule(int delay, Runnable task)task on the EDT after delay milliseconds. Overridable so a
subclass can drive the polling from its own clock.Parameters
delayint- the delay in milliseconds
taskRunnable- what to run