class-wc-rest-payments-connection-tokens-controller.php
1.3 KB
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
/**
* Class WC_REST_Payments_Connection_Tokens_Controller
*
* @package WooCommerce\Payments\Admin
*/
defined( 'ABSPATH' ) || exit;
/**
* REST controller for terminal tokens.
*/
class WC_REST_Payments_Connection_Tokens_Controller extends WC_Payments_REST_Controller {
/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'payments/connection_tokens';
/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/payments/connection_tokens',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'create_token' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
}
/**
* Create a connection token via API.
*
* @param WP_REST_Request $request Full data about the request.
*/
public function create_token( $request ) {
$response = $this->forward_request( 'create_token', [ $request ] );
// As an aid to mobile clients, tuck in the test_mode flag in the response returned to the request.
if ( is_a( $response, 'WP_REST_Response' ) ) {
if ( property_exists( $response, 'data' ) ) {
if ( is_array( $response->data ) ) {
$response->data['test_mode'] = WC_Payments::get_gateway()->is_in_test_mode();
}
}
}
return $response;
}
}