class-wc-rest-payments-documents-controller.php
4.44 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Class WC_REST_Payments_Documents_Controller
*
* @package WooCommerce\Payments\Admin
*/
use WCPay\Exceptions\API_Exception;
defined( 'ABSPATH' ) || exit;
/**
* REST controller for documents.
*/
class WC_REST_Payments_Documents_Controller extends WC_Payments_REST_Controller {
/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'payments/documents';
/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_documents' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/summary',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_documents_summary' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<document_id>[\w-]+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_document' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
}
/**
* Retrieve documents to respond with via API.
*
* @param WP_REST_Request $request Full data about the request.
*/
public function get_documents( $request ) {
$page = (int) $request->get_param( 'page' );
$page_size = (int) $request->get_param( 'pagesize' );
$sort = $request->get_param( 'sort' );
$direction = $request->get_param( 'direction' );
$filters = $this->get_documents_filters( $request );
return $this->forward_request( 'list_documents', [ $page, $page_size, $sort, $direction, $filters ] );
}
/**
* Retrieve documents summary to respond with via API.
*
* @param WP_REST_Request $request Full data about the request.
*/
public function get_documents_summary( $request ) {
$filters = $this->get_documents_filters( $request );
return $this->forward_request( 'get_documents_summary', [ $filters ] );
}
/**
* Retrieve and serve a document for API requests.
* This method serves the document directly and halts execution, skipping the REST return
* and preventing additional data to be sent.
*
* @param WP_REST_Request $request Full data about the request.
*/
public function get_document( $request ) {
$document_id = $request->get_param( 'document_id' );
try {
$response = $this->api_client->get_document( $document_id );
} catch ( API_Exception $e ) {
$message = sprintf(
/* translators: %1: The document ID. %2: The error message.*/
esc_html__( 'There was an error accessing document %1$s. %2$s', 'woocommerce-payments' ),
$document_id,
$e->getMessage()
);
wp_die( esc_html( $message ), '', (int) $e->get_http_code() );
}
// WooCommerce core only includes Tracks in admin, not the REST API, so we need to use this wc_admin method
// that includes WC_Tracks in case it's not loaded.
if ( function_exists( 'wc_admin_record_tracks_event' ) ) {
wc_admin_record_tracks_event(
'wcpay_document_downloaded',
[
'document_id' => $document_id,
'mode' => WC_Payments::get_gateway()->is_in_test_mode() ? 'test' : 'live',
]
);
}
// Set the headers to match what was returned from the server.
if ( ! headers_sent() ) {
nocache_headers();
status_header( $response['response']['code'], $response['response']['message'] ?? '' );
header( 'Content-Type: ' . $response['headers']['content-type'] );
header( 'Content-Disposition: ' . $response['headers']['content-disposition'] ?? '' );
}
// We should output the server's file without escaping.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $response['body'];
exit;
}
/**
* Extract documents filters from request
*
* @param WP_REST_Request $request Full data about the request.
*/
private function get_documents_filters( $request ) {
return array_filter(
[
'match' => $request->get_param( 'match' ),
'date_before' => $request->get_param( 'date_before' ),
'date_after' => $request->get_param( 'date_after' ),
'date_between' => $request->get_param( 'date_between' ),
'type_is' => $request->get_param( 'type_is' ),
'type_is_not' => $request->get_param( 'type_is_not' ),
],
static function ( $filter ) {
return null !== $filter;
}
);
}
}