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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_Settings extends WC_Braintree_Rest_Controller {
protected $namespace = 'admin/settings';
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'connection-test',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'connection_test' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' )
)
);
register_rest_route( $this->rest_uri(), '/payment_gateways/(?P<id>[\w-]+)',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_gateway_settings' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' )
) );
}
public function connection_test( $request ) {
$env = $request->get_param( 'environment' );
$conn_settings = array(
'environment' => $env,
'merchantId' => $request->get_param( 'merchant_id' ),
'publicKey' => $request->get_param( 'public_key' ),
'privateKey' => $request->get_param( 'private_key' ),
);
try {
$gateway = new \Braintree\Gateway( $conn_settings );
$gateway->clientToken()->generate();
return rest_ensure_response(
array(
'success' => true,
'message' => sprintf(
sprintf(
__(
'%1$s connection test was successful.',
'woo-payment-gateway'
),
$env == 'sandbox' ? __( 'Sandbox', 'woo-payment-gateway' ) : __( 'Production', 'woo-payment-gateway' )
)
),
)
);
} catch ( Exception $e ) {
if ( $e instanceof \Braintree\Exception\Configuration ) {
$error = __( 'A Configuration exception was thrown. This error typcically happens when you have entered your API keys incorrectly or have left a value blank.', 'woo-payment-gateway' );
}
if ( $e instanceof \Braintree\Exception\Authentication ) {
$error = __( 'An Authentication exception was thrown. This error typcically happens when you have entered your API keys incorrectly', 'woo-payment-gateway' );
}
if ( $e instanceof \Braintree\Exception\Authorization ) {
$error = __( 'An Authorization exception was thrown. This error typically happens when you have entered an incorrect API key. Double check that you entered your API keys in the correct fields. Also, make sure you didn\'t confuse your Merchant ID with a Merchant Account ID value.', 'woo-payment-gateway' );
}
wc_braintree_log_error( sprintf( 'Error during connection test. Exception: %s', $e->getMessage() ) );
return new WP_Error(
'connection-error',
$error,
array(
'status' => 200,
'message' => $error,
)
);
}
}
public function update_gateway_settings( $request ) {
$payment_method = WC()->payment_gateways()->payment_gateways()[ $request->get_param( 'id' ) ];
$payment_method->init_form_fields();
$payment_method->init_settings();
$settings = $payment_method->settings;
try {
foreach ( $payment_method->get_form_fields() as $key => $field ) {
if ( isset( $request['settings'][ $key ] ) ) {
$post_data = array( $payment_method->get_field_key( $key ) => $request['settings'][ $key ] );
$settings[ $key ] = $payment_method->get_field_value( $key, $field, $post_data );
}
}
update_option( $payment_method->get_option_key(), $settings );
return rest_ensure_response( array() );
} catch ( Exception $e ) {
return new WP_Error( 'settings-error', $e->getMessage(), array( 'status' => 200 ) );
}
}
}