Build on Android

Link to section

Overview

The Mobile Payments SDK offers developers a secure way to integrate the Square checkout flow into their mobile applications and accept in-person payments with Square Readers.

There are four steps required to take your first payment using the Mobile Payments SDK:

  1. Install and configure the Mobile Payments SDK in your Android application
  2. Authorize the Mobile Payments SDK
  3. Pair a card Reader
  4. Take a payment
Link to section

Requirements and limitations

  • You have a Square account enabled for payment processing. If you haven't enabled payment processing on your account (or you're not sure), visit squareup.com/activation.

  • You have a Square Reader device to take payments.

  • Your application's minSdkVersion is API 24 (Android 7) or later.

  • Your application's targetSdkVersion is API 33 (Android 13) or earlier.

  • Your application's compileSdkVersion is 34.

  • Your application uses AndroidX or enables Jetifier in gradle.properties.

  • You're using the Android Gradle Plugin version 8.1.0 or later. The Mobile Payments SDK might work with 7.1.0 or later, but stability isn't guaranteed.

  • You're not using Proguard for code optimization. Compressing the Mobile Payments SDK binary removes critical bytecode elements and results in runtime errors.

  • Your version of the Mobile Payments SDK adheres to the Square update policy. To limit risk to developers and their users, Square enforces an update policy requiring developers keep their version of the Mobile Payments SDK current.

  • You're generally familiar with developing applications on Android. If you're new to Android development, you should read the Documentation on the Android Developers site before continuing.

Link to section

Device permissions

To take payments with Square Readers, your application must request the following device permissions.

Android device permissionPurpose
ACCESS_FINE_LOCATIONTo confirm that payments are occurring in a supported Square location.
RECORD_AUDIOTo receive data from magstripe readers.
BLUETOOTH_CONNECTTo receive data from contactless and chip readers.
BLUETOOTH_SCANTo store information during checkout.
READ_PHONE_STATETo identify the device sending information to Square servers.
Link to section

Device compatibility

The Mobile Payments SDK isn't compatible with custom devices from OEMs that violate Square's security rules or devices that grant root access to the mobile device operating system code and kernel. These devices cannot connect to a contactless and chip reader and don't comply with Square's security requirements.

The following additional considerations apply when trying to run the SDK on custom devices:

  • The Mobile Payments SDK isn't supported for rooted or jailbroken devices.
  • Square prohibits using any software that provides the ability to record or capture screen data or that results in elevated privileges.
  • Square recommends running the Mobile Payments SDK on devices from large mobile device manufacturers such as Google, Samsung, or HTC.
  • If your mobile application runs on an uncommon device manufacturer, be sure to test the device with any expected software requirements, including Bluetooth connectivity, before investing in the device.
  • Supported OS versions should be the latest or as close to the latest as possible.
  • Review the Square support window for the OS version and confirm support for the compilation target version and the runtime version. The compilation target currently supports version 34. For runtime, the supported versions are 24 to 34.
Link to section

Quickstart

Get started with the Mobile Payments SDK by configuring your Android application and taking a test payment in the Square Sandbox.

Link to section

1. Install the SDK and dependencies

Add the Square Maven repo and the following dependencies to your project's build.gradle file. The Mock Reader UI is used to simulate virtual reader devices.

Link to section

2. Initialize the Mobile Payments SDK

Before taking a payment or attempting any other operation with the Mobile Payments SDK, you must initialize it using the initialize method and your Square developer application ID. To get your application ID, sign in to the Developer Dashboard and choose your application. If you don't have an application, see Get Started for steps to create a Square application.

Your application opens in the Developer Dashboard to the Credentials page. This page contains your Square application ID used to initialize the Mobile Payments SDK.

Note

To test the Mobile Payments SDK in the Square Sandbox with mock readers and mock payments, switch the toggle at the top of the Developer Dashboard to Sandbox to show your Sandbox credentials and initialize the SDK with your Sandbox application ID. To start taking production payments, you must initialize the Mobile Payments SDK again with your production application ID.

class DemoApplication : Application() { override fun onCreate() { super.onCreate() val appId = OAuthHelper.getAppId(this) MobilePaymentsSdk.initialize(appId, this) } }
Link to section

3. Authorize the Mobile Payments SDK

The AuthorizationManager handles authorization for the Mobile Payments SDK. Call the authorize() method with an OAuth access token and location ID. If you're testing your application in the Square Sandbox, you can use your Sandbox personal access token and location ID.

Important

Don't deploy an application that contains your personal access token into production. For secure authorization in production, you should implement OAuth instead of using a personal access token.

In your call to authorize(), configure a callback to be notified of authorization errors and display those to the user.

Link to section

4. Test with mock readers in the Square Sandbox

Physical card readers aren't supported in the Square Sandbox. To take test payments, you must simulate a virtual reader with the Mock Reader UI.

The Mock Reader UI consists of a floating action button (FAB) that automatically attaches itself to the currently displayed Activity as you navigate between application screens when you call MockReaderUI.show(). The icon can be dragged anywhere on the screen and persists throughout your application lifecycle.

Click this button to simulate connecting a magstripe reader or a contactless and chip reader to your device. You can use the ReaderManager to programmatically access information about these simulated readers, such as their model and available card entry methods.

When mock readers have been connected and you begin a payment, click the FAB again to test your application's payment flow and simulate a swiped, inserted, or tapped card with the mock reader. Click Payment details to select different card brands and payment behaviors (approved or declined) for testing purposes. To hide the FAB in your application, call MockReaderUI.hide().

To pair physical readers for production payments, see Pair and Manage Card Readers.

Link to section

5. Set up a payment

The PaymentManager handles payments in the Mobile Payments SDK. Prior to starting a payment, you create a set of PaymentParameters to represent details of an individual payment and a set of PromptParameters to indicate how the payment prompts are presented to the buyer.

When setting up the payment, use the setAvailableCardEntryMethodChangedCallback to notify your application whether the available card entry methods have changed (for example, if a magstripe reader was inserted or removed from the mobile device). You should include this callback in either a ViewModel or an Activity’s onCreate() or onResume() method.

Link to section

Payment parameters

Before beginning a payment, create a PaymentParameters object, which includes the payment amount, idempotency key, optional tip, application fee (if applicable), and the processing mode used if you want to take payments while offline. For the complete list of payment parameter values and details, see the Android technical reference.

Link to section

Prompt parameters

Each payment also requires a set of PromptParameters, consisting of a mode and a set of additionalPaymentMethods that buyers can use for a payment. At this time, KEYED (a manually entered credit card payment) is the only additional payment method available.

Square provides a payment prompt screen that displays a UI to buyers prompting them to swipe, insert, or tap their card, depending on the payment options available. To use this screen, set PromptParameters.mode to DEFAULT. If you want to create your own custom payment prompt screen instead of using the default, set PromptParameters.mode to CUSTOM.

A graphic showing the Square-provided payment prompt screen for the Mobile Payments SDK. The screen shows a 24 dollar payment and prompts the buyer to swipe, tap, or insert in card reader to pay.

Link to section

6. Take a payment

When you're ready to begin a payment, call PaymentManager.startPaymentActivity() with the PaymentParameters and PromptParameters you created for this payment, along with a callback to notify you of the payment's success or failure.

During a payment, Square takes control of the screen display as another Android Activity to ensure that buyer information is handled securely and that the final confirmation of the payment is correctly shown to the buyer.

A graphic showing two payment screens in the Mobile Payments SDK, showing a 24 dollar payment going through "Processing" and "Approved" status

If the payment completes successfully, a Payment object is returned to your payment callback. This payment can be accessed on the Seller Dashboard or with the Payments API. If the payment fails, the response contains a PaymentError.

Link to section

Next steps

After installing the Mobile Payments SDK and testing your application with test payments in the Square Sandbox, learn more about configuring your application to take production payments: