CCAvenue: “Error!!! in decrypting application request”

妖精的绣舞 提交于 2019-12-05 14:38:17

Hey I faced the same issue. Its because of the new CCAvenue SDK. The flow of CCAvenue for loading the page request is of three steps.

1) We get an RSA key from our server.

2) We encrypt the order details using this RSA key and create a web request.

3) Open the request in a webview.

The issue arrises when there is no delay left between step 1 and step 3. For overcoming this issue the new SDK introduced a new step for creating a delay.

1) We get an RSA key from our server.

2) We encrypt the order details using this RSA key and create a web request.

3) CCAvenue introduced a new URLRequest here just for making a delay in step 1 and 4

4) Open the request in a webview.

This works for most of the times but it fails when the step 3 is completed in milli seconds or in other terms there is no delay or very little delay in step 1 and step 4.

So the workaround here is to create a manual delay in step 1 and step 4.

SO change your function to below:

private func encyptCardDetails(data: Data){

guard let rsaKeytemp = String(bytes: data, encoding: String.Encoding.ascii) else{
    print("No value for rsaKeyTemp")
    return
}
rsaKey = rsaKeytemp
rsaKey = self.rsaKey.trimmingCharacters(in: CharacterSet.newlines)
rsaKey =  "-----BEGIN PUBLIC KEY-----\n\(self.rsaKey)\n-----END PUBLIC KEY-----\n"
print("rsaKey :: ",rsaKey)

let myRequestString = "amount=\(amount)&currency=\(getCurrency())"
let ccTool = CCTool()
var encVal = ccTool.encryptRSA(myRequestString, key: rsaKey)

encVal = CFURLCreateStringByAddingPercentEscapes(
    nil,
    encVal! as CFString,
    nil,
    "!*'();:@&=+$,/?%#[]" as CFString,
    CFStringBuiltInEncodings.UTF8.rawValue) as String?

let urlAsString = "https://secure.ccavenue.com/transaction/initTrans"
let encryptedStr = String(format:"merchant_id=%@&order_id=%@&redirect_url=%@&cancel_url=%@&enc_val=%@&access_code=%@", CC_AVENUE_MERCHANTID, self.orderId, CC_AVENUE_REDIRECTURL, CC_AVENUE_REDIRECTURL, encVal!,CC_AVENUE_ACCESSKEY)
print("access_code=\(CC_AVENUE_ACCESSKEY)")
print("order_id=\(self.orderId)")

let myRequestData = encryptedStr.data(using: String.Encoding.utf8)

request  = NSMutableURLRequest(url: URL(string: urlAsString)! as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 30)
request?.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request?.setValue(urlAsString, forHTTPHeaderField: "Referer")
request?.httpMethod = "POST"
request?.httpBody = myRequestData

print("\n\n\nwebview :: ",request?.url as Any)
print("\n\n\nwebview :: ",request?.description as Any)
print("\n\n\nwebview :: ",request?.httpBody?.description as Any)
print("\n\n\nwebview :: ",request?.allHTTPHeaderFields! as Any)

let session = URLSession(configuration: URLSessionConfiguration.default)
print("session",session)

session.dataTask(with: request! as URLRequest) {
    (data, response, error) -> Void in

    if let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode{

        guard let data = data else{
            print("No value for data")
            return
        }

        // Create the delay here
        DispatchQueue.main.asyncAfter(deadline: .now()+1.0, execute: {
            self.viewWeb.loadRequest(self.request! as URLRequest)
        }
        print("data :: ",data)
    }else{
        print("into else")
        showAlertWithTitleWithMessage(message: "Unable to load webpage currently, Please try again later.")
    }
    }.resume()
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!