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.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


Installation


def shared_pods
  pod 'MidtransCoreKit'
  pod 'MidtransKit'
end

target 'MyBeautifulApp' do
  shared_pods
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 --verbose
  • Save the file and run pod install to install MidtransKit.
  • Cocoapods will download and install MidtransKit and also create a .xcworkspace project.

//AppDelegate.m
#import <MidtransKit/MidtransKit.h>

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

Once you have completed installation of MidtransKit, configure it with your clientKey, merchant server URL and server environment in your AppDelegate.h


📘

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


There are two ways to start presenting the payment page on the SDK, based on where the Snap token/transaction token is created.


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 startPaymentUiFlow method. SDK will then show the payment page immediately; no need to create any transaction object on the SDK.

From iOS SDK v1.12.0, we provide SDK method to allow you to make payment by using snap token without initialize transaction details. You just need to pass Snap token as argument of requestTransacationWithCurrentToken: method.


[[MidtransMerchantClient shared] requestTransacationWithCurrentToken:{{string token}}
  completion:^(MidtransTransactionTokenResponse * _Nullable regenerateToken, NSError * _Nullable error) {
  	MidtransUIPaymentViewController *paymentVC = [[MidtransUIPaymentViewController alloc] initWithToken:token];
  	paymentVC.paymentDelegate = self;
  	[self.navigationController presentViewController:paymentVC animated:YES completion:nil];
}];

Start payment by using SDK flow


Prepare Transaction Details


If you're using SDK flow, you need to create/generate the transaction token (Snap token) by providing a transaction details such as:

  1. Item details : MidtransItemDetail class is provided to create this object. This class contains item id, item name, item price and item quantity.

  2. Customer details : MidtransCustomerDetails class is provided to create this object. This class contains customer first name, last name, email, phone, shipping address and billing address.

  3. Transaction details : MidtransTransactionDetails class is provided to create this object. This class contains order id and gross amount. Order id should always be unique for every transaction.


//ViewController.m
MidtransItemDetail *itemDetail =
[[MidtransItemDetail alloc] initWithItemID:@"item_id"
                                      name:@"item_name"
                                     price:item_price
                                  quantity:item_quantity];

MidtransCustomerDetails *customerDetail =
[[MidtransCustomerDetails alloc] initWithFirstName:@"user_firstname"
                                          lastName:@"user_lastname"
                                             email:@"user_email"
                                             phone:@"user_phone"
                                   shippingAddress:ship_address
                                    billingAddress:bill_address];

MidtransTransactionDetails *transactionDetail =
[[MidtransTransactionDetails alloc] initWithOrderID:@"order_id"
                                     andGrossAmount:items_gross_amount];

[[MidtransMerchantClient shared]
 requestTransactionTokenWithTransactionDetails:transactionDetail
 itemDetails:self.itemDetails
 customerDetails:customerDetail
 completion:^(MidtransTransactionTokenResponse *token, NSError *error)
 {
     if (token) {
        MidtransUIPaymentViewController *vc = [[MidtransUIPaymentViewController alloc] initWithToken:token];
        [self presentViewController:vc animated:YES completion:nil];
     }
     else {
         // do something on error
     }
 }];

If you have created the transaction details in the SDK, now all you need to do is call the requestTransactionTokenWithTransactionDetails method, and then present the payment page when the token reponse is retrieved.




Transaction Callback / Delegates


//ViewController.m

#import <MidtransKit/MidtransKit.h>

@interface ViewController () <MidtransUIPaymentViewControllerDelegate>
//other code

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

  • Set your view controller to conform with MidtransUIPaymentViewControllerDelegate

//ViewController.m

MidtransUIPaymentViewController *vc = [[MidtransUIPaymentViewController alloc] initWithToken:token];
//set the delegate
vc.paymentDelegate = self;
  • Set the delegate of MidtransUIPaymentViewController

//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