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

jafacakes2011 created an issue. See original summary.

jafacakes2011’s picture

Edit: adding the Organisation I work for to this issue

bojanz’s picture

Which other gateways do you have on the form? Is it the same no matter from which one you switch?

jafacakes2011’s picture

I 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

mrhorse’s picture

Looking 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

mrhorse’s picture

Status: Active » Needs review
StatusFileSize
new496 bytes

This 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

mrhorse’s picture

StatusFileSize
new841 bytes

Updated patch to namespace the form submit handler - this way we don't blindly blanket unbind anything else bound to submit.

mglaman’s picture

  1. +++ b/js/commerce_braintree.form.js
    @@ -44,9 +44,10 @@
    -      commerceBraintree.integration.teardown();
    

    I think the more generic option is chosen as there will be support for PayPal via Braintree.

  2. +++ b/js/commerce_braintree.form.js
    @@ -44,9 +44,10 @@
    +      $form.off('submit.braintreeSubmit');
    
    @@ -79,7 +80,7 @@
    +        $form.on('submit.braintreeSubmit', function (event, options) {
    

    But how is this event triggered?

mrhorse’s picture

1. 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.

mglaman’s picture

Assigned: Unassigned » mglaman

Going 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.

mglaman’s picture

Here is a much simpler fix. The problem was that teardown was not a proper value. A fix is to ensure there is a immplementationInstance property. Using a generic property like this can then hold either the HostedField or PayPal (once implemented) and allow easy setup and teardown.

mglaman’s picture

StatusFileSize
new537 bytes

Attaching much simpler patch.

mglaman’s picture

Assigned: mglaman » Unassigned
nikathone’s picture

StatusFileSize
new205.58 KB

Can confirm that this patch did fix the problem. so +1 for RTBC. A before patch screenshot error.

sophie.sk’s picture

Unfortunately, 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.

nikathone’s picture

I 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...

mglaman’s picture

Thanks Sophie.SK! We should try to fix the issue here first, which should improve our JavaScript and make implementing the PayPal items easier.

nikathone’s picture

Status: Needs review » Needs work

Let put it back to needs work based on #15

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB

mrhorse 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.

nikathone’s picture

StatusFileSize
new390.05 KB

#19 Looks good to me.

mglaman’s picture

Sophie.SK, mrhorse : with your RTBC blessing we can merge.

jafacakes2011’s picture

Thanks for this guys, just tested the patch for my issue and it works great, will leave this for mrhorse and Sophie.SK to respond.

sophie.sk’s picture

Status: Needs review » Reviewed & tested by the community

Works 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 :-))

sophie.sk’s picture

Ugh, 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.

mrhorse’s picture

Yep works great for us! Go forth and use namespaced submit handlers :P

Thanks all

  • mglaman committed 2a35033 on 8.x-1.x authored by mrhorse
    Issue #2898723 by mglaman, mrhorse, nikathone, Sophie.SK: Can't switch...
mglaman’s picture

Status: Reviewed & tested by the community » Fixed

Thank you, everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.