CCAvenue:"错误!!!解密申请请求"

时间:2018-03-24 10:34:53

标签: ios swift ccavenue

我正在尝试将CCAvenue支付网关集成到我在swift4中开发的iOS应用程序中。 我正在

  

"错误!!!解密申请请求时出现问题"

我已经检查了答案:https://stackoverflow.com/a/37327122/3548469,但我的情况没有运气。

以下是我从文件中试过的内容

private func gettingRsaKey(completion: @escaping (_ success: Bool, _ object: AnyObject?) -> ()){

    let serialQueue = DispatchQueue(label: "serialQueue", qos: .userInitiated)

    serialQueue.sync {
        print("access_code=\(CC_AVENUE_ACCESSKEY)")
        print("order_id=\(self.orderId)")
        self.rsaKeyDataStr = "access_code=\(CC_AVENUE_ACCESSKEY)&order_id=\(self.orderId)"
        let requestData = self.rsaKeyDataStr.data(using: String.Encoding.utf8)
        guard let urlFromString = URL(string: CC_AVENUE_RSAURL) else{
            return
        }
        var urlRequest = URLRequest(url: urlFromString)
        urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
        urlRequest.httpMethod = "POST"
        urlRequest.httpBody = requestData

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


        session.dataTask(with: urlRequest 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")
                    completion(false, "Not proper data for RSA Key" as AnyObject?)
                    return
                }
                print("data :: ",data)
                completion(true, data as AnyObject?)
            }
            else{
                completion(false, "Unable to generate RSA Key please check" as AnyObject?)
            }
            }.resume()
    }
}

 var request: NSMutableURLRequest?

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
            }
            DispatchQueue.main.async {
                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()
}

我能够生成无法进一步的RSA密钥。

注意*

我有真实帐户,我使用访问代码商家密钥作为帐户 AccessCode和orderId与用于生成RSA密钥的相同。

1 个答案:

答案 0 :(得分:6)

嘿,我遇到了同样的问题。这是因为新的CCAvenue SDK。用于加载页面请求的CCAvenue流程分为三个步骤。

1)我们从服务器获得RSA密钥。

2)我们使用此RSA密钥加密订单详细信息并创建Web请求。

3)在webview中打开请求。

当步骤1和步骤3之间没有任何延迟时,问题就出现了。为了克服这个问题,新的SDK引入了一个创建延迟的新步骤。

1)我们从服务器获得RSA密钥。

2)我们使用此RSA密钥加密订单详细信息并创建Web请求。

3)CCAvenue在此引入了一个新的URLRequest,仅用于在步骤1和4中延迟

4)在网页浏览中打开请求。

大部分时间都可以使用,但是当步骤3以毫秒为单位完成时,或者在步骤1和步骤4中没有延迟或很少延迟时,它会失败。

因此,此处的解决方法是在步骤1和步骤4中创建手动延迟。

将您的功能更改为:

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()
}