Point of Sale API

Build on Android

Open the Square Point of Sale app from your mobile Android application to process in-person payments using Square hardware.

Android
UI

Prerequisites and assumptions
Permalink Get a link to this section

This guide makes the following assumptions:

  • You have read the What It Does page for this product.

The example code in this guide assumes the following:

  • You are building an Android app. For guidance on building an iOS app, check our Build on iOS Guide. For guidance on building a Mobile Web app, check our Build on Mobile Web Guide.

  • You are using the Point of Sale SDK for Android version 2.0 and the Square Point of Sale v4.64 and above. The Point of Sale SDK provides a helpful wrapper around the Point of Sale API, which simplifies the development process.

  • You are using 15 as the minimum supported Android SDK version. If you are using an earlier version, you may need to update your SDK to use the sample code in this guide.

  • You are using Java. The Point of Sale SDK only supports Java on Android.

Information that you need
Permalink Get a link to this section

To use the steps in this guide you will need the following information:

  • A valid access token. Square recommends testing with sandbox credentials whenever possible. See Square API Access Tokens for more information.

Step 1: Install the Square Point of Sale app
Permalink Get a link to this section

We recommend installing the latest version of the Square Point of Sale app from the Google Play Store.

If you choose to use an older version of the Point of Sale application, you will need to confirm the version is compatible with the Android Point of Sale SDK version you are using:

App VersionAndroid SDK Version
4.64 and laterv2.0
4.48 and laterv1.
4.41 and laterv1.0

There are 2 ways to determine the current version of the Square Point of Sale app:

adb shell dumpsys package com.squareup | grep versionName

Step 2: Register your application
Permalink Get a link to this section

The Square Point of Sale app for Android will only accept requests if it can authenticate the source of the request. Square uses the following information to authenticate application requests:

  • SHA-1 fingerprint — The Square Point of Sale app uses the SHA-1 fingerprint of an app to validate the source of incoming requests.

  • Package name (such as com.example.myapp). A Java-language-style package name for your Android app. This needs to match the name you give in the package attribute in the Android manifest for your application.

To register your application:

  1. Obtain your Android app fingerprint.

  2. Go to the Point of Sale API settings page for your application in the Application Dashboard.

  3. Under the Android section, click "Add a new Android Package".

  4. Enter your package name and fingerprint.

  5. Click "Save".

Step 3: Add the Square Point of Sale SDK to your project
Permalink Get a link to this section

Install the Point of Sale SDK manually
Permalink Get a link to this section

To install the Point of Sale SDK manually, download the point-of-sale-android-sdk repo on Github.

Add the Point of Sale SDK to a Gradle project
Permalink Get a link to this section

If you develop with Gradle, add the dependency noted below that is appropriate for your plugin to your build.gradle file and rebuild your project.

Android Plugin for Gradle 3.0.0+
Permalink Get a link to this section

dependencies {
  implementation 'com.squareup.sdk:point-of-sale-sdk:2.+'
}

Android Plugin for Gradle 2.+
Permalink Get a link to this section

dependencies {
  compile 'com.squareup.sdk:point-of-sale-sdk:2.+'
}

Add the Point of Sale SDK to a Maven project
Permalink Get a link to this section

If you develop with Maven, add the following dependency to your pom.xml file and rebuild your project:

<dependency>
  <groupId>com.squareup.sdk</groupId>
  <artifactId>point-of-sale-sdk</artifactId>
  <version>2.0</version>
  <type>aar</type>
</dependency>

Step 4: Add code to initiate a transaction from your app
Permalink Get a link to this section

  1. Set your application ID. You can find your Application ID by opening your Application Dashboard, then clicking on an application to open the control panel.

  2. Add a constructor that configures an instance of PosClient.

  3. Add code that creates a new charge request and uses it to initiate a transaction in the Point of Sale app. The function below uses ChargeRequest.Builder to create a charge for 1 USD, then uses that request to create a ChargeRequest and initiate the transaction with an Intent object.


Set your application ID

   private static final String APPLICATION_ID = "{REPLACE ME}";

Add a constructor

    public class MainActivity extends Activity {

      private PosClient posClient;

      @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        // Replace APPLICATION_ID with a Square-assigned application ID
        posClient = PosSdk.createClient(this, APPLICATION_ID);
        }
    }

Create a new charge request

// create a new charge request and initiate a Point of Sale transaction
    private static final int CHARGE_REQUEST_CODE = 1;
    public void startTransaction() {
      ChargeRequest request = new ChargeRequest.Builder(
      100,
      CurrencyCode.USD)
      .build();
      try {
        Intent intent = posClient.createChargeIntent(request);
        startActivityForResult(intent, CHARGE_REQUEST_CODE);

      } catch (ActivityNotFoundException e) {
        AlertDialogHelper.showDialog(
            this,
            "Error",
            "Square Point of Sale is not installed"
            );
        posClient.openPointOfSalePlayStoreListing();
      }
    }

The AlertDialogHelper object that is used in Step 4 and Step 5 code blocks is an example of a helper class instance and not part of the Point of Sale SDK.

Step 5: Add code to receive data from the Square Point of Sale app
Permalink Get a link to this section

When the Square Point of Sale app completes a transaction (or encounters an error), it returns UI focus to your app and calls the onActivityResult() method of the activity that initiated the transaction. Add code to parse the response.

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // Handle unexpected errors
    if (data == null || requestCode != CHARGE_REQUEST_CODE) {
        AlertDialogHelper.showDialog(this,
            "Error: unknown",
            "Square Point of Sale was uninstalled or stopped working");
      return;
    }

    // Handle expected results
    if (resultCode == Activity.RESULT_OK) {
      // Handle success
      ChargeRequest.Success success = posClient.parseChargeSuccess(data);
      AlertDialogHelper.showDialog(this,
          "Success",
          "Client transaction ID: "
              + success.clientTransactionId);
    } else {
        // Handle expected errors
        ChargeRequest.Error error = posClient.parseChargeError(data);
        AlertDialogHelper.showDialog(this,
            "Error" + error.code,
            "Client transaction ID: "
                + error.debugDescription);
    }
    return;
  }

See the Android Point of Sale API Technical Reference for more information about the ChargeRequest class.

Did you know?

Your application should keep track of whether a Point of Sale API request has been issued and whether or not a callback has been received. Because of the constraints of app-switching APIs, merchants may leave Square Point of Sale during an API request and return to your app, and it is your responsibility to direct them to return to Square and finish the transaction.

Step 6: Test your code
Permalink Get a link to this section

  1. Log into the Square Point of Sale app with the business location you want to use to accept payments.

  2. Open your app and initiate a transaction.

  3. Test your callbacks:

    1. Test the cancellation callback: cancel the transaction.

    2. Test the success callback: complete a cash transaction.

v2 Sandbox (beta) does not support credit card testing for mobile. See the Testing Mobile Apps guide for alternatives.

Next steps

Now that you have a basic build in place, expand on it with these recipes!