How to implement credit card scanning by using iPhone camera for payment

Posted By : Varun Wadhwa | 13-Mar-2016

Credit card scanning by using your iPhone's camera is fast and easy way for providing your card details for payment instead of manual entering your credit card details. In this blog I'll explain how to implement credit card scanning in your iOS native app.

Step 1.  First of all you need to add card.io iOS SDK by adding :

 pod 'CardIO'  

in Podfile of your project. 

Step 2. Now import card.io in your project by adding  #import "CardIO.h" in Bridging-Header.h file of your project.

Step 3. In order to add card scanning our class must conform to CardIOPaymentViewControllerDelegate (which is to provided us by card.io sdk)

import UIKit

class PaymentViewController: UIViewController,CardIOPaymentViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        CardIOUtilities.preload()
    }
    
    
    func userDidCancelPaymentViewController(paymentViewController: CardIOPaymentViewController!) {
        paymentViewController?.dismissViewControllerAnimated(true, completion: nil)
    }
    
    @IBAction func scanCardButtonTapped(sender: AnyObject) {
        //It will be starting point for card scanning 
        let cardIOVC = CardIOPaymentViewController(paymentDelegate: self)
        cardIOVC.modalPresentationStyle = .FormSheet
        presentViewController(cardIOVC, animated: true, completion: nil)
    }
    
    func userDidProvideCreditCardInfo(cardInfo: CardIOCreditCardInfo!, inPaymentViewController paymentViewController: CardIOPaymentViewController!) {
        if let info = cardInfo {
            //info.cardNumber holds the credit card number 
            //NSString(format:"%02lu/%lu",info.expiryMonth, info.expiryYear) holds expiry date
            //info.cvv holds cvv number of card
            paymentViewController?.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    
    func userDidCancelPayment() {
        dismissViewControllerAnimated(true, completion: nil)
    }  
}

Description of above code:

scanCardButtonTapped(sender : AnyObject) is user's defined method act as entry point of credit card scanning.

userDidProvideCreditCardInfo(cardInfo: CardIOCreditCardInfo!, inPaymentViewController paymentViewController: CardIOPaymentViewController!)  method called after successfully scanning of card  and cardInfo parameter of method holds credit card info.

userDidCancelPaymentViewController(paymentViewController: CardIOPaymentViewController!) called when user cancelled the card scanning.

 

THANKS

 

About Author

Author Image
Varun Wadhwa

Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.

Request for Proposal

Name is required

Comment is required

Sending message..