Selling Cookies Demo App with Reader SDK for iOS

Overview

Reader SDK allows you to quickly create in-person payment solutions with built-in EMV and PCI compliance using Square hardware. Let's build a simple iOS app for taking payments at a Cookie Cart. (Note: Reader SDK is available on both Android and iOS.)

Cookie shop animated

First, let's clone the project on GitHub.

Then, we'll go through the following steps to integrate Reader SDK into the project:

  1. Get Reader SDK credentials.
  2. Configure the Cookie Cart app.
  3. Authorize Reader SDK.
  4. Show the Reader Settings screen.
  5. Start Checkout.

Note: Reader SDK requires the system microphone and location permissions to process payments. See PermissionsViewController for an example.

Let's get started!

Step 1: Get Reader SDK credentials.

  1. Open the Square Application Dashboard. You will be prompted to log in or create a new account.
  2. Create a new Square application and call it Cookie Cart.
  3. Click the Cookie Cart app to bring up your Square application settings pages.
  4. Open the Reader SDK tab and click Request Credentials to generate your Reader SDK repository password.

In the Cookie Cart app directory, install Reader SDK with the application ID and repository password (from the Reader SDK settings page):

ruby <(curl https://connect.squareup.com/readersdk-installer) install \
   --app-id APPLICATION_ID_FROM_STEP_1                                \
   --repo-password REPO_PASSWORD_FROM_STEP_1

After it finishes, open the project in Xcode.

Step 3: Authorize Reader SDK.

Reader SDK must be authorized to collect payments. Let's open AuthorizationViewController and add some code to authorize Reader SDK.

First, we need to get a one-time authorization code. For testing, you can get one from your Square application settings page under the Reader SDK tab. In production, you should use the Mobile Authorization API. Once you've obtained an authorization code, fill out the placeholder in the authorizeReaderSDK method at the end of the file.

Next, let's fill in the authorizeReaderSDK method:

let authorizationCode = "<#Your Mobile Authorization Code#>"

// Check if Reader SDK is already authorized
if SQRDReaderSDK.shared.isAuthorized,
    let authorizedLocation = SQRDReaderSDK.shared.authorizedLocation {
    print("Location: \(authorizedLocation.name)")
    return
}

// Authorize Reader SDK
SQRDReaderSDK.shared.authorize(withCode: authorizationCode) { (location, error) in
    // Success! We're authorized to accept payments with Reader SDK.
    if let authorizedLocation = location {
        print("Location: \(authorizedLocation.name)")
    }

    // An error occurred :(
    if let localizedErrorDescription = error?.localizedDescription {
        // In your real application, ensure you handle errors gracefully instead of crashing.
        fatalError(localizedErrorDescription)
    }
}

This first checks if Reader SDK is already authorized, and if it is, prints the authorized location's name. If Reader SDK isn't authorized, we call the authorize(withCode:) method and handle the result.

Step 4: Show the Reader Settings screen.

Reader SDK supports Square Reader for contactless and chip. In order to pair and manage readers, we need to show the Reader Settings screen.

Open PayViewController and add the following to the settingsButtonTapped method to present the Reader Settings screen when the Settings button is tapped:

let readerSettingsController = SQRDReaderSettingsController(delegate: self)
readerSettingsController.present(from: self)

Step 5: Start Checkout.

While we're still in PayViewController, let's fill out the charge method:

let money = SQRDMoney(amount: amount)
let checkoutParameters = SQRDCheckoutParameters(amountMoney: money)
checkoutParameters.note = note
checkoutParameters.additionalPaymentTypes = [.manualCardEntry, .cash]

let checkoutController = SQRDCheckoutController(parameters: checkoutParameters, delegate: self)
checkoutController.present(from: self)

First, we create a SQRDMoney instance that represents the amount to be charged in the currency of the authorized location.

Then, we make a SQRDCheckoutParameters instance that customizes the checkout flow. Use this to configure accepted payment types, tip settings, and more.

Finally, we create a SQRDCheckoutController instance with our parameters and show it by calling the present(from:) method.

Conclusion

You're all set! You can run the Cookie Cart app on the iOS simulator to take cash payments or on an iOS device to take card payments with Square hardware.

Follow the Reader SDK Setup Guide to integrate Reader SDK into your own app!