• Resolved twisted1919

    (@twisted1919)


    Hi guys,

    So digging around i found out that i need to subscribe on the events produced by braintree ( https://developers.braintreepayments.com/reference/client-reference/javascript/v2/hosted-fields#events) basically i need to have a callback to see when the field changes (fieldStateChange event).

    Looking through the code, seems that WC_Braintree_Payment_Form::render_js() we can use a hook to be able to modify the $params array, something like:

    public function render_js() {
    
    		// defaults for both gateways
    		$params = array_merge( array(
    			'id'            => $this->get_gateway()->get_id(),
    			'id_dasherized' => $this->get_gateway()->get_id_dasherized(),
    			'debug'         => $this->get_gateway()->debug_checkout(),
    			'type'          => str_replace( '-', '_', $this->get_gateway()->get_payment_type() ),
    		), $this->get_payment_form_handler_js_params() );
    		
                    // add this
    		$params = apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_handler_js_params', $params );
    
    		$handler_class = $this->get_gateway()->is_credit_card_gateway() ? 'WC_Braintree_Credit_Card_Payment_Form_Handler' : 'WC_Braintree_PayPal_Payment_Form_Handler';
    
    		wc_enqueue_js( sprintf( 'window.wc_%1$s_handler = new %2$s( %3$s );', esc_js( $this->get_gateway()->get_id() ), $handler_class, json_encode( $params ) ) );
    	}

    Which would allow us to hook into all params and change them as we need.
    Next thing that comes into my mind is that if you pass the callbacks into an array and then to json_encode, it will quote them so they won’t execute (see https://stackoverflow.com/questions/1745248/php-json-encode-and-javascript-functions ) so we need also a way when we call
    wc_enqueue_js( sprintf( 'window.wc_%1$s_handler = new %2$s( %3$s );', esc_js( $this->get_gateway()->get_id() ), $handler_class, json_encode( $params ) ) );
    to avoid this issue.

    Any way we can make this happen? Do you have a github repository where i could possibly make a pull request with these additions, in case you don’t have enough time for it ?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can you add new filter hook?’ is closed to new replies.