Skip to main content

CCAvenue Payment Gateway Integration in Swift 3 - iOS

CCAvenue Payment Gateway Integration



First check this link "https://www.ccavenue.com/inapp_payments.jsp" for integration, what CCAvenue provides.
Kindly follow the below steps for implementing the Payment Gateway on your Mobile Application by Non Seamless Method :
1. Kindly make sure that your Public/Outgoing IP has been waitlisted on CCAvenue's end.
2. Place GetRSA and ccavResponseHandler files on your server.
3. In your App, mention the path of GetRSA file kept on your server
4. Call GetRSA from your app which is kept on your server for fetching the RSA public key.
5. Using the fetched public key merchant will encrypt parameters(amount,currency). To encrypt the RSA key, do following steps (From Step1 to Step8).
Step1: You have to download CCAvenue SDK from CCAvenue website and to download you must have to login using merchant account credential.

Integration link :https://mars.ccavenue.com/downloads/Integration_kits.zip
Step2: After login, to download SDK go to Resources->Mobile Integration Kit. See following screen shot.
enter image description here
Step3: To download, click on Download Integration Kits as shown in following screen shot. Before downloading choose Seamless or Non-Seamless. (Check details about this in document provided by CCAvenue)

Seamless: If a merchant is configured as seamless, then all the billing/shipping details including the payment options are accepted on the merchant page.
Non-Seamless: If a merchant is configured as non-seamless, then the Billing/Shipping details including the payment options are accepted on the CCAvenue's mobile page.
enter image description here
Step4: After downloading SDK kit, you have to copy folder "OpenSSL" to your project or else you can install "openssl" using pods also.
Step5: You will find 2 folders include and lib under Openssl/openssl-1.0.1i-iOS folder. Also under Build Settings of project go for Search Paths->Library, Search Paths, remove what is provided Users/test/desktop/.... and drag & drop include/ and lib/ this two folder. Also you have to do same thing for Paths->User Header Search Paths.
Stop6: Copy this two files (Files available in you downloaded SDK`s) to you project "CCTOOL & BASE64.
Step7: Using bridging header import "CCTool.h".
Step8: Finally encrypt your RSA key using the method which is present in "CCTool" file.
6. After encrypting the parameters you will post the encrypted value along with the other billing shipping details(if any) to the CCAvenue server via a browser(embedded webView) post. (If you did not able to convert all code from objective-c to swift, use objective-c file CCWEBVIEWCONTROLER)
7. The user will be redirected to the CCAvenue's billing shipping page where he will fill in the required details including the payment option.
8. After filling the required details he will be redirected to the bank page wherein he will authorise himself.
9. After authorisation he will be redirected to CCAvenue and a response i.e. Success or failure will be sent to the merchant page which was configured as return url during registration.
10. You should then decrypt the response, which can then be parsed to get transaction status.
Note :
  • Access code and order ID should be the same as passed during the RSA call.
  • Order ID should always be unique
  • The transaction should hit the CCAvenue server within 60 seconds after receiving the RSA key.
  • Working key posted in ccavResponseHandler file should be corresponding to the Accesscode being posted to CCAvenue.

  • In ViewDidLoad :
Method To get RSA Key

    let rsaKey = getRsaKeyForCCAvenue()

    //Encrypting Card Details
    let myRequestString = "amount=\(AMOUNT)&currency=\(CURRENCY)"

    let ccTool = CCTool()

    var encVal = ccTool .encryptRSA(myRequestString, key: rsaKey)

    let charset = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]").invertedSet
    encVal = encVal.stringByAddingPercentEncodingWithAllowedCharacters(charset)

    //Preparing for a webview call
    let urlAsString = TRANS_URL

    let encryptedStr = "\(KEY_MERCHANT_ID)=\(MERCHANT_ID)&\(KEY_ORDER_ID)=\(ORDER_ID)&\(KEY_REDIRECT_URL)=\(REDIRECT_URL)&\(KEY_CANCEL_URL)=\(CANCEL_URL)&\(KEY_ENC_VAL)=\(encVal)&\(KEY_CC_ACCESS_CODE)=\(CC_ACCESS_CODE)&\(KEY_BILLING_NAME)=""&\(KEY_BILLING_ADDRESS)=""&\(KEY_BILLING_ZIP)=""&\(KEY_BILLING_CITY)=""&\(KEY_BILLING_STATE)=""&\(KEY_BILLING_COUNTRY)=""&\(KEY_BILLING_TEL)=""&\(KEY_BILLING_EMAIL)="""

    let myRequestData = encryptedStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    let request = NSMutableURLRequest(URL: NSURL(string: urlAsString)!)

    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")

    request.setValue("https://secure.ccavenue.com/", forHTTPHeaderField: "Referer")

    request.HTTPMethod = "POST"

    request.HTTPBody = myRequestData

    ccWebView .loadRequest(request)


    func getRsaKeyForCCAvenue() -> String {

    //Getting RSA Key
    let rsaKeyDataStr = "\(KEY_CC_ACCESS_CODE)=\(CC_ACCESS_CODE)&\(KEY_ORDER_ID)=\(ORDER_ID)"

    let requestData = rsaKeyDataStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    let rsaRequest = NSMutableURLRequest(URL: NSURL(string: RSA_KEY_URL)!)

    rsaRequest .setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")

    rsaRequest.HTTPMethod = "POST"

    rsaRequest.HTTPBody = requestData

    let rsaKeyData: NSData? = try? NSURLConnection.sendSynchronousRequest(rsaRequest, returningResponse: nil)

    var rsaKey =  NSString(data: rsaKeyData!, encoding: NSASCIIStringEncoding)

    let strNewRSA = rsaKey as! String

    let range: Range<String.Index> = strNewRSA.rangeOfString("<")!
    let index: Int = strNewRSA.startIndex.distanceTo(range.startIndex)

    rsaKey = strNewRSA.substringToIndex(range.startIndex)

    rsaKey = rsaKey?.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())

    rsaKey = "-----BEGIN PUBLIC KEY-----\n\(rsaKey!)\n-----END PUBLIC KEY-----\n"

    return rsaKey as! String
}
Web View Delegate method to read the response :
    func webViewDidFinishLoad(webView: UIWebView) {

    let webString = webView.request?.URL?.absoluteString
    if (webString! as NSString).containsString("PaymentCCAvenue/ReturnAPI") {

        let html = webView .stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")          
        if (html! as NSString).containsString("Success") {
        }
        else if (html! as NSString).containsString("Failure") {  
        }
        else {




        }
    }

}

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

Video Calling In IOS Objective C

Video Calling Sources Project homepage on GIT — https://github.com/QuickBlox/quickblox-ios-sdk/tree/master/sample-videochat-webrtc Download ZIP - https://github.com/QuickBlox/quickblox-ios-sdk/archive/master.zip Overview The VideoChat code sample allows you to easily add video calling and audio calling features into your iOS app. Enable a video call function similar to FaceTime or Skype using this code sample as a basis. It is built on the top of WebRTC technology.            System requirements The QuickbloxWebRTC.framework supports the next:     * Quickblox.framework v2.7 (pod QuickBlox)     * iPhone 4S+.     * iPad 2+.     * iPod Touch 5+.     * iOS 8+.     * iOS simulator 32/64 bit (audio might not work on simulators).     * Wi-Fi and 4G/LTE connections. Getting Started with Video Calling API Installation with CocoaPods CocoaPods is a dependency manag...