Updated 30 April 2019
This blog will be about the integration Midtrans payment gateway in the iOS application using swift language. Before the start to integration the Midtrans payment, let you know payment type in the Midtrans payment gateway.
1. GO-PAY: Midtrans accepting transaction directly from the website or chat application.
2. AKULAKU: Midtrans Payment service without credit card from the customer account. Customers can make payments using the credit limit on my Akulaku account.
3. Indomaret: Midtrans Payment services through Indomaret outlets throughout Indonesia. Payment can be made by cash or debit.
4. Credit card: Midtrans Payment using a credit/debit card bearing the VISA / MasterCard / JCB / Amex logo, from all local and international banks.
5.Virtual Account: Transfer payment services from any local account via ATM Bersama / Prima / Alto. Facilitated by Bank Permata.
6.Mandiri clickpay: Internet banking payment service from Bank Mandiri. Payment can be made by entering a 16 digit Mandiri debit and token card.
Let start the integrate Midtrans payment gateway, for this, we have to follow some steps which I mentioned in the below
1 |
pod 'MidtransKit' |
1 2 3 4 5 6 7 8 |
import MidtransKit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. MidtransConfig.shared().setClientKey("client-key", environment: .production, merchantServerURL: "server-url") return true } |
Important: We need to add -ObjC to the Xcode project. Navigate to your .xcodeproj file in Xcode, choose your app main target in Targets, and in the Build Settings tab, search for Other Linker Flags, double click and add -ObjC.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
func payment(){ let shippingAddress = MidtransAddress(firstName: , lastName: , phone: , address:, city:, postalCode:, countryCode: ) // Billing and Shipping Address let customerDetail = MidtransCustomerDetails(firstName: , lastName:, email: , phone: , shippingAddress:shippingAddress, billingAddress:shippingAddress) // Customer details let transactionDetail = MidtransTransactionDetails(orderID: ,andGrossAmount: ) // Transaction details // Customer details for a generate token from charge API. let customer : NSDictionary = [ "billing_address": [ "address": "", "city": "", "country_code": "", "first_name": "", "last_name": "", "phone": "", "postal_code": "" ], "email": "", "first_name": "", "last_name": "", "phone": "", "shipping_address": [ "address": "", "city": "", "country_code": "", "first_name": "", "last_name": "", "phone": "", "postal_code": "" ] ] // Item details for a genarate token from charge API. let item_details :NSArray = [ [ "id": "", "name": "", "price":, "quantity": 1 ] ] // Transaction details for a genarate token from charge API. let transaction_details : NSDictionary = [ "currency": "", "gross_amount":, "order_id": "" ] let parameters = [ "customer_details": customer as Any, "item_details":items as Any, "transaction_details": transaction as Any, ] as [String : Any] //charge API method calling makeApiCalling(parameters: parameters, completion: { (response: NSDictionary?, error:String?) in if error == nil{ //Checking token genarate is success or fail var token: MidtransTransactionTokenResponse? // Genarate SDK Token token = MidtransTransactionTokenResponse.modelObject(with: response as? [AnyHashable : Any], transactionDetails: transactionDetail, customerDetails: customerDetail, itemDetails: itemDetails) DispatchQueue.main.async { if let vc = MidtransUIPaymentViewController.init(token: token){ //create PaymentView object vc.paymentDelegate = self //handle the MidtransUIPaymentViewController event self.present(vc, animated: true, completion: nil) } } }else{ print(error) //fail to get token from charge API } }) } func makeApiCalling(parameters :[String : Any] ,completion: @escaping (NSDictionary?, String?) -> Void){ // Must use this header to hitting charge API. let headers = [ "Content-Type": "application/json", "cache-control": "no-cache", ] do{ let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) // convert dicnary to Data var request = URLRequest(url: URL(string: "https://www.server-url/charge/index.php")!) //create url request request.httpMethod = "POST" //request type request.allHTTPHeaderFields = headers //set header in the request request.httpBody = postData //Set body let dataTask = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response,error) -> Void in if (error != nil || data == nil) { completion( nil, "Connection Failed.") } else { do{ if let response = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary { let errorResponse = response["error_messages"] if errorResponse != nil { completion( nil, errorResponse as? String) } else { completion( response, nil) } }else{ completion( nil, "Data Error") } }catch{ completion( nil, "Server Error") } } }) dataTask.resume() }catch{ } } |
3.3 Now finally we handle the event from SDK.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
extension ViewController: MidtransUIPaymentViewControllerDelegate { func paymentViewController(_ viewController: MidtransUIPaymentViewController!, save result: MidtransMaskedCreditCard!) { // To handle event when successfully to save card } func paymentViewController(_ viewController: MidtransUIPaymentViewController!,saveCardFailed error: Error!) { // To handle event when failed to save card } func paymentViewController(_ viewController: MidtransUIPaymentViewController!,paymentFailed error: Error!) { // To handle event when transaction is failed } func paymentViewController(_ viewController: MidtransUIPaymentViewController!, paymentPending result: MidtransTransactionResult!) { // To handle event when transaction is pending } func paymentViewController(_ viewController: MidtransUIPaymentViewController!,paymentSuccess result: MidtransTransactionResult!) { // To handle event when transaction is successfull } func paymentViewController_paymentCanceled(_ viewController: MidtransUIPaymentViewController!) { // To handle event when user canel the payment } } |
Hope the above blog will help you in integrate Midtrans payment gateway in iOS using swift.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.