Integrating Systems Into a Swift App
This post is about how we can integrate the Paytm payment service with an iOS app. Read on to get started!
Join the DZone community and get the full member experience.
Join For FreeSteps to Integrate Paytm via the SDK
1. Import the Library
A library can be imported via two processes:
a) Via pods
- Add pod 'Paytm-Payments' in the pod file.
- Run 'pod install' from the terminal.
- Open the xcworkspace.
- Go to "Link Binary With Libraries" in the "Build Phases" in the project settings, and add SystemConfiguration.framework.
- Check if PaytmSDK.framework is added in both “Link Binary With Libraries” and “Embedded Binaries.” If not, add it by clicking on the plus icon.
b) Directly add the library to the project
- Download the SDK from https://github.com/Paytm-Payments/Paytm_iOS_App_Kit/tree/master/Swift.
- Add Paytm.framework to the project.
- Go to "Link Binary With Libraries" in the "Build Phases" in the project settings, and add SystemConfiguration.framework.
- Check if PaytmSDK.framework is added in both “Link Binary With Libraries” and “Embedded Binaries.” If not, add it by clicking on the plus icon.
2. Add PaytmSDK to ViewController
import PaymentSDK
3. Generate Checksum
Checksumhash is a unique id used by Paytm to ensure that the transaction has not been tampered with. Checksumhash is generated on the server-side.
4. Start the Payment Process
A transaction can be made via the following processes:
- First, choose the Paytm server based on the environment. For staging, create an instance of the PGServerEnvironment and set the
serverType
to eServerTypeStaging. For production, create an instance of the PGServerEnvironment and set theserverType
to eServerTypeProduction. - Now create the PGOrder instance with all the parameters.
- Create the PGTransactionViewController instance by calling initTransactionForOrder and pass the instance of PGOrder as a parameter.
private func setupPaytm() {
serv = serv.createStagingEnvironment()
let type :ServerType = .eServerTypeStaging
let order = PGOrder(orderID: "", customerID: "", amount: "", eMail: "", mobile: "")
order.params = ["MID":"rxazcv89315285234363",
"ORDER_ID":"order1",
"CUST_ID": "121",
"CHANNEL_ID":"WAP",
"INDUSTRY_TYPE_ID":"Retail",
"WEBSITE": "APP_STAGING",
"TXN_AMOUNT": "1024",
"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)
}
5. Implement the 'PGTransactionDelegate' Protocol
Implement the 'PGTransactionDelegate' protocol in the view-controller from where payments are made:
//MARK : Delegate Methods
extension ScheduleOnOderVC : PGTransactionDelegate {
//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)
}
}
6. Verify Checksum
Checksumhash needs to be verified so that we know the response has not been tampered with. Again, verification is done on the server-side.
Published at DZone with permission of ayush kumar. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Tech Hiring: Trends, Predictions, and Strategies for Success
-
Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
-
A Deep Dive Into the Differences Between Kafka and Pulsar
-
Integrating AWS With Salesforce Using Terraform
Comments