Applies to: Point of Sale API - Android
Learn how to open the Square Point of Sale application from your mobile Android application to process in-person payments using Square hardware.
Requirements and limitations
- You've read the introductory review of this API.
- You need a valid access token. You should test with Sandbox credentials whenever possible. For more information, see Access Tokens and Other Square Credentials.
- You're building an Android application - For information about building an iOS application, see Build on iOS. For information about building a Mobile Web application, see Build on Mobile Web.
- You're using the Point of Sale SDK for Android version 2.0 and the Square Point of Sale application v4.64 and later - The Point of Sale SDK provides a helpful wrapper around the Point of Sale API, which simplifies the development process.
- You're using version 15 as the minimum supported Android SDK version - If you're using an earlier version, you might need to update your SDK to use the sample code in this topic.
- You're using Java - The Point of Sale SDK only supports Java on Android.
1. Install the Square Point of Sale application
You should install the latest version of the Square Point of Sale application from the Google Play Store.
If you choose to use an older version of the Point of Sale application, you need to confirm that the version is compatible with the Android Point of Sale SDK version you're using.
Application version | Android SDK version |
---|
4.64 and later | v2.0 |
4.48 and later | v1. |
4.41 and later | v1.0 |
There are two ways to determine the current version of the Square Point of Sale application:
2. Register your application
The Square Point of Sale application for Android only accepts 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 application uses the SHA-1 fingerprint of an application to validate the source of incoming requests.
- Package name (such as com.example.myapp) - A Java-language-style package name for your Android application. This needs to match the name you use in the
package
attribute in the Android manifest for your application.
To register your application:
- Obtain your Android application fingerprint.
- Open the Developer Console.
- In the left pane, choose Point of Sale API.
- In the Android section, choose Add a new Android Package.
- Enter your package name and fingerprint.
- Choose Save.
3. Add the Square Point of Sale SDK to your project
Install the Point of Sale SDK manually
To install the Point of Sale SDK manually, download the point-of-sale-android-sdk repo in GitHub.
Add the Point of Sale SDK to a Gradle project
If you develop with Gradle, add the following dependency that is appropriate for your plugin to your build.gradle file and rebuild your project.
Android plugin for Gradle 3.0.0 and later
dependencies {
implementation 'com.squareup.sdk:point-of-sale-sdk:2.+'
}
Android plugin for Gradle 2 and later
dependencies {
compile 'com.squareup.sdk:point-of-sale-sdk:2.+'
}
Add the Point of Sale SDK to a Maven project
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>
4. Update your project AndroidManifest
If your project targets Android 11 or newer, you must add the following object to your manifest:
<queries>
<package android:name="com.squareup" />
</queries>
5. Add code to initiate a transaction from your application
- Set your application ID. To find your application ID, open the Developer Console, and choose an application.
- Add a constructor that configures an instance of PosClient.
- Add code that creates a new charge request and uses it to initiate a transaction in the Point of Sale application. The following function uses ChargeRequest.Builder to create a charge for $1 USD and then uses that request to create a
ChargeRequest
and initiate the transaction with an Intent
object.
Set your application ID as shown:
private static final String APPLICATION_ID = "{REPLACE ME}";
Add a constructor as shown:
public class MainActivity extends Activity {
private PosClient posClient;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
posClient = PosSdk.createClient(this, APPLICATION_ID);
}
}
Create a new charge request as shown:
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();
}
}
Note
The AlertDialogHelper
object that is used in the step 4 and step 5 code blocks is an example of a helper class instance and not part of the Point of Sale SDK.
6. Add code to receive data from the Square Point of Sale application
When the Square Point of Sale application completes a transaction (or encounters an error), it returns the UI focus to your application 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) {
if (data == null || requestCode != CHARGE_REQUEST_CODE) {
AlertDialogHelper.showDialog(this,
"Error: unknown",
"Square Point of Sale was uninstalled or stopped working");
return;
}
if (resultCode == Activity.RESULT_OK) {
ChargeRequest.Success success = posClient.parseChargeSuccess(data);
AlertDialogHelper.showDialog(this,
"Success",
"Client transaction ID: "
+ success.clientTransactionId);
} else {
ChargeRequest.Error error = posClient.parseChargeError(data);
AlertDialogHelper.showDialog(this,
"Error" + error.code,
"Client transaction ID: "
+ error.debugDescription);
}
return;
}
For more information about the ChargeRequest
class, see the Android Point of Sale API Technical Reference.
Did you know?
Your application should keep track of whether a Point of Sale API request has been issued and whether a callback has been received. Because of the constraints of application-switching APIs, sellers can leave Square Point of Sale during an API request and return to your application. It's your responsibility to direct them to return to Square and finish the transaction.
- Sign in to the Square Point of Sale application with the business location that you want to use to accept payments.
- Open your application and initiate a transaction.
- Test your callbacks:
- Test the cancellation callback by canceling a transaction.
- Test the success callback by completing a cash transaction.
The Sandbox doesn't support credit card testing for mobile. For alternatives, see Testing for supported countries.