facebook

Paytm Payment Integration in iOS using Swift Language

By Avijit Mondal

Paytm Payment Integration in iOS using Swift Language

When you start working with PayTm Payment Integration, first of all, you have to know the basic idea of installing framework file manually. I recently worked on this process and faced some problem while installing the PodFile. That’s why I recommended you to install PaymentsSDK file from this link. Then follow these steps one by one.

Step 1: After successfully downloading, just drag and drop the .framework file into your swift project.

Step 2: After completing step 1, you need to add the framework into your target file that you can find here- YourProject(TARGET) >BuildPhrase >Link Binary With Libraries, after that check if it add into – YourProject(TARGET) >General >Linked Frameworks With Libraries, this directory or not. If not added then you can also do the same by clicking the “+” button. You can now use PaymentsSDK easily into your project’s each and every View Controller by using “import PaymentSDK” in header section in your swift file.

Step 3: If you find some error in step 2 then step 3 is for you otherwise you can proceed with the next step. When I have done this two-step, I found an error Like “PaymentSDK framework not found” after compiling. I suggest you simply two things to do to remove that error. First, enable your bit code by simply going to YourProject(TARGET) >Build Setting >Build Option >Enable Bitcode

and set it to Yes because here we are using that framework which is bitcode enabled. Forgot it if already set Yes and now add paymentSDK framework to the Other Linker Framework by going to YourProject(TARGET) >Build Setting >Linking >Other Liker Flags

add PaymentSDK framework file just like this,

now clean your project by simply using Cmd + Option + Shift + K and build it. I hope this gonna work perfectly.

Step 4: Now it’s time to dive into your code in your ViewController.swift file. Import PaymentSDK and create a delegate method of Payment “PGTransactionDelegate” because we are now using this delegate method to work with the payment procedure and in it two variable just like this,

var server = PGServerEnvironment()

        var txnController = PGTransactionViewController()

After this, you need to write some code to navigate to transaction controller for payment procedure.

func beginPayment() {

server = server.createProductionEnvironment()

let type :ServerType = .eServerTypeProduction

let order = PGOrder(orderID: “”, customerID: “”, amount: “”, eMail: “”, mobile: “”)

order.params = [“MID”: “rxazcv89315285244163”,

“ORDER_ID”: “order1”,

“CUST_ID”: “cust123”,

“MOBILE_NO”: “7777777777”,

“EMAIL”: “username@emailprovider.com”,

“CHANNEL_ID”: “WAP”,

“WEBSITE”: “WEBSTAGING”,

“TXN_AMOUNT”: “100.12”,

“INDUSTRY_TYPE_ID”: “Retail”,

“CHECKSUMHASH”: “oCDBVF+hvVb68JvzbKI40TOtcxlNjMdixi9FnRSh80Ub7XfjvgNr9NrfrOCPLmt65UhStCkrDnlYkclz1qE0uBMOrmuKLGlybuErulbLYSQ=”,

“CALLBACK_URL”: “https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=order1”]

self.txnController =  self.txnController.initTransaction(for: order) as?PGTransactionViewController

self.txnController.title = “Paytm Payments”

self.txnController.setLoggingEnabled(true)

if(type != ServerType.eServerTypeNone) {

self.txnController.serverType = type;

} else {

return

}

self.txnController.merchant = PGMerchantConfiguration.defaultConfiguration()

self.txnController.delegate = self

self.navigationController?.pushViewController(self.txnController, animated: true)

}

I assume that all merchant config and all params for this method are created from the backend side. The suggestion is to talk with your backend programmer and get understood that merchant configuration.

** Here you need to know something if you are working Stagging Environment then just change the

server = server.createStagingEnvironment()

** And if you do not use navigation controller don’t worry, you can present that payment page as a popup by creating an instance of navigationController, because Paytm not allow presenting that txnController. You need to modify that just like this :

let storyBoard: UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)

        let navigationController = storyBoard.instantiateViewController(withIdentifier: ReuseIdentifiers.identifierForNavigationVC) as! UINavigationController

        navigationController.pushViewController(txnController, animated: true)

        navigationController.modalTransitionStyle = .crossDissolve

        txnController.delegate = self

        self.present(navigationController, animated: true, completion: nil)

Step 5: After doing the previous step your last work remains to check whether the payment is successful or cancel or failed. If you want to check it then you need to perform some method from PGTransactionDelegate method those are :

//this function triggers when transaction gets finished

func didFinishedResponse(_ controller: PGTransactionViewController, response responseString: String) {

let msg : String = responseString

var titlemsg : String = “”

if let data = responseString.data(using: String.Encoding.utf8) {

do {

if let jsonresponse = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] , jsonresponse.count > 0{

titlemsg = jsonresponse[“STATUS”] as? String ?? “”

}

} catch {

print(“Something went wrong”)

}

}

let actionSheetController: UIAlertController = UIAlertController(title: titlemsg , message: msg, preferredStyle: .alert)

let cancelAction : UIAlertAction = UIAlertAction(title: “OK”, style: .cancel) {

action -> Void in

controller.navigationController?.popViewController(animated: true)

}

actionSheetController.addAction(cancelAction)

self.present(actionSheetController, animated: true, completion: nil)

}

//this function triggers when transaction gets cancelled

func didCancelTrasaction(_ controller : PGTransactionViewController) {

controller.navigationController?.popViewController(animated: true)

}

//Called when a required parameter is missing.

func errorMisssingParameter(_ controller : PGTransactionViewController, error : NSError?) {

controller.navigationController?.popViewController(animated: true)

}

Now you can do whatever you want into those function. I hope that this tutorial helps you a lot. Thanks for visiting our blog.

Avijit Mondal Author
iOS Developer , Openweb Solutions

iOS Developer at Openweb Solutions

Posts created 4

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
shares