Applies to: Mobile Payments SDK - iOS | Payments API | OAuth API
Learn how to build a secure in-person payment solution for iOS devices with the Mobile Payments SDK.
Applies to: Mobile Payments SDK - iOS | Payments API | OAuth API
Learn how to build a secure in-person payment solution for iOS devices with the Mobile Payments SDK.
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 a Square Reader or Square Stand.
There are four steps required to take your first payment using the Mobile Payments SDK:
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.
You're using Xcode 15.1+.
You've set your Xcode iOS deployment target to iOS 15+.
You have a Square application ID. You can find your application ID on the Developer Dashboard.
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 iOS. If you're new to iOS development, you should visit the Apple Training Center before continuing.
The Mobile Payments SDK requires that each user grant access to their device's current location while using your application. Bluetooth must be enabled to take payments with a contactless and chip reader and microphone access must be granted to take payments with a magstripe reader.
Include the following privacy key/value pairs in your application's Info.plist
file:
Key | Description |
---|---|
NSBluetoothAlwaysUsageDescription | Square uses Bluetooth to connect and communicate with Square readers and compatible accessories. |
NSLocationWhenInUseUsageDescription | Square needs to know where transactions take place to reduce risk and minimize payment disputes. |
NSMicrophoneUsageDescription | Some Square readers use the microphone to communicate payment card data to your device. |
The Mobile Payments SDK isn't compatible with custom devices from OEMs that violate Square's security rules or on devices that grant root access to the mobile device operating system code and kernel. These devices cannot connect to a contactless and chip reader.
The following additional considerations apply when trying to run the SDK on custom devices:
Get started with the Mobile Payments SDK by configuring your iOS application and taking a test payment in the Square Sandbox.
Install the Mobile Payments SDK with Swift Package Manager by following these steps in your Xcode project:
https://github.com/square/mobile-payments-sdk-ios
.2.0.0-beta3
.SquareMobilePaymentsSDK
product is added to your target.MockReaderUI
product to your target to simulate a physical reader when one is not present in a sandbox environment.Install the Mobile Payments SDK with Cocoapods by adding the following to your Podfile:
use_frameworks!
pod "SquareMobilePaymentsSDK", "~> 2.0.0-beta3"
# To use a simulated reader device to take sandbox payments, add the optional MockReaderUI:
pod "MockReaderUI", "~> 2.0.0-beta3", configurations: ['Debug']
On the Build Phases tab for your application target, click the + button (at the top left of the pane).
Choose New Run Script Phase. The new run script phase should be the last build phase. Otherwise, the build fails.
Paste the following into the editor panel of the new run script:
FRAMEWORKS="${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}" "${FRAMEWORKS}/SquareMobilePaymentsSDK.framework/setup"
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 application ID in your AppDelegates
didFinishLaunchingWithOptions
method.
To get your application ID, sign in to the Developer Dashboard and choose your application. If you don't have an application yet, see Get Started for steps to create a Square application.
Your application opens to the Credentials page. This page contains the Square application ID used to initialize the Mobile Payments SDK.
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.
import UIKit
import SquareMobilePaymentsSDK
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
MobilePaymentsSDK.initialize(
applicationLaunchOptions: launchOptions,
squareApplicationID: "<#your_square_application_id#>"
)
return true
}
}
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 personal access token and location ID from the Sandbox tab in your Developer Dashboard.
Don't deploy an application into production that contains your personal access token. For a secure authorization implementation in production, you should implement OAuth instead of using a personal access token.
When authorization completes, the AuthorizeCompletionHandler
provides the result of the authorization, including any errors.
import SquareMobilePaymentsSDK
class <#YourViewController#>: UIViewController {
func authorizeMobilePaymentsSDK(accessToken: String, locationID: String) {
guard MobilePaymentsSDK.shared.authorizationManager.state == .notAuthorized else {
return
}
MobilePaymentsSDK.shared.authorizationManager.authorize(
withAccessToken: accessToken,
locationID: locationID) { error in
guard let authError = error else {
print("Square Mobile Payments SDK successfully authorized.")
return
}
// Handle auth error
print("error: \(authError.localizedDescription)")
}
}
}
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 button that can be dragged anywhere on the screen and persists as you navigate between screens in your application. Instantiate the Mock Reader UI and show the button with try? self.mockReaderUI?.present()
.
import MockReaderUI
private lazy var mockReaderUI: MockReaderUI? = {
do {
return try MockReaderUI(for: MobilePaymentsSDK.shared)
} catch {
assertionFailure("Could not instantiate a mock reader UI: \(error.localizedDescription)")
}
return nil
}()
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 button 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 mock reader UI in your application, call self.mockReaderUI?.dismiss()
.
When testing card payment flows, any inserted cards must be removed through the mock reader UI before beginning a new payment.
To pair physical readers for production payments, see Pair and Manage Card Readers.
The PaymentManager
handles payments in the Mobile Payments SDK. Prior to starting a payment, you create PaymentParameters
to represent details of an individual payment, and PromptParameters
indicating how the payment prompts are presented to the buyer.
When setting up a payment, you must conform to the PaymentManagerDelegate
to handle results and errors from a payment. If the payment completes successfully, a Payment
object is returned, which can be accessed on the Sandbox Seller Dashboard or with the Payments API. If the payment fails, the response contains a PaymentError
.
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 if you want to take payments while offline. For the complete list of payment parameter values and details, see the iOS technical reference.
Each payment must also have PromptParameters
configured, consisting of a mode
and a set of additionalMethods
available to buyers. 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 the PromptParameters.mode
to default
.
If you create your own custom payment prompt UI, use the availableCardInputMethodsObserver
to update your UI if the available card entry methods change (for example, if a magstripe reader is inserted or removed from the mobile device).
When you're ready to begin a payment, call PaymentManager.startPayment()
with the PaymentParameters
and PromptParameters
you created for this payment.
import SquareMobilePaymentsSDK
class ViewController: UIViewController {
private var paymentHandle: PaymentHandle?
func startPayment() {
paymentHandle = MobilePaymentsSDK.shared.paymentManager.startPayment(
makePaymentParameters(),
promptParameters: PromptParameters(
mode: .default,
additionalMethods: .all
),
from: self,
delegate: self
)
}
func makePaymentParameters() -> PaymentParameters {
PaymentParameters(
idempotencyKey: UUID().uuidString,
amountMoney: Money(amount: 100, currency: .USD)
)
}
}
During each payment, Square takes control of the screen display to ensure that buyer information is handled securely and that the final confirmation of the payment is correctly shown to the buyer. While a payment is in progress, the UIViewController
passed to startPayment
must remain presented or pushed until your PaymentManagerDelegate
receives a callback indicating the payment has completed, failed, or was cancelled.
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 an error.
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:
If you need more assistance, contact Developer and App Marketplace Support or ask for help in the Developer Forums.