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
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)
- Open your application in Xcode and select your project’s Package Dependencies tab and click the
+
button:

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

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

- 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 inTargets
, and inBuild Settings
tab, search forOther Linker Flags
, double click and add-ObjC
.
Prepare Transaction Details
If you're using sdkflow, you need to create/generate the transaction token (snaptoken) by providing a transaction details such as:
-
Item details :
MidtransItemDetail
class is provided to create this object. This class contains item id, item name, item price and item quantity. -
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. -
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];
Starting Payment
There are two ways to start presenting the payment page on the SDK, based on where the snaptoken/transaction token is created.
Start payment by using SDK flow
[[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.
Start payment by using snap token on iOS
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];
}];
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:
- paymentSuccess : handle your successful transaction here
- paymentPending : handle pending transaction here, for async payment, the last status that the sdk recieved will be here as pending.
- paymentFailed: handle your failed transaction here, this method will give error as a result.
- paymentCanceled: handle canceled transaction here, this is called only when customer closes the Payment Page of Midtrans SDK UIKit.
- 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:
- saveCard: to check if saved card for credit card is successful
- saveCardFailed : to check if saved card for credit card is failed