Skip to content

Instantly share code, notes, and snippets.

@rajnandan1
Created March 29, 2022 10:21
Show Gist options
  • Save rajnandan1/54e5abc9b2e90bab30290d7b5d2dec2c to your computer and use it in GitHub Desktop.
Save rajnandan1/54e5abc9b2e90bab30290d7b5d2dec2c to your computer and use it in GitHub Desktop.
Cashfree PG Integration in iOS
import Foundation
import UIKit
import CashfreePGCoreSDK
import CashfreePG
class YourViewController: UIViewController, CFCardPaymentDelegate, CFNetbankingPaymentDelegate, CFWalletPaymentDelegate, CFUPIPaymentDelegate, CFPaylaterPaymentDelegate {
private let cfPaymentGatewayService = CFPaymentGatewayService.getInstance()
override func viewDidLoad() {
self.cfPaymentGatewayService.setCallback(self)
}
private func getSession() -> CFSession? {
do {
let session = try CFSession.CFSessionBuilder()
.setEnvironment(.SANDBOX)
.setOrderID("order_id")
.setOrderToken("orderToken")
.build()
return session
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
return nil
}
@IBAction func cardPayTapped(_ sender: Any) {
// Get Session
if let session = self.getSession() {
do {
// CFCard
let card = try CFCard.CFCardBuilder()
.setCVV("123")
.setCardNumber("4111111111111111")
.setCardExpiryYear("22") // in YY format
.setCardExpiryMonth("12")
.setCardHolderName("Cashfree")
.build()
// CFCardPayment
let cardPayment = try CFCardPayment.CFCardPaymentBuilder()
.setCard(card)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(cardPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
@IBAction func emiCardPayTapped(_ sender: Any) {
// Get Session
if let session = self.getSession() {
do {
// CFEMICard
let card = try CFEMICard.CFEMICardBuilder()
.setCVV("123")
.setCardNumber("4111111111111111")
.setCardExpiryYear("22") // in YY format
.setCardExpiryMonth("12")
.setCardHolderName("Cashfree")
.setEMITenure(3)
.setBankName("icici")
.build()
// CFEMICardPayment
let emiCardPayment = try CFEMICardPayment.CFEMICardPaymentBuilder()
.setCard(card)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(emiCardPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
@IBAction func upiCollectPayTapped(_ sender: Any) {
// Get Session
if let session = self.getSession() {
do {
// CFUPI
let upiCollect = try CFUPI.CFUPIBuilder()
.setChannel(.COLLECT)
.setUPIID("testsuccess@gocash")
.build()
// CFUPIPayment
let upiPayment = try CFUPIPayment.CFUPIPaymentBuilder()
.setUPI(upiCollect)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(upiPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
@IBAction func upiIntentPayTapped(_ sender: Any) {
let installedApps = CFUPIUtils().getInstalledUPIApplications()
// Get Session
if let session = self.getSession() {
do {
// CFUPI
let upiIntent = try CFUPI.CFUPIBuilder()
.setChannel(.INTENT)
.setUPIID(installedApps[0]["id"] ?? "") // Using first index. (App should be shown to user based on the list and clicked app's "id" has to be sent here
.build()
// CFUPIPayment
let upiPayment = try CFUPIPayment.CFUPIPaymentBuilder()
.setUPI(upiIntent)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(upiPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
@IBAction func walletPayTapped(_ sender: Any) {
// Get Session
if let session = self.getSession() {
do {
// CFWallet
let wallet = try CFWallet.CFWalletBuilder()
.setProvider("phonepe")
.setPhone("9999999999")
.build()
// CFWalletPayment
let walletPayment = try CFWalletPayment.CFWalletPaymentBuilder()
.setWallet(wallet)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(walletPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
@IBAction func netBankingPayTapped(_ sender: Any) {
// Get Session
if let session = self.getSession() {
do {
// CFNetbanking
let nb = try CFNetbanking.CFNetbankingBuilder()
.setBankCode(3003)
.build()
// CFNetbankingPayment
let nbPayment = try CFNetbankingPayment.CFNetbankingPaymentBuilder()
.setNetbanking(nb)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(nbPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
@IBAction func paylaterPayTapped(_ sender: Any) {
// Get Session
if let session = self.getSession() {
do {
// CFPayLater
let payLater = try CFPaylater.CFPaylaterBuilder()
.setPhone("9999999999")
.setProvider("lazypay")
.build()
// CFPaylaterPayment
let payLaterPayment = try CFPaylaterPayment.CFPaylaterPaymentBuilder()
.setPaylater(payLater)
.setSession(session)
.build()
// Initiate SDK Payment
try self.cfPaymentGatewayService.doPayment(payLaterPayment, viewController: nil)
} catch let e {
let error = e as! CashfreeError
print(error.localizedDescription)
// Handle errors here
}
}
}
// MARK: - CARD and EMICARD PAYMENT CALLBACKS
func initiatingCardPayment() {
// Show Loader
}
func presentWebForAuthenticatingCardPayment() {
// Present CFWebView here
}
func cardPayment(didFinishExecutingWith error: CFErrorResponse) {
// Show Error here
}
func verifyCardPaymentCompletion(for orderId: String) {
// Start Payment verification here
}
// MARK: - NETBANKING PAYMENT CALLBACKS
func initiatingNetbankingPayment() {
// Show Loader
}
func presentWebForAuthenticatingNetbankingPayment() {
// Present CFWebView here
}
func netbankingPayment(didFinishExecutingWith error: CFErrorResponse) {
// Show Error here
}
func verifyNetbankingPaymentCompletion(for orderId: String) {
// Start Payment verification here
}
// MARK: - WALLET PAYMENT CALLBACKS
func initiatingWalletPayment() {
// Show Loader
}
func presentWebForAuthenticatingWalletPayment() {
// Present CFWebView here
}
func verifyWalletPaymentCompletion(for orderId: String) {
// Start Payment verification here
}
func walletPayment(didFinishExecutingWith error: CFErrorResponse) {
// Show Error here
}
// MARK: - UPI PAYMENT CALLBACKS
func initiatingUPIPayment() {
// Show Loader
}
func presentWebForAuthenticatingUPIPaymentInSandBoxEnvironment() {
// Present CFWebView here
}
func verifyUPIPaymentCompletion(for orderId: String) {
// Start Payment verification here
}
func upiPayment(didFinishExecutingWith error: CFErrorResponse) {
// Show Error here
}
// MARK: - PAYLATER PAYMENT CALLBACKS
func initiatingPaylaterPayment() {
// Show Loader
}
func presentWebForAuthenticatingPaylaterPayment() {
// Present CFWebView here
}
func verifyPaylaterPaymentCompletion(for orderId: String) {
// Start Payment verification here
}
func payLaterPayment(didFinishExecutingWith error: CFErrorResponse) {
// Show Error here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment