Build on iOS

Link to section

Overview

While the In-App Payments SDK supports payments for physical and digital sales, using the In-App Payments SDK to process digital sales might not be allowed by some mobile application distribution services (such as App Store and Google Play) and might result in your application being removed. Examples of digital sales include intangible goods (such as in-game assets), online services (such as upgrades in application functionality), and digital subscriptions (such as cloud or streaming services).

To avoid the potential removal from application distribution platforms, the In-App Payments SDK should only be used to process sales of goods or services that are consumed outside the application. You should review the terms of service for the respective mobile application distribution services you use to ensure that you're compliant with their guidelines.

Link to section

Requirements and limitations

  • Your application ID - Find your application ID on the Credentials page of your Square application in the Developer Dashboard.

  • You need an active location ID - Copy a developer account location ID from the Locations page of your Square application in the Developer Dashboard or set the Developer Dashboard to Sandbox mode and then copy a Sandbox location ID.

  • The deployment target for your application is iOS 12.0 or later.

  • You've added the In-App-Payments SDK to your iOS project. For more information, see Install the In-App-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/activate.

  • You're generally familiar with developing applications on iOS. If you're new to iOS development, you should read The Basics at Swift.org.

  • You're using Xcode 15+.

Link to section

1. Set your Square application ID in your application delegate

Update REPLACE_ME in the following code with your Square application ID. If you don't set your Square application ID before initializing SQIPCardEntryViewController, your application terminates with an uncaught exception.

Important

To test an application in the Square Sandbox, set the Developer Dashboard to Sandbox mode before completing the following instructions in this step.

@import SquareInAppPaymentsSDK; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [SQIPInAppPaymentsSDK setSquareApplicationID:@"<# REPLACE_ME #>"]; return YES; }

Replace <# REPLACE_ME #> with your application ID. For example, the application ID sq0ids-KHbh5gEaJuH7GEi1lgGMBQ replaces the placeholder as shown in the following code:

[SQIPInAppPaymentsSDK setSquareApplicationID:@"sq0ids-KHbh5gEaJuH7GEi1lgGMBQ"];

Note

When testing your application in the Square Sandbox, be sure to use Sandbox test values to enter card information.

Link to section

2. Set supported orientations for the card entry form

The In-App Payments SDK supports all orientations on the iPad, but only supports portrait orientations on the iPhone.

If your application supports landscape on the iPhone, you should conform to UINavigationControllerDelegate and override the supported interface orientations.

@interface < #YourViewController #>() < UINavigationControllerDelegate> @end @implementation < #YourViewController #> - (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations : ( UINavigationController *)navigationController; { return UIInterfaceOrientationMaskPortrait; } @end
Link to section

3. Create and display the card entry form to collect card information

Initialize a SQIPCardEntryViewController with a SQIPTheme instance and present the view controller as a modal. You can customize the appearance of the card entry form by setting SQIPTheme properties. For more information, see Customize the Payment Entry Form.

Important

The card entry form should always be displayed in a UINavigationController. For example, let nc = OrderNavigationController(rootViewController: orderViewController), where the orderViewController renders the In-App Payments card entry form.

Link to section

4. Implement SQIPCardEntryViewControllerDelegate methods

This build guide has shown you how to:

  • Configure your application for the SDK (step 1).
  • Set the card entry form orientation (step 2).
  • Show the card entry form in your view controller (step 3).
  • Create a stub function to handle card entry results (step 3).

In this step, you learn how to handle the result of the card entry by getting the one-time-use secure payment token from the card entry form and sending the token to your backend to be processed as a payment. You add code to the stubbed card entry response delegates from step 3 to get the payment token and send it to your backend.

Your application must take two actions after the payment token is returned:

  • Send the token to your backend for payment processing.
  • Handle the buyer's action of the card entry completion or cancellation.
Link to section

Send the payment token to your backend

Guidance for :didObtainCardDetails:completionHandler:

Use the card entry view controller delegate methods to complete the payment processing after a buyer provides a payment card by submitting the payment token to your backend, awaiting a response, and then updating the mobile application with the results.

  • Submit the payment token to your backend - The handler is invoked after the buyer submits a payment card. Your code should send the provided card details to your backend to perform any additional work, such as charging the card. After your backend processes the card, notify the card entry view controller of the result so it can be displayed to the buyer.
  • Handle backend results - If your backend processes the card with no error, call the completion handler with a single nil argument. A success animation is shown to the buyer and cardEntryViewController:didCompleteWithStatus: is called. At this point, you should dismiss the card entry view controller.
  • Handle card processing errors - If your backend returns an error while processing the card, call the completion handler with the error. The localizedDescription error is shown in the card entry view controller. The buyer can read the error text, edit the entered card information, and then resubmit the payment.
Link to section

Server-side payment completion

The following mobile client example code assumes that you've created a backend service for your application to process the payment token received from the In-App Payments SDK. The backend service calls the Payments API to create a payment that charges the card input by a buyer in your mobile client. For more information about taking a payment, see Take Payments.

Link to section

Handle the buyer's action in the card entry form

Guidance for :didCompleteWithStatus:

  • Invoked when the card entry form is completed. The status parameter indicates whether the card entry succeeded or was canceled.
  • Use this method to dismiss the card entry view controller and update any other application state.
- (void)cardEntryViewController:(nonnull SQIPCardEntryViewController *)cardEntryViewController didCompleteWithStatus:(SQIPCardEntryCompletionStatus)status; { // Note: If you pushed the card entry form onto an existing navigation controller, // use [self.navigationController popViewControllerAnimated:YES] instead [self dismissViewControllerAnimated:YES completion:nil]; }
Link to section

Save a card on file

When your backend service has received the payment token from your mobile application, it can take a payment but it can also use the token to store a card on file for the customer. Your mobile application must provide enough customer information for the backend to find the customer using the SearchCustomers endpoint. For example, if your mobile application has the buyer's email address and sends it to your backend along with the payment token.

To learn how to save a card on file from your backend, see Create a Card on File and a Payment.

Link to section

See also