Applies to: In-App Payments SDK - iOS | Payments API | Customers API
Learn how to build with the In-App Payments SDK on iOS to provide a secure, managed payments client.
Applies to: In-App Payments SDK - iOS | Payments API | Customers API
Learn how to build with the In-App Payments SDK on iOS to provide a secure, managed payments client.
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.
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+.
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.
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"];
When testing your application in the Square Sandbox, be sure to use Sandbox test values to enter card information.
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
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.
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.
@import SquareInAppPaymentsSDK;
@interface <
#YourViewController #>() < SQIPCardEntryViewControllerDelegate> @end
@implementation <
#YourViewController #>
- (void)showCardEntryForm;
{
SQIPTheme *theme = [[SQIPTheme alloc] init];
// Customize the card entry form
theme.tintColor = UIColor.greenColor;
theme.saveButtonTitle = @"Submit";
SQIPCardEntryViewController *cardEntryForm =
[[SQIPCardEntryViewController alloc] initWithTheme:theme];
cardEntryForm.delegate = self;
// The card entry form should always be displayed in a UINavigationController.
// The card entry form can also be pushed onto an existing navigation controller.
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:cardEntryForm];
[self presentViewController:navigationController animated:YES completion:nil];
}
#pragma mark - SQIPCardEntryViewControllerDelegate
- (void)cardEntryViewController:(nonnull SQIPCardEntryViewController *)cardEntryViewController
didCompleteWithStatus:(SQIPCardEntryCompletionStatus)status;
{
// Implemented in Step 4.
}
- (void)cardEntryViewController:(nonnull SQIPCardEntryViewController *)cardEntryViewController
didObtainCardDetails:(nonnull SQIPCardDetails *)cardDetails
completionHandler:(void (^_Nonnull)(NSError *_Nullable))completionHandler;
{
// Implemented in Step 4.
}
@end
This build guide has shown you how to:
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:
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.
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.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.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.
#pragma mark - SQIPCardEntryViewControllerDelegate
- (void)cardEntryViewController:(nonnull SQIPCardEntryViewController *)cardEntryViewController
didObtainCardDetails:(nonnull SQIPCardDetails *)cardDetails
completionHandler:(void (^_Nonnull)(NSError *_Nullable))completionHandler;
{
// Send card nonce to your server to charge the card or store the card on file.
// When a response is received, call the completionHandler with `nil` for success,
// or an error to indicate failure.
/*
[MyAPIClient.shared chargeCardWithNonce:cardDetails.nonce
completion:^(id transaction, NSError *chargeError) {
if (chargeError) {
completionHandler(chargeError);
} else {
completionHandler(nil);
}
}];
*/
completionHandler(nil);
}
Guidance for :didCompleteWithStatus:
status
parameter indicates whether the card entry succeeded or was canceled.- (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];
}
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.
If you need more assistance, contact Developer and App Marketplace Support or ask for help in the Developer Forums.