The S4/Apple SDK is a wrapper around the Shift4 REST API (Shift4 Gateway API). The Shift4 REST API is an http based API.
- As an integrator, you can integrate directly to the Shift4 REST API
- Or, you can use the SDK, which simplifies and abstracts many of the complexities of the REST API
- As a wrapper, please note the following
- You’ll still refer to the core REST API documentation (Shift4 Gateway API) for details on the transaction types, fields and all other details
- The SDK allows you to build requests and receive responses. Using the SDK, you’ll control the following aspects:
- The HTTP headers
- The HTTP body
- The HTTP type (POST, GET, etc)
- Some of the areas of the REST API that are simplified or eliminated are:
- The underlying http communication
- The SDK will handle http communication automatically
- Timeout and communication failure handling
- The SDK will handle timeouts and communication failures automatically
- Logging and log masking
- The SDK will log requests/responses and will mask sensitive data
- The underlying http communication
- As a wrapper, please note the following
The S4/Apple component requires iOS 15.6 or macOS 11.5 minimum. (Swift/Objective-C, Apple ARM, and Intel).
- If there is a need for compatibility with earlier versions of the Apple SDK, please contact your Shift4 Integration Analyst for review. If you do not already have a Shift4 Integration Analyst assigned, one will be assigned to you during project kickoff.
| Description | Version | File |
| Apple SDK Framework File | 1.20 | AppleSDK.zip |
| iOS Sample App | 1.0 | iOSSampleApp.zip |
| Mac Sample App | 1.0 | macOSSampleApp.zip |
The sample iOS, and MacOS applications can be used to demonstrate usage, and get started with development. Download the sample app .zip release above, and extract this package. Drop the sample src file into a new xcode project and compile.
S4/Apple is comprised of the following classes, all of which are located in the namespace S4RestAPI:
- S4Request: This class is used to setup a request
- S4Response: This is the class that represents a response
- S4Mode: this class has defines that instruct the API on which processing mode is being used
- S4Method: this class has defines that instruct the API on which http method is being used on the underlying request to Shift4
The following outlines the steps to process a transaction
The first step is to init an S4Request object. You must init a new object for every request.
The next step is to set some configuration parameters such as the method, endpoint, the processing mode.
Next, you’ll set the http headers as needed for your REST (Shift4 Gateway API) request. Such as the AccessToken.
The developer must then build up an NSDictionary object, and populate it with all of the structured data fields required for the request. When the request object is built, the developer calls setRequest and passes it the dictionary object.
Next, call the process method. It is an async method. Provide it an anonymous callback method as an argument. Make sure this callback has S4Response object as the only argument. This callback will be called when the transaction is completely processed and the response is available. The developer does not need to init an S4Response object. The process method will automatically attach the S4Response object to the anonymous callback.
The developer then calls getHttpStatusCode() to confirm the http result and getResponse() to retrieve a response object. This object contains all of the response fields. At this stage the response can be handled and processed in your integration layer
This SDK is provided in the form of a single .xcframework file folder. It is compatible with macOS, and iOS. Confirm that you have received the S4/.xcframework file. This should have been provided to you by your Integration Analyst prior to the integration project kick-off meeting.
- Drop the .xcframework file folder into your Xcode project.
- Add the framework as an asset in your project settings (Project → Targets → General → Frameworks, Libraries, and Embedded Content → Embed and Sign).
- Enable outgoing, incoming network capability.
- macOS: Big Sur 11.5 or later
- iOS: iOS 15.6 or later
- Apple ARM, and Intel
#import <Shift4RestAPISDK/Shift4RestAPISDK.h>
S4Request *s4Request = [[S4Request alloc] init];
[s4Request setConfig:@"Mode" mode:S4ModeHostDirect];
[s4Request setConfig:@"Method" method:S4MethodPOST];
[s4Request setConfig:@"Host" fieldValue:@"https://api.shift4test.com"]; // test environment, use https://api.shift4api.net/api/rest/v1 for production.
[s4Request setConfig:@"URLPath" fieldValue:@"/api/rest/v1/transactions/sale"];
[s4Request setHeader:@"InterfaceVersion" fieldValue:@"2.1"];
[s4Request setHeader:@"InterfaceName" fieldValue:@"RestAPIObjectiveCSdk"];
[s4Request setHeader:@"CompanyName" fieldValue:@"Shift4"];
[s4Request setHeader:@"AccessToken" fieldValue:@""]; //PROVIDED BY SHIFT4
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
[s4Request setRequest: @{
@"amount": @{
@"tax": @"1.00",
@"total": @"11.00",
},
@"dateTime": [dateFormatter stringFromDate:[NSDate date]],
@"transaction": @{
@"invoice": @"6820973"
},
@"card": @{
@"entryMode": @"M",
@"number": @"4444333322221111",
@"expirationDate": @"1229",
@"securityCode": @{
@"indicator": @"1",
@"value": @"333"
}
},
@"customer": @{},
@"postalCode": @"89000"
}];
NSLog(@"Processing Transaction");
[s4Request process:^(S4Response *s4Response) {
// Process complete async callback
NSLog(@"Completed Transaction");
if ([s4Response getHttpStatusCode] == 200) {
NSDictionary *responseData = [s4Response getResponse];
if (responseData[@"result"][0][@"transaction"][@"responseCode"] != nil) {
NSLog(@"Response Code: %@", responseData[@"result"][0][@"transaction"][@"responseCode"]);
}
if (responseData[@"result"][0][@"transaction"][@"authorizationCode"] != nil) {
NSLog(@"Authorization Code: %@", responseData[@"result"][0][@"transaction"][@"authorizationCode"]);
}
} else {
NSLog(@"Error: %ld", [s4Response getHttpStatusCode]);
NSLog(@"Error Response: %@", [s4Response getHttpError]);
if ([s4Response getRequiresAudit]) {
NSLog(@"Transaction Requires Audit");
// Developer needs to add this transaction to a list that requires manual audit
// See https://docs.shift4.com/guides/response-handling/timeouts-and-communications-failures
}
}
}];The enum names are prefixed by S4, to avoid name collisions with other enums you may have.
S4Mode is used to control the transaction processing mode.
- S4ModeComEng: Commerce engine. Used when processing direct to commerce engine on premises.
- S4ModeComEngCloud: Commerce engine cloud. Used when processing via commerce engine cloud.
- S4ModeHostDirect: Host direct. Used when processing direct to the Shift4 cloud endpoint.
S4Method is used to control the http method used to process a transaction.
- S4MethodDELETE: http delete
- S4MethodPOST: http post
- S4MethodGET: http get
S4LogVerbosity is used to control what messages are written to the optional built-in log file during a transaction. The lower the level of verbosity, the less lines are logged. Error being the lowest level.
- S4LogVerbosityTrace - highest level of logging
- S4LogVerbosityDebug
- S4LogVerbosityInfo - default
- S4LogVerbosityWarning
- S4LogVerbosityError - only errors will show in log
| Initializer Syntax | S4Request |
| Parameters | None |
| Results | S4Request instance |
| Description | S4Request is a class that is used to build and process a request. |
| Initializer Syntax | S4Response |
| Parameters | None |
| Results | S4Response instance |
| Description | S4Response is a class that represents a response. |
| Method Syntax |
|
| Parameters |
|
| Results | None |
| Description | Used to set configuration for a request. For a list of configuration values, see Configuration Options below. This function must be called once for each configuration that is being set. |
| Exceptions | None |
| Method Syntax | (void)setHeader:(nonnull NSString *)fieldName fieldValue:(nonnull NSString *)fieldValue; |
| Parameters |
|
| Results | None |
| Description | This function is used to specify the http headers in the request. For example, an Authorization requires 4 headers: InterfaceVersion, InterfaceName, CompanyName and AccessToken. To perform this transaction, you would call setHeader 4 times, once for each header. |
| Exceptions | None |
| Method Syntax |
|
| Parameters |
|
| Results | None |
| Description | This method is used to specify the body of the request. The body must match the JSON fields indicated in the REST API for the request that you are processing. |
| Exceptions | None |
| Method Syntax | (void)process:(void (^_Nonnull)(S4Response * _Nonnull s4Response))completionHandler; |
| Parameters | completionHandler |
| Results | None |
| Description | This async function is used to process a request. This call is non-blocking. Calls your completionHandler when the transaction is fully complete, and passes in a new S4Response object automatically. |
| Exceptions | None |
| Method Syntax | (NSInteger) getHttpStatusCode; |
| Parameters | None |
| Results | Returns an NSInteger representing the http response code
|
| Description | This method is used to obtain the http response code of the request. For example for an Authorization, you might get
|
| Exceptions | None |
| Method Syntax | (BOOL) getRequiresAudit; |
| Parameters | None |
| Results | Returns a boolean indicating if the request requires an audit |
| Description | As noted in the Handling Response guide, certain transactions may need to be recorded for manual review to determine if they processed not. While it’s rare that a transaction requires manual review, it can happen. This function call can be used to determine if manual review/audit is required. Note that when getHttpStatusCode returns a 200 (indicating a valid response), getRequiresAudit does not need to be checked. |
| Exceptions | None |
| Method Syntax | (nullable NSDictionary<NSString *, id> *) getResponse; |
| Parameters | None |
| Results | Returns an NSDictionary representing the JSON response. Returns nil in the case of no response. |
| Description | This method is used to obtain an object representing the JSON response. |
| Exceptions | None |
| Method Syntax | (nonnull NSDictionary<NSString *, NSString *> *) getHeaders; |
| Parameters | None |
| Results | Returns an immutable NSDictionary representing the http response headers |
| Description | This method is used to obtain the http response headers of the transaction. |
| Exceptions | None |
| Parameter Name | Mode |
| Mandatory/Optional | Mandatory |
| Description | This is a configuration that instructs the API on how it’s processing the transaction. Options are:
|
| Parameter Name | Host |
| Mandatory/Optional | Mandatory |
| Description | This is used to specify the host that the API will connect to. See Quick Start URLs for a list of potential values to use for the host. Example: for host direct, you would use
|
| Parameter Name | Method |
| Mandatory/Optional | Mandatory |
| Description | This is used to specify the http method for the transaction request that you are performing
|
| Parameter Name | LogEnabled |
| Mandatory/Optional | Optional |
| Description | By default, the API will record API activity to log file. It’s recommended to always leave this on.
|
| Parameter Name | LogVerbosity |
| Mandatory/Optional | Optional |
| Description | By default, the API will record API activity to log file with a log verbosity of Info. LogVerbosity can be adjusted with this configuration. It’s recommended to keep LogVerbosity at the default level.Logging from the SDK is done at these levels.
|
| Parameter Name | LogPath |
| Mandatory/Optional | Optional |
| Description | By default, the API will record API activity to log file in the local directory of the application that is running. This configuration can be used to change the log file path. |
| Parameter Name | Port |
| Mandatory/Optional | Optional |
| Description | This is used to specify the port that the API will connect to. See Quick Start URLs for a list of potential values to use for the host port.
|
| Parameter Name | Timeout |
| Mandatory/Optional | Optional |
| Description | This is used to set the request timeout value in seconds. It is not recommended to adjust this configuration. Default value is 65 |
| Parameter Name | URLPath |
| Mandatory/Optional | Optional |
| Description | This is used to specify the URL path for the request that you are performing This will match the url path specified in the REST API. For example, an Authorization would use api/rest/v1/transactions/authorization |
The S4/Apple framework supports a multi-threaded environment. However, each instance (object) of the API can only be used in one thread at a time. You cannot share an object concurrently between different threads at the same time. In practical terms, this means that a program must complete a request in one thread without using the same object in a different thread. Objects should be created and then discarded after each request.
The [s4Request process] call is non-blocking. Make sure you attach a callback function argument to handle the response.
S4/Apple is written in Objective C. It is ARC-enabled. You may need to make considerations for this if you’re importing the SDK into a Swift application.
When importing into Swift
- Ensure the
Shift4RestAPISDK.xcframeworkis added to the target’s “Frameworks, Libraries, and Embedded Content” with Embed & Sign. - Confirm that the module name
Shift4RestAPISDKis visible to Swift (import Shift4RestAPISDK). - If you have a mixed Objective‑C/Swift target, ensure your Objective‑C Bridging Header and Swift Compiler settings are configured correctly so that your own Objective‑C code can interoperate with the SDK types (e.g.,
S4Request,S4Response). - The SDK uses Foundation types (
NSString,NSDictionary, etc.). In Swift you’ll interact with them as bridged types (String,[String: Any], etc.) and may need to cast when parsing responses.
The SDK uses HTTPS to communicate with the Shift4 REST API. The Host configuration must be HTTPS. If you are using any non‑default TLS/ATS behavior, ensure your Info.plist configuration still allows connections to the Shift4 endpoints as documented in the Quick Start. You will also need to enable outgoing, incoming network capability.