flutter-braintree icon indicating copy to clipboard operation
flutter-braintree copied to clipboard

Invalid client token crash, when tokenizeCreditCard call

Open intsitesdotru opened this issue 1 year ago • 5 comments

Hi, iOS application crashes when we calling the Braintree.tokenizeCreditCard function if you pass an invalid tokenization key(empty string for example) in the authorization field. You can check in your example by replacing the tokenizationKey variable with an empty string

`[BraintreeSDK] ERROR Invalid client token. Please ensure your server is generating a valid Braintree ClientToken. [BraintreeSDK] ERROR BTClient could not initialize because the provided clientToken was invalid flutter_braintree/FlutterBraintreeCustomPlugin.swift:77: Fatal error: Unexpectedly found nil while unwrapping an Optional value flutter_braintree/FlutterBraintreeCustomPlugin.swift:77: Fatal error: Unexpectedly found nil while unwrapping an Optional value

  • thread #1, queue = 'com.apple.main-thread', stop reason = Fatal error: Unexpectedly found nil while unwrapping an Optional value frame #0: 0x000000018518debc libswiftCore.dylib_swift_runtime_on_report libswiftCore.dylib: -> 0x18518debc <+0>: ret
    libswiftCore.dylib: 0x18518dec0 <+0>: b 0x18518debc ; _swift_runtime_on_report libswiftCore.dylib: 0x18518dec4 <+0>: adrp x8, 461960 0x18518dec8 <+4>: ldrb w0, [x8, #0x2cc] Target 0: (Runner) stopped. Lost connection to device. the Dart compiler exited unexpectedly.`

Flutter Channel stable, 3.16.7 Xcode 14.3

intsitesdotru avatar Mar 06 '24 05:03 intsitesdotru

screen

intsitesdotru avatar Mar 06 '24 06:03 intsitesdotru

Can you share a bit of code involving tokenization ?

BunnyBuddy avatar Mar 06 '24 07:03 BunnyBuddy

The crash is repeated in the this project example if you make an empty line in tokenizationKey variable. Thank you. https://github.com/pikaju/flutter-braintree/blob/main/example/lib/main.dart#L106

screen2

intsitesdotru avatar Mar 06 '24 09:03 intsitesdotru

Put it in a try/catch block and make sure the tokenizationKey is not empty. Handle the condition in case it doesn't work as expected. Something like this,

try { result = await Braintree.tokenizeCreditCard( tokenizationKey, request, ); log('nonce -> ${result.nonce}'); } catch (e) { Get.back(); debuPrint("ERROR TOKENIZATION -> ${e.toString()}"); return {"status": false, "message": "Transaction declined"}; }

BunnyBuddy avatar Mar 06 '24 11:03 BunnyBuddy

try/catch won't help, error from native code doesn't pop up to dart code. tokenizationKey may be some non-empty string, but you will get the crash. if you ever had a valid token, but then it suddenly became invalid, then you will get the crash.

intsitesdotru avatar Mar 06 '24 16:03 intsitesdotru