payu-intrepos / PayUCheckoutPro-Android

PayUCheckoutPro SDK for Android
9 stars 9 forks source link

Invalid Hash error #39

Closed ApunichGULSHER closed 1 year ago

ApunichGULSHER commented 1 year ago

I copied the code from this repo and tested it in my app with my prod key-salt and test key-salt. But it returns an Invalid hash error after creating the hash inside override fun generateHash().

`private fun preparePayUBizParams(): PayUPaymentParams {

    val vasForMobileSdkHash = HashGenerationUtils.generateHashFromSDK(
        "$prodKey|${PayUCheckoutProConstants.CP_VAS_FOR_MOBILE_SDK}|${PayUCheckoutProConstants.CP_DEFAULT}|",
        prodSalt
    )

    val paymentRelatedDetailsHash = HashGenerationUtils.generateHashFromSDK(
        "$prodKey|${PayUCheckoutProConstants.CP_PAYMENT_RELATED_DETAILS_FOR_MOBILE_SDK}|",
        prodSalt
    )

    val additionalParamsMap: HashMap<String, Any?> = HashMap()

    additionalParamsMap[PayUCheckoutProConstants.CP_VAS_FOR_MOBILE_SDK] = vasForMobileSdkHash
    additionalParamsMap[PayUCheckoutProConstants.CP_PAYMENT_RELATED_DETAILS_FOR_MOBILE_SDK] =
        paymentRelatedDetailsHash

    return PayUPaymentParams.Builder().setAmount("10.0")
        .setIsProduction(false)
        .setKey(prodKey)
        .setProductInfo("Testing")
        .setPhone(phone)
        .setTransactionId(System.currentTimeMillis().toString())
        .setFirstName("Gulsher")
        .setEmail(email)
        .setSurl(surl)
        .setFurl(furl)
        .setAdditionalParams(additionalParamsMap)
        .setUserToken("Happy69")
        .build()
}`

And

``private fun initUiSdk(payUPaymentParams: PayUPaymentParams) { PayUCheckoutPro.open( requireActivity(), payUPaymentParams, object : PayUCheckoutProListener {

            override fun onPaymentSuccess(response: Any) {
                Toast.makeText(requireContext(), "Payment Successful", Toast.LENGTH_SHORT)
                    .show()
            }

            override fun onPaymentFailure(response: Any) {
                Toast.makeText(requireContext(), "Payment Failed", Toast.LENGTH_SHORT).show()
            }

            override fun onPaymentCancel(isTxnInitiated: Boolean) {
                Toast.makeText(requireContext(), "Payment Canceled", Toast.LENGTH_SHORT).show()
            }

            override fun onError(errorResponse: ErrorResponse) {

                if (errorResponse.errorMessage != null && errorResponse.errorMessage!!.isNotEmpty())
                    Toast.makeText(
                        requireContext(),
                        errorResponse.errorMessage!!,
                        Toast.LENGTH_SHORT
                    ).show()
                else
                    Toast.makeText(requireContext(), "Unknown Error", Toast.LENGTH_SHORT).show()
            }

            override fun generateHash(
                map: HashMap<String, String?>,
                hashGenerationListener: PayUHashGenerationListener
            ) {
                if (map.containsKey(CP_HASH_STRING) && map.containsKey(CP_HASH_NAME)) {

                    val hashData = map[CP_HASH_STRING]
                    val hashName = map[CP_HASH_NAME]
                    val hashType = map[CP_HASH_TYPE]

                    var salt = prodSalt

                    if (map.containsKey(PayUCheckoutProConstants.CP_POST_SALT))
                        salt = salt.plus(map[PayUCheckoutProConstants.CP_POST_SALT])

                    val hash: String? = if (hashType.equals(CP_V2_HASH)) {
                        HashGenerationUtils.generateV2HashFromSDK(hashData!!, prodSalt)
                    } else {
                        //calculate SDH-512 hash using hashData and salt
                        HashGenerationUtils.generateHashFromSDK(
                            hashData!!,
                            salt
                        )
                    }
                    Toast.makeText(requireContext(), "Hash: $hash", Toast.LENGTH_SHORT).show()
                    if (!TextUtils.isEmpty(hash)) {
                        val hashMap: HashMap<String, String?> = HashMap()
                        hashMap[hashName!!] = hash!!
                        hashGenerationListener.onHashGenerated(hashMap)
                    }
                }
            }

            override fun setWebViewProperties(webView: WebView?, bank: Any?) {
            }
        })
}

``

shahfaizalc commented 1 year ago

updated here. https://github.com/payu-intrepos/payu-non-seamless-react/issues/43#issuecomment-1366358503