Safepay logo
Docs
Safepay logo
Launch Safepay

IOS SDK

Learn how to integrate Safepay Checkout into your IOS mobile application


A set of components allowing easy integration of Safepay Button and Safepay Checkout into your website, powered by zoid

1. Create an IBOutlet for the main view

Create an IBOutlet for the main view and declare a property for the WKWebView.

2. Variable declarations

Next, you declare several variables that are essential for tracking the state and managing the web view. These include flags to monitor success and failure states, as well as arrays to store the success and failure keywords.

3. Setup of URL and Success/Failure Keywords

When the view appears, this method sets up the URL and defines the success and failure keywords that will be used when the view appears.

4. Configure the Web View to Load the Safepay URL

This method initializes and configures the web view to load the Safepay URL. It sets up the web view, loads the specified URL, and adds an observer to monitor changes in the URL.

5. URL Check

Inside the observation method, it retrieves the new URL from the change dictionary and checks whether it contains any success or failure URLs from the `success_array` and `failure_arrays` arrays.

6. Handle success

The code loops through the success URL addresses obtained from `success_array`. It checks each address to see if the redirected URL contains it. If a match is found, the isSuccess flag is set to true, indicating a successful URL match. Additionally, the isFailure flag is set to false to signify no failure.

7. Handle Failure

Similarly, the code iterates through the failure URL addresses retrieved from `failure_array`. For each address, it examines whether the redirected URL contains it. If a match is detected, the isFailure flag is set to true, indicating a failure URL match. Simultaneously, the isSuccess flag is set to false to denote the absence of a success.

8. Delay and Action

If a success or failure URL is found, it schedules the doCompletionAction method to be called after a delay of 2 seconds. This is necessary because the observer triggers rapidly with each URL change. The delay will ensure that only one condition is met before performing your action.

9. Completion Action

The doCompletionAction method is responsible for handling the completion action based on whether the URL indicates success or failure. It ensures that the action is executed only once (isSuccessAlreadyFired flag), and then executes the appropriate block of code for success or failure.

Preview
// Properties
@property (weak, nonatomic) IBOutlet UIView *webMainView;
@property (strong, nonatomic) WKWebView *safePayWebView;
BOOL isSuccessAlreadyFired;
BOOL isSuccess;
BOOL isFailure;
NSString *safePayURL;
NSArray * success_array;
NSArray * failure_array;
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
// Changed URL in case of success on Safepay Hosted Checkout
safePayURL = @"https://sandbox.api.getsafepay.com/checkout/external/mobile?env=development&source=mobile&beacon=track_fd2fdcd2-b5ca-4c1f-b703-01ccb38b92a1&tracker=track_fd2fdcd2-b5ca-4c1f-b703-01ccb38b92a1&sig=e224792a7fa3f52da74a9cbedae9f58b27e59064d248a7217b61bb13321da431&action=complete";
// Changed URL in case of an error on Safepay Hosted Checkout
safePayURL = @"https://sandbox.api.getsafepay.com/checkout/external/error?error=Tracker%20is%20in%20an%20invalid%20state";
// Changed URL in case of success on Safepay Embedded Checkout
safePayURL = @"https://sandbox.api.getsafepay.com/embedded/external/complete?environment=sandbox&tbt=6Qw_YaQ304iuB-Ad3UXicBOQwYT33huFj3I1zLYrkbRmpiz3ce2dD2Up3xDWoxeS8mivGmW17g%3D%3D&tracker=track_a7d2bc6d-3b8f-4643-aae5-4f506307075b&finalizer_message=Card%20saved%20successfully&action=complete";
// Changed URL in case of success on Safepay Embedded Checkout
safePayURL = @"https://sandbox.api.getsafepay.com/embedded/external/error?environment=sandbox&tbt=6Qw_YaQ304iuB-Ad3UXicBOQwYT33huFj3I1zLYrkbRmpiz3ce2dD2Up3xDWoxeS8mivGmW17g%3D%3D&tracker=track_5e4f5be3-f235-47ef-82a7-8c0c680ea195&finalizer_message=error%20performing%20action%20%27AUTHORIZATION%27%3A%20cannot%20add%20payment%20method%3A%20duplicate%20&action=complete";
// Add success/failure keywords on completion
success_array = [@"success", @"success/done", @"complete", @"external/complete", @"external/mobile"];
failure_array = [@"failure", @"failed", @"not_processed", @"external/error"];
[self createSafePayWkWebView];
}
-(void)createSafePayWkWebView{
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
self.safePayWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.webMainView.bounds.size.width, self.webMainView.bounds.size.height) configuration:theConfiguration];
self.safePayWebView.navigationDelegate = self;
NSURL *url = [NSURL URLWithString:safePayURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.safePayWebView loadRequest:request];
[self.safePayWebView addObserver:self forKeyPath:@"URL" options:NSKeyValueObservingOptionNew context:nil];
[self.safePayWebView addSubview:self.webMainView];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"URL"] && object == self.safePayWebView) {
NSURL *currentURL = [change objectForKey:NSKeyValueChangeNewKey];
NSString *redirectedURL = currentURL.absoluteString;
isSuccess = NO;
isFailure = NO;
// Compare Success URL's
for (NSString * strSuccess in success_array) {
NSRange range = [redirectedURL rangeOfString:strSuccess];
if (range.location != NSNotFound) {
// NSLog(@"success url: %@", redirectedURL);
isSuccess = YES;
isFailure = NO;
}
}
// Compare Failure URL's
for (NSString * strFailure in failure_array) {
NSRange range = [redirectedURL rangeOfString:strFailure];
if (range.location != NSNotFound) {
// NSLog(@"failure url: %@", redirectedURL);
isFailure = YES;
isSuccess = NO;
}
}
if (isSuccess || isFailure){
[self performSelector:@selector(doCompletionAction) withObject:self afterDelay:2.0 ];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
-(void)doCompletionAction{
if (!isSuccessAlreadyFired) {
isSuccessAlreadyFired = YES;
if (isSuccess){
// Deal success block here
} else {
if (isFailure){
// Deal failure block here
}
}
}
}
Previous
Java SDK
Next
Element - Save Card