DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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
  1. DZone
  2. Coding
  3. Frameworks
  4. Integrating Systems Into a Swift App

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!

ayush kumar user avatar by
ayush kumar
·
Mar. 15, 19 · Tutorial
Like (1)
Save
Tweet
Share
7.60K Views

Join the DZone community and get the full member experience.

Join For Free

Steps 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:

  1. 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 the serverType to eServerTypeProduction.
  2. Now create the PGOrder instance with all the parameters.
  3. 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.

app Swift (programming language)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: