CCAvenue Gateway Integration kit for ios Application

后端 未结 6 1292
我寻月下人不归
我寻月下人不归 2020-12-13 07:44

Hi,
I have developed an application which needs payment gateway. I have already worked on \"Paypal\". It was successful and now i need to integrate another payment gate

6条回答
  •  离开以前
    2020-12-13 08:05

    For anyone who needs an answer for Swift 3.2 here is the code. Took me a while to convert to it. But here you go. Before that let me explain in case things are still unclear.

    (1) you place your GetRSA file on the server and then copy and paste that link in your app as "rsaKeyUrl".

    (2) The "getRsaKeyForCCAvenue()" function makes a call to that file placed on the server, the IP of which has to be whitelisted beforehand.

    (3) The "getRSA" file makes a request to CCavenue on the link that is already in it.(This is important as CCAvenue only accepts requests from whitelisted IP's and this enables all the requests to be routed from just one IP.

    (4) CCAvenue checks the details and processes the request and sends the response and redirects to the payment page if everything checks.

    (5) The redirection URL shall be the link of the "ccavResponseHandler" file which has your access code and translates the response.

    THE CODE IN SWIFT 3.2

    1) Get RSA Key

    func getRsaKeyForCCAvenue() -> String {
    
        //Getting RSA Key
        let rsaKeyDataStr = "access_code=\(accessCode)&order_id=\(orderId)"
    
        let requestData = rsaKeyDataStr.data(using: String.Encoding.utf8, allowLossyConversion: false)
    
        var request = URLRequest(url: URL(string: rsaKeyUrl)!)
    
    
        let rsaRequest =  NSMutableURLRequest(url: URL(string: rsaKeyUrl)!)
    
        rsaRequest .setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
    
        rsaRequest.httpMethod = "POST"
    
        rsaRequest.httpBody = requestData
    
        let rsaKeyData: NSData? = try? NSURLConnection.sendSynchronousRequest(rsaRequest as URLRequest, returning: nil) as NSData
    
        var rsaKey =  NSString(data: rsaKeyData! as Data, encoding: String.Encoding.ascii.rawValue)
        rsaKey = rsaKey?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) as NSString?
    
    
        //   rsaKey = rsaKey?.trimmingCharacters(in: NSCharacterSet.newlines) as? NSString
    
        rsaKey = "-----BEGIN PUBLIC KEY-----\n\(rsaKey!)\n-----END PUBLIC KEY-----\n" as NSString
        print(rsaKey)
        return rsaKey! as String
    }
    

    2) Code after super.viewDidLoad()

        let rsaKey = getRsaKeyForCCAvenue()
    
        //Encrypting Card Details
        let myRequestString = "amount=\(VQ_MOUNT)¤cy=\(currency)"
    
        let ccTool = CCTool()
    
        var encVal :NSString = ccTool.encryptRSA(myRequestString, key: (rsaKey as NSString) as String!) as NSString
    
        let charset : String = "!*'();:@&=+$,/?%#[]"
    
        encVal = CFURLCreateStringByAddingPercentEscapes(nil, encVal as CFString, nil, (charset as CFString), CFStringBuiltInEncodings.UTF8.rawValue) as String as NSString
    
    
        //Preparing for a webview call
        let urlAsString = TRANS_URL
    
        var encryptedStr = "merchant_id=\(merchantId)&order_id=\(orderId)&redirect_url=\(redirectUrl)&cancel_url=\(cancelUrl)&enc_val=\(encVal)&access_code=\(accessCode)"
    
        let myRequestData = NSData.init(bytes: encryptedStr.cString(using: .utf8), length: encryptedStr.characters.count) as Data
    
        let request = NSMutableURLRequest(url: URL(string: urlAsString)!)
    
    
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
    
        request.setValue(TRANS_URL, forHTTPHeaderField: "Referer")
    
        request.httpMethod = "POST"
    
        request.httpBody = myRequestData
    
        viewWeb.loadRequest(request as URLRequest)
    

    3) Finally the webViewDidFinishLoad() implementation

        func webViewDidFinishLoad(_ webView: UIWebView) {
        let webString = webView.request?.url?.absoluteString
        if ((webString as NSString?)?.range(of: "/ccavResponseHandler.jsp"))?.location != NSNotFound {
            let html: String? = webView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
            var transStatus = "Not Known"
            if (((html as NSString?)?.range(of: "Aborted"))?.location != NSNotFound) || (((html as NSString?)?.range(of: "Cancel"))?.location != NSNotFound) {
                transStatus = "Transaction Cancelled"
            }
            else if (((html as NSString?)?.range(of: "Success"))?.location != NSNotFound) {
                transStatus = "Transaction Successful"
            }
            else if (((html as NSString?)?.range(of: "Fail"))?.location != NSNotFound) {
                transStatus = "Transaction Failed"
            }
            var controller = storyboard?.instantiateViewController(withIdentifier: "CCResultViewController") as? CCResultViewController
            controller?.transStatus = transStatus
            controller?.modalTransitionStyle = .crossDissolve
            present(controller ?? UIViewController(), animated: true)
        }else {
            print("Something went wrong...")
        }
    }
    

提交回复
热议问题