When I have multiple gateways available, and I switch from Braintree to my second gateway, I get the following error:
Uncaught TypeError: Cannot read property 'teardown' of undefined
at Object.detach (eval at globalEval (jquery.min.js?v=2.2.4:2), <anonymous>:47:36)
at Object.Drupal.detachBehaviors (drupal.js?v=8.3.5:220)
at Drupal.AjaxCommands.insert (ajax.js?v=8.3.5:1056)
at Drupal.Ajax.success (ajax.js?v=8.3.5:864)
at Object.success (ajax.js?v=8.3.5:505)
at Object.t.success (jquery.form.min.js?v=3.51:11)
at i (jquery.min.js?v=2.2.4:2)
at Object.fireWith [as resolveWith] (jquery.min.js?v=2.2.4:2)
at z (jquery.min.js?v=2.2.4:4)
at XMLHttpRequest.<anonymous> (jquery.min.js?v=2.2.4:4)
This happens in the `Payment Information` pane.
It looks like it happening when the behaviours are being detached from the Braintree form.
Comments
Comment #2
jafacakes2011 commentedEdit: adding the Organisation I work for to this issue
Comment #3
bojanz commentedWhich other gateways do you have on the form? Is it the same no matter from which one you switch?
Comment #4
jafacakes2011 commentedI was using the PayPal Gateway, but I also noticed it happened when I switched from `New Card` to a previously used card.
It only seems to occur when coming off the New Card form
Comment #5
mrhorse commentedLooking further into this I can see that on detach commerceBraintree.integration.teardown() is being called in commerce_braintree.form.js when changing payment method - at that moment in time the integration object is null so the call fails. I can't seem to spot where the integration object is added to the commerceBraintree object.
Changing commerceBraintree.integration.teardown() to commerceBraintree.hostedFieldsInstance.teardown() results in teardown executing successfully, but then receive the js error 'tokenize cannot becalled after teardown' on submit, regardless of which method I'm using.
Any pointers in the right direction would be greatly appreciated.
Thanks
Comment #6
mrhorse commentedThis patch changes teardown to act on the current hostingFieldsInstance instead of 'integration', as well as unbinding the submit handler on teardown which wasn't happening previously
Comment #7
mrhorse commentedUpdated patch to namespace the form submit handler - this way we don't blindly blanket unbind anything else bound to submit.
Comment #8
mglamanI think the more generic option is chosen as there will be support for PayPal via Braintree.
But how is this event triggered?
Comment #9
mrhorse commented1. In our case, with multiple payments in addition to braintree the integration object was never created, so teardown() could never be called.
2. It's a namespaced submit handler... in essence it's just a standard form submit handler. Namespacing it means I can remove specifically that handler instead of blanket removing all handlers attached to form submit.
Comment #10
mglamanGoing to sit down on this today. I feel some of the JS changes in https://www.drupal.org/node/2331119 may fix this as well. Will cherrypick as needed.
Comment #11
mglamanHere is a much simpler fix. The problem was that
teardownwas not a proper value. A fix is to ensure there is aimmplementationInstanceproperty. Using a generic property like this can then hold either the HostedField or PayPal (once implemented) and allow easy setup and teardown.Comment #12
mglamanAttaching much simpler patch.
Comment #13
mglamanComment #14
nikathoneCan confirm that this patch did fix the problem. so +1 for RTBC. A before patch screenshot error.
Comment #15
sophie.skUnfortunately, having applied this patch, I get the error:
uncaught exception: BraintreeError: tokenize cannot be called after teardown.Steps to reproduce:
1. Checkout and save card
2. Checkout again and switch from saved card to new card
3. Switch back from new card to saved card.
4. Try to "continue to review", see the error in the console.
Sorry, had to finish this comment quickly earlier so I left off a step by mistake.
Comment #16
nikathoneI will be re-rolling a new patch about https://www.drupal.org/node/2331119 and I am having the same error described in #15. This means we need to find a proper way to teardown and these both issues will have fix...
Comment #17
mglamanThanks Sophie.SK! We should try to fix the issue here first, which should improve our JavaScript and make implementing the PayPal items easier.
Comment #18
nikathoneLet put it back to needs work based on #15
Comment #19
mglamanmrhorse had this fix we discovered in #15. I'm just stupid and totally didn't understand namespaced event handling. I'd like to keep the teardown to `integration.teardown`, so my fix in #12 and then also original patch from #7.
Here is tweak. Authorship should go to mrhorse in #7.
Comment #20
nikathone#19 Looks good to me.

Comment #21
mglamanSophie.SK, mrhorse : with your RTBC blessing we can merge.
Comment #22
jafacakes2011 commentedThanks for this guys, just tested the patch for my issue and it works great, will leave this for mrhorse and Sophie.SK to respond.
Comment #23
sophie.skWorks perfectly for me! Thanks for pinging me to check this, and for the fix! Marking as RTBC.
(I would record a fancy gif, but I don't know how :-))
Comment #24
sophie.skUgh, just making sure that if I do get any kind of credit for testing a patch :D, it marks my organisation too. Not quite sure why d.o has done away with my settings for that.
Comment #25
mrhorse commentedYep works great for us! Go forth and use namespaced submit handlers :P
Thanks all
Comment #27
mglamanThank you, everyone!