pikaju / flutter-braintree

Flutter plugin that wraps the native Braintree SDKs. Enables payments with credit cards, PayPal, Google Pay and more.
https://pub.dev/packages/flutter_braintree
MIT License
64 stars 126 forks source link

Invalid client token crash, when tokenizeCreditCard call #152

Open intsitesdotru opened 7 months ago

intsitesdotru commented 7 months ago

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

intsitesdotru commented 7 months ago
screen
BunnyBuddy commented 7 months ago

Can you share a bit of code involving tokenization ?

intsitesdotru commented 7 months ago

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
BunnyBuddy commented 7 months ago

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"}; }

intsitesdotru commented 7 months ago

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.