Search code examples
iosswiftframeworkspayment-gateway

CFSDK payment gateway 'Invalid token sent in request'


I am working on an app in which I need to integrate payment gateway and I am using Cashfree payment gateway WebView Checkout option as per the need. It is easy to implement from their docs. This is how I initiate SDK:

func initiateCFSDK() {
        let cashfreeVC = CFViewController(params: getPaymentParams(), appId: self.appId, env: self.environmentCF, callBack: self)
        let navVC = UINavigationController(rootViewController: cashfreeVC)
        self.present(navVC, animated: true, completion: nil)
    }

Payment parameters:

func getPaymentParams() -> Dictionary<String, String> {
        return [
            "orderId": self.orderId,
            "tokenData" : self.paymentToken,
            "orderAmount": self.paymentValue,
            "customerName": "name",
            "orderNote": "health prodcuts",
            "orderCurrency": "INR",
            "customerPhone": "9876543210",
            "customerEmail": "abc@gmail.com",
            "notifyUrl": "https://test.gocashfree.com/notify"
        ]
    }

From their docs, we need to drag and drop framework to Xcode project and add it to Embedded Binaries. The token in generated from the backend using the orderId and need to pass it in payment parameters.

Problem 1: Everytime I Initiate SDK it gives me error: "Invalid token sent in request" and prints following result in delegate method:

Finished navigating to url https://test.cashfree.com/billpay/checkout/post/submit JSON value : {"orderId":"","referenceId":"","orderAmount":"","txMsg":"Invalid token sent in request","txTime":"","txStatus":"FAILED","paymentMode":"","signature":""}

Following is the screenshot for the reference.

enter image description here

Problem 2: Since I present the SDK by embedding inside a UINavigationController, when I press back button it can't dismiss itself.

I am banging my head from many weeks for the error (Invalid token) I can't resolve. So anyone here tried it and please take a look what is wrong? Looking forward for the solutions from SO.

P.S: I tried contacting their tech support and everytime they just sent link to their docs.


Solution

  • I have prepared demo project with Cash Free SDK, Using Xcode 11.0

    Step 1

    To Generate the token , I have used in postman

    https://test.cashfree.com/api/v2/cftoken/order

    with parameters

    {
    "orderId":"ORD123456",
    "orderAmount":"30",
    "orderCurrency":"INR"
    }
    

    with following headers

    Content-Type:application/json
    X-Client-Id:XXXXXXX
    X-Client-Secret:XXXXXX
    

    Step 2

    Now In code

    func initiateCFSDK() {
        let cashfreeVC = CFViewController(params: getPaymentParams(), appId: "xxxxxxxxxxx", env: "TEST", callBack: self)
        self.navigationController?.pushViewController(cashfreeVC, animated: true)
     }
           func getPaymentParams() -> Dictionary<String, String> {
               return [
                   "orderId": "ORD123456",
                   "tokenData" : "<<TOKEN FROM POSTMAN REQUEST>>",
                   "orderAmount": "30",
                   "customerName": "name",
                   "orderNote": "health prodcuts",
                   "orderCurrency": "INR",
                   "customerPhone": "9876543210",
                   "customerEmail": "abc@gmail.com",
                   "notifyUrl": "https://test.gocashfree.com/notify"
               ]
           }
    

    Here Nothing changed just used TEST environment and passed the appid and token

    Notes:

    1. make sure you are using TEST environment URL to generate token with TEST environment client id and client secret
    2. also check notifyUrl
    3. Order ID should be same
    4. make sure you are not using old or expired tokens

    Problem 2 : Don't bother to present , just push this controller :)

    Cheers !! :)

    enter image description here