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
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_3ds extends WC_Braintree_Rest_Controller {
protected $namespace = '3ds/';
public function __construct() {
}
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'vaulted_nonce',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_vaulted_nonce' ),
'permission_callback' => '__return_true',
)
);
}
public function get_vaulted_nonce( $request ) {
$response = new WP_REST_Response();
if ( isset( $request['token_id'] ) ) {
$token = WC_Payment_Tokens::get( $request['token_id'] )->get_token();
} else {
$token = $request->get_param( 'token' );
}
try {
$gateway = new \Braintree\Gateway( wc_braintree_connection_settings() );
$result = $gateway->paymentMethodNonce()->create( $token );
$response->set_data(
array(
'success' => true,
'data' => array(
'nonce' => $result->paymentMethodNonce->nonce,
'details' => $result->paymentMethodNonce->details,
),
)
);
$response->set_status( 200 );
} catch ( \Braintree\Exception $e ) {
$response->set_data(
array(
'success' => false,
'message' => wc_braintree_errors_from_object( $e ),
)
);
}
return $response;
}
}