Skip to content

Getting Started on iPhone

HeboHebo edited this page Aug 31, 2021 · 10 revisions

iPhone

The AppSwitch SDK supports two countries, which corresponds to respective versions of the MobilePay app, and each app has a distinct url scheme:

MobilePay Denmark: mobilepay://

MobilePay Finland: mobilepayfi://

####1. Enable AppSwitch Open your info.plist find "URL types" entry and add a "URL Identifier" entry with a value matching your bundle identifier "com.trifork.MobilePayFruitShop".

Then add a "URL Schemes" Item with a name matching your app. In this example we use "fruitshop", and you should not add this to your production app - this is just an example! This is the part that enables opening your app from an url.

add urlscheme to info.plist

In this case you are now able to open a browser on your iPhone and type in "fruitshop://" and your app will open.

As of Xcode 7 and iOS9 SDK you must also whitelist the MobilePay URL scheme for the SDK (your merchant app) in order for it to be able to open the MobilePay app. This must be added to your info.plist - see the below screenshot. whitelist urlscheme in info.plist

####2. Add the SDK to your project You can add the AppSwitch SDK either via CocoaPods or manually

CocoaPods

Close Xcode

Create a Podfile with the following:

platform :ios, '7.0'

target 'Fruitshop' do
pod 'MobilePay-AppSwitch-SDK'
end

Run a 'pod install' in the folder where your Podfile is located.

Open your projects workspace file - f.ex fruitshop.xcworkspace. Don't open your project with *.xcodeproj after adding CocoaPods

Or read the tutorials on CocoaPods website - CocoaPods.org

Manually

The SDK is a static lib libMobilePayManager.a with a matching header file MobilePayManager.h.

Add the lib to your target under "Build Phases" -> "Link Binary Libraries With Libraries"

link sdk with your project

Add MobilePayManager.h to your project

add header file

Open AppDelegate.m and setup the MobilePayManager

import "MobilePayManager.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /*
     * INFO!!! See the example app for more details on how to use the SDK.
     */
    [[MobilePayManager sharedInstance] setupWithMerchantId:@"APPDK0000000000" merchantUrlScheme:@"fruitshop" country:MobilePayCountry_Denmark];
    return YES;
}

Replace "APPDK0000000000" and "fruitshop" in the example above with the MerchantId you have received from Danske Bank, and the URLScheme you have defined in your app. This example is based on integrating with the Danish version of the MobilePay app.

If your project is able to build in Xcode without errors, you are ready to go.

Please note: It is NOT a requirement that you initialize the SDK in the appdelegate. Most likely you will do this when a certain viewcontroller related to payment is loaded. But this is a design decision you have to make!

####3. Create your first payment

To start the payment flow you call the following method

    MobilePayPayment *payment = [[MobilePayPayment alloc]initWithOrderId:@"123456" productPrice:product.price];
    //No need to start a payment if one or more parameters are missing
    if (payment && (payment.orderId.length > 0) && (payment.productPrice >= 0)) {
        
        [[MobilePayManager sharedInstance]beginMobilePaymentWithPayment:payment error:^(MobilePayErrorPayment * _Nullable mobilePayErrorPayment) {
            NSError *error = mobilePayErrorPayment.error;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:error.localizedDescription
                                                            message:[NSString stringWithFormat:@"reason: %@, suggestion: %@",error.localizedFailureReason, error.localizedRecoverySuggestion]
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Install MobilePay",nil];
            [alert show];
            
        }];
        
    }

OrderId is sent through the payment flow and returned when payment is completed so that you are able to track the payment and deliver the right product.

An error will occur if MobilePay isn't installed, else the payment flow will start.

Documentation for all parameters can be found in MobilePayManager.h

####4. Handle callbacks from MobilePay All communication back to your app is done through url communication and can be handled in a single method.

Open AppDelegate.m and add the following code

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    //IMPORTANT - YOU MUST USE THIS IF YOU COMPILING YOUR AGAINST IOS9 SDK
    [self handleMobilePayPaymentWithUrl:url];
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //IMPORTANT - THIS IS DEPRECATED IN IOS9 - USE 'application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options' INSTEAD
    [self handleMobilePayPaymentWithUrl:url];
    return YES;
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
 
    //IMPORTANT - THIS IS DEPRECATED IN IOS9 - USE 'application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options' INSTEAD
    [self handleMobilePayPaymentWithUrl:url];
    return YES;
}

- (void)handleMobilePayPaymentWithUrl:(NSURL *)url
{
    [[MobilePayManager sharedInstance]handleMobilePayPaymentWithUrl:url success:^(MobilePaySuccessfulPayment * _Nullable mobilePaySuccessfulPayment) {
        NSString *orderId = mobilePaySuccessfulPayment.orderId;
        NSString *transactionId = mobilePaySuccessfulPayment.transactionId;
        NSDecimalNumber *amountWithDrawnFromCard = mobilePaySuccessfulPayment.amountWithdrawnFromCard;
        NSLog(@"MobilePay purchase succeeded: Your have now paid for order with id '%@' and MobilePay transaction id '%@' and the amount withdrawn from the card is: '%@'", orderId, transactionId,amountWithdrawnFromCard);
        [ViewHelper showAlertWithTitle:@"MobilePay Succeeded" message:[NSString stringWithFormat:@"You have now paid with MobilePay. Your MobilePay transactionId is '%@'", transactionId]];
        
    } error:^(MobilePayErrorPayment * __nullable mobilePayErrorPayment) {
        NSError *error = mobilePayErrorPayment.error;
        NSDictionary *dict = error.userInfo;
        NSString *errorMessage = [dict valueForKey:NSLocalizedFailureReasonErrorKey];
        NSLog(@"MobilePay purchase failed:  Error code '%li' and message '%@'",(long)error.code,errorMessage);
        [ViewHelper showAlertWithTitle:[NSString stringWithFormat:@"MobilePay Error %li",(long)error.code] message:errorMessage];
        
        //TODO: show an appropriate error message to the user. Check MobilePayManager.h for a complete description of the error codes
        
        //An example of using the MobilePayErrorCode enum
        //if (error.code == MobilePayErrorCodeUpdateApp) {
        //    NSLog(@"You must update your MobilePay app");
        //}
    } cancel:^(MobilePayCancelledPayment * _Nullable mobilePayCancelledPayment) {
        NSLog(@"MobilePay purchase with order id '%@' cancelled by user", mobilePayCancelledPayment.orderId);
        [ViewHelper showAlertWithTitle:@"MobilePay Canceled" message:@"You cancelled the payment flow from MobilePay, please pick a fruit and try again"];
        
    }];
}

You can now move on to the API implementation https://github.com/MobilePayDev/MobilePay-AppSwitch-API



###Examples of customizing different properties/settings of the SDK See the MobilePayManager.h file for the defaults of each property

####IsMobilePayInstalled This is the ONLY method that can called before the setupWithMerchantId:, and it checks if AppStore version of MobilePay is installed on the device.

Currently there is a version of the MobilePay app in two countries, and therefore two URL schemes:

Denmark: mobilepay://

Finland: mobilepayfi://

if ([[MobilePayManager sharedInstance]isMobilePayInstalled:MobilePayCountry_Denmark]) {
     [[MobilePayManager sharedInstance] setupWithMerchantId:@"APPDK0000000000" merchantUrlScheme:@"fruitshop" timeoutSeconds:60 returnSeconds:3 captureType:MobilePayCaptureType_Reserve country:MobilePayCountry_Denmark];
}

####Reserve/Partial Capture Reserve: A reservation of the amount is made.

Partial Reservation: A approximated amount is reserved. And a amount smaller than the reserved amount is withdrawn.

Default is Reserve.

[MobilePayManager sharedInstance].captureType = MobilePayCaptureType_Reserve ;

####TimeoutSeconds A time limit you set for which the user must have swiped in MobilePay to confirm the purchase. If exceeded errorcode 8 is returned. Default is 0 = never timeout.

This is just an example, but our recommendation is to set it set it high in case the user is on a edge / poor connection.

[[MobilePayManager sharedInstance]setTimeoutSeconds:90];