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
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_Merchant_Accounts extends WC_Braintree_Rest_Controller {
protected $namespace = '';
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'merchant-accounts',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_merchant_accounts' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' ),
)
);
}
public function get_merchant_accounts( $request ) {
try {
$env = $request->get_param( 'environment' );
$gateway = braintree()->gateway( $env );
$accounts = $gateway->merchantAccount()->all();
$merchant_accounts = array();
$settings = braintree()->merchant_settings;
foreach ( $accounts as $account ) {
$merchant_accounts[ $account->currencyIsoCode ] = $account->id;
}
$settings->settings["{$env}_merchant_accounts"] = $merchant_accounts;
update_option( $settings->get_option_key(), $settings->settings );
return rest_ensure_response( $merchant_accounts );
} catch ( Exception $e ) {
return new WP_Error(
'merchant-account-error',
sprintf( 'Error fetching merchant accounts in %1$s. Reason: %2$s', $env, wc_braintree_errors_from_object( $e ) ),
array(
'status' => 200,
)
);
}
}
}