iOS SDK

Sample project to implement the iOS SDK here.


Latest Version

Latest released version logs on Cocoapods

Latest released version logs on Github release page.


iOS SDK Changes

Starting from version 1.16.4

A new mandatory transaction callback delegate method called paymentDeny is introduce to handle the new 3ds Flow on Credit Card. Xcode will automatically tell you If you have not implemented this method, and will be able generate it for you to remove the error. You can see on the Transaction Callback/Delegates section for more detailed implementation.

//ViewController.swift
// MidtransUIPaymentViewControllerDelegate
  
//This delegate methods is added on ios sdk v1.16.4 to handle the new3ds flow
func paymentViewController(_ viewController: MidtransUIPaymentViewController!, paymentDeny result: MidtransTransactionResult!) {
        print("payment denied")
    }
//ViewController.m
#pragma mark - MidtransUIPaymentViewControllerDelegate

//This delegate methods is added on ios sdk v1.16.4 to handle the new3ds flow
- (void)paymentViewController:(MidtransUIPaymentViewController *)viewController paymentDeny:(MidtransTransactionResult *)result{
    NSLog(@"Deny %@", result);
}

Installation

1. Installing via SPM (Swift Package Manager)

  1. Open your application in Xcode and select your project’s Package Dependencies tab and click the + button:

  1. Copy the Midtrans SDK repository URL https://github.com/veritrans/Veritrans-ios-sdk into the search field, Under Dependency Rule, select Branch: master (to always get the latest version), and then click Add Package.

  1. After the package download completes, select, and click Add Package. For example, to use the UIKit version, you can select MidtransKit.

  1. It's done! Midtrans sdk should now be listed under Swift Package Dependencies in the Xcode Project navigator.

2. Installing via Cocoapods

📘

Prerequisite

Please install Cocoapods version 1.0.0. You can find the installation guide here

To install using Cocoapods, you need to add MidtransKit to your podfile

def shared_pods
  pod 'MidtransKit'
end
  • Navigate to your project's root directory and run pod init to create a Podfile.
  • Open up the Podfile and add MidtransKit to your project's target.
pod install
  • Save the file and run pod install to install MidtransKit.
  • Cocoapods will download and install MidtransKit and also create a .xcworkspace project.\

SDK Initialization

Once you have completed installation of MidtransKit, you can now initialize the SDK with your clientKey, merchant server URL and server environment in your AppDelegate

//AppDelegate
import MidtransKit

MidtransConfig.shared().setClientKey("VT-CLIENT-sandbox-client-key", environment: .sandbox, merchantServerURL: "https://merchant-url-sandbox.com")
//AppDelegate.m
#import <MidtransKit/MidtransKit.h>

[[MidtransConfig shared] setClientKey:@"VT-CLIENT-sandbox-client-key"
   environment:MidtransServerEnvironmentSandbox
 merchantServerURL:@"https://merchant-url-sandbox.com"];

📘

If you use Swift as your project language

You will need to add -ObjC to your Xcode project.
Navigate to your .xcodeproj file in Xcode, choose you app main target in Targets, and in Build Settings tab, search for Other Linker Flags, double click and add -ObjC.


Starting Payment


Start payment by using Snap token


The easiest way to use the SDK - what you need to do will be to integrate to Midtrans' backend and retrieve the Snap Token, then pass Snap token as argument of requestTransacation(withCurrentToken:completion:) method. Then you need to initialize MidtransUIPaymentViewController and pass the response. SDK will then show the payment page immediately; no need to create any transaction object on the SDK.


MidtransMerchantClient.shared().requestTransacation(withCurrentToken: snaptokenTextField.text!) { (response, error) in
    if (response != nil){
        //initialize MidtransUIPaymentViewController
        let vc = MidtransUIPaymentViewController.init(token: response)
        //set you ViewController as delegate
        vc?.paymentDelegate = self
        //present the payment page
        self.present(vc!, animated: true, completion: nil)
    } else {
        print("error \(error!)");
    }
}
[[MidtransMerchantClient shared] requestTransacationWithCurrentToken:{{string token}}
  completion:^(MidtransTransactionTokenResponse * _Nullable regenerateToken, NSError * _Nullable error) {
    //initialize MidtransUIPaymentViewController
    MidtransUIPaymentViewController *paymentVC = [[MidtransUIPaymentViewController alloc] initWithToken:token];
  	//set the delegate
    paymentVC.paymentDelegate = self;
    //present the payment page
  	[self.navigationController presentViewController:paymentVC animated:YES completion:nil];
}];

Transaction Callback / Delegates

SDK will give callback/ delegate of transaction response tothe host app. To be able to do so, please follow these steps:

  • Set your ViewController to conform with MidtransUIPaymentViewControllerDelegate
//ViewController
class ViewController: UIViewController, MidtransUIPaymentViewControllerDelegate {
//ViewController.m
@interface ViewController () <MidtransUIPaymentViewControllerDelegate>
@end
  • Set the delegate of MidtransUIPaymentViewController
//MARK: - MidtransUIPaymentViewControllerDelegate
//These are the 5 mandatory transaction callback/delegates that need to be implemented to handle all the transaction status
func paymentViewController(_ viewController: MidtransUIPaymentViewController!, paymentPending result: MidtransTransactionResult!) {
    print("payment pending")
}

func paymentViewController(_ viewController: MidtransUIPaymentViewController!, paymentSuccess result: MidtransTransactionResult!) {
    print("payment success")
}

func paymentViewController(_ viewController: MidtransUIPaymentViewController!, paymentFailed error: Error!) {
    print("payment failed")
}

func paymentViewController_paymentCanceled(_ viewController: MidtransUIPaymentViewController!) {
    print("payment cancelled")
}

func paymentViewController(_ viewController: MidtransUIPaymentViewController!, paymentDeny result: MidtransTransactionResult!) {
    print("payment denied")
}
//ViewController.m

#pragma mark - MidtransUIPaymentViewControllerDelegate

- (void)paymentViewController:(MidtransUIPaymentViewController *)viewController paymentSuccess:(MidtransTransactionResult *)result {
	NSLog(@"success: %@", result);
}

- (void)paymentViewController:(MidtransUIPaymentViewController *)viewController paymentFailed:(NSError *)error {
	NSLog(@"failed: %@", error);
}

- (void)paymentViewController:(MidtransUIPaymentViewController *)viewController paymentPending:(MidtransTransactionResult *)result {
	NSLog(@"pending: %@", result);
}

- (void)paymentViewController_paymentCanceled:(MidtransUIPaymentViewController *)viewController {
	NSLog(@"canceled");
}

//This delegate methods is added on ios sdk v1.16.4 to handle the new3ds flow
- (void)paymentViewController:(MidtransUIPaymentViewController *)viewController paymentDeny:(MidtransTransactionResult *)result{
  NSLog(@"Deny %@", result);
}
  • Implement the MidtransUIPaymentViewControllerDelegate functions

The 5 mandatory delegate functions that are required to implement:

  1. paymentSuccess : handle your successful transaction here
  2. paymentPending : handle pending transaction here, for async payment, the last status that the sdk recieved will be here as pending.
  3. paymentFailed: handle your failed transaction here, this method will give error as a result.
  4. paymentCanceled: handle canceled transaction here, this is called only when customer closes the Payment Page of Midtrans SDK UIKit.
  5. paymentDeny(Note This delegate method is added starting on ios sdk v1.16.4 to handle the new3ds flow) : handle your denied transaction here, especially for credit card payment.

The 2 optional delegate functions:

  1. saveCard: to check if saved card for credit card is successful
  2. saveCardFailed : to check if saved card for credit card is failed

Logging

Enable to show SDK log for debugging. For security purposes however, we encourage merchants to disable it on production apps.


MidtransNetworkLogger.shared().startLogging()
[[MidtransNetworkLogger shared] startLogging];