1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Settings_API' ) ) {
return;
}
class WC_Braintree_API_Settings extends WC_Braintree_Settings_API {
public function __construct() {
$this->id = 'braintree_api';
$this->tab_title = __( 'API Settings', 'woo-payment-gateway' );
add_action( 'woocommerce_update_options_checkout_' . $this->id, array( $this, 'process_admin_options' ) );
parent::__construct();
}
public function init_form_fields() {
$this->form_fields = apply_filters( 'wc_braintree_api_form_fields', include 'api-settings.php' );
}
public function process_admin_options() {
$settings = $this->settings;
parent::process_admin_options();
$this->fetch_merchant_accounts( $settings );
}
public function fetch_merchant_accounts( $settings = array() ) {
$env = $this->get_option( 'environment' );
$old_keys = array( $settings["{$env}_public_key"], $settings["{$env}_private_key"], $settings["{$env}_merchant_id"] );
$new_keys = array( $this->settings["{$env}_public_key"], $this->settings["{$env}_private_key"], $this->settings["{$env}_merchant_id"] );
$old_hash = implode( '_', $old_keys );
$new_hash = implode( '_', $new_keys );
if ( $old_hash !== $new_hash && ! in_array( "", $new_keys, true ) ) {
braintree()->rest_api->force( true );
rest_get_server();
$request = new WP_REST_Request( 'POST', '/wc-braintree/v1/merchant-accounts' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body_params( array( 'environment' => $env ) );
rest_do_request( $request );
}
}
public function get_localized_params() {
return array_merge(
parent::get_localized_params(),
array(
'routes' => array( 'connection_test' => braintree()->rest_api->settings->rest_url() . 'connection-test' ),
)
);
}
}