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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ReceiptRendering / ReceiptRenderingRestController.php
woocommerce / src / Internal / ReceiptRendering Last commit date
Templates 3 months ago ReceiptRenderingEngine.php 2 months ago ReceiptRenderingRestController.php 1 year ago
ReceiptRenderingRestController.php
202 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Internal\ReceiptRendering;
4
5 use Automattic\WooCommerce\Internal\TransientFiles\TransientFilesEngine;
6 use \WP_REST_Server;
7 use \WP_REST_Request;
8 use \WP_Error;
9 use Automattic\WooCommerce\Internal\RestApiControllerBase;
10
11 /**
12 * Controller for the REST endpoints associated to the receipt rendering engine.
13 * The endpoints require the read_shop_order capability for the order at hand.
14 */
15 class ReceiptRenderingRestController extends RestApiControllerBase {
16
17 /**
18 * Get the WooCommerce REST API namespace for the class.
19 *
20 * @return string
21 */
22 protected function get_rest_api_namespace(): string {
23 return 'order-receipts';
24 }
25
26 /**
27 * Register the REST API endpoints handled by this controller.
28 */
29 public function register_routes() {
30 register_rest_route(
31 $this->route_namespace,
32 '/orders/(?P<id>[\d]+)/receipt',
33 array(
34 array(
35 'methods' => WP_REST_Server::CREATABLE,
36 'callback' => fn( $request ) => $this->run( $request, 'create_order_receipt' ),
37 'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_shop_order', $request->get_param( 'id' ) ),
38 'args' => $this->get_args_for_create_order_receipt(),
39 'schema' => $this->get_schema_for_get_and_post_order_receipt(),
40 ),
41 )
42 );
43
44 register_rest_route(
45 $this->route_namespace,
46 '/orders/(?P<id>[\d]+)/receipt',
47 array(
48 array(
49 'methods' => WP_REST_Server::READABLE,
50 'callback' => fn( $request ) => $this->run( $request, 'get_order_receipt' ),
51 'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_shop_order', $request->get_param( 'id' ) ),
52 'args' => $this->get_args_for_get_order_receipt(),
53 'schema' => $this->get_schema_for_get_and_post_order_receipt(),
54 ),
55 )
56 );
57
58 }
59
60 /**
61 * Handle the GET /orders/id/receipt:
62 *
63 * Return the data for a receipt if it exists, or a 404 error if it doesn't.
64 *
65 * @param WP_REST_Request $request The received request.
66 * @return array|WP_Error
67 */
68 public function get_order_receipt( WP_REST_Request $request ) {
69 $order_id = $request->get_param( 'id' );
70 $filename = wc_get_container()->get( ReceiptRenderingEngine::class )->get_existing_receipt( $order_id );
71
72 return is_null( $filename ) ?
73 new WP_Error( 'woocommerce_rest_not_found', __( 'Receipt not found', 'woocommerce' ), array( 'status' => 404 ) ) :
74 $this->get_response_for_file( $filename );
75 }
76
77 /**
78 * Handle the POST /orders/id/receipt:
79 *
80 * Return the data for a receipt if it exists, or create a new receipt and return its data otherwise.
81 *
82 * Optional query string arguments:
83 *
84 * expiration_date: formatted as yyyy-mm-dd.
85 * expiration_days: a number, 0 is today, 1 is tomorrow, etc.
86 * force_new: defaults to false, if true, create a new receipt even if one already exists for the order.
87 *
88 * If neither expiration_date nor expiration_days are supplied, the default is expiration_days = 1.
89 *
90 * @param WP_REST_Request $request The received request.
91 * @return array|WP_Error Request response or an error.
92 */
93 public function create_order_receipt( WP_REST_Request $request ) {
94 $expiration_date =
95 $request->get_param( 'expiration_date' ) ??
96 gmdate( 'Y-m-d', strtotime( "+{$request->get_param('expiration_days')} days" ) );
97
98 $order_id = $request->get_param( 'id' );
99
100 $filename = wc_get_container()->get( ReceiptRenderingEngine::class )->generate_receipt( $order_id, $expiration_date, $request->get_param( 'force_new' ) );
101
102 return is_null( $filename ) ?
103 new WP_Error( 'woocommerce_rest_not_found', __( 'Order not found', 'woocommerce' ), array( 'status' => 404 ) ) :
104 $this->get_response_for_file( $filename );
105 }
106
107 /**
108 * Formats the response for both the GET and POST endpoints.
109 *
110 * @param string $filename The filename to return the information for.
111 * @return array The data for the actual response to be returned.
112 */
113 private function get_response_for_file( string $filename ): array {
114 $expiration_date = TransientFilesEngine::get_expiration_date( $filename );
115 $public_url = wc_get_container()->get( TransientFilesEngine::class )->get_public_url( $filename );
116
117 return array(
118 'receipt_url' => $public_url,
119 'expiration_date' => $expiration_date,
120 );
121 }
122
123 /**
124 * Get the accepted arguments for the GET request.
125 *
126 * @return array[] The accepted arguments for the GET request.
127 */
128 private function get_args_for_get_order_receipt(): array {
129 return array(
130 'id' => array(
131 'description' => __( 'Unique identifier of the order.', 'woocommerce' ),
132 'type' => 'integer',
133 'context' => array( 'view', 'edit' ),
134 'readonly' => true,
135 ),
136 );
137 }
138
139 /**
140 * Get the schema for both the GET and the POST requests.
141 *
142 * @return array[]
143 */
144 private function get_schema_for_get_and_post_order_receipt(): array {
145 $schema = $this->get_base_schema();
146 $schema['properties'] = array(
147 'receipt_url' => array(
148 'description' => __( 'Public url of the receipt.', 'woocommerce' ),
149 'type' => 'string',
150 'context' => array( 'view', 'edit' ),
151 'readonly' => true,
152 ),
153 'expiration_date' => array(
154 'description' => __( 'Expiration date of the receipt, formatted as yyyy-mm-dd.', 'woocommerce' ),
155 'type' => 'string',
156 'context' => array( 'view', 'edit' ),
157 'readonly' => true,
158 ),
159 );
160
161 return $schema;
162 }
163
164 /**
165 * Get the accepted arguments for the POST request.
166 *
167 * @return array[]
168 */
169 private function get_args_for_create_order_receipt(): array {
170 return array(
171 'id' => array(
172 'description' => __( 'Unique identifier of the order.', 'woocommerce' ),
173 'type' => 'integer',
174 'context' => array( 'view', 'edit' ),
175 'readonly' => true,
176 ),
177 'expiration_date' => array(
178 'description' => __( 'Expiration date formatted as yyyy-mm-dd.', 'woocommerce' ),
179 'type' => 'string',
180 'context' => array( 'view', 'edit' ),
181 'readonly' => true,
182 'default' => null,
183 ),
184 'expiration_days' => array(
185 'description' => __( 'Number of days to be added to the current date to get the expiration date.', 'woocommerce' ),
186 'type' => 'integer',
187 'context' => array( 'view', 'edit' ),
188 'readonly' => true,
189 'default' => 1,
190 ),
191 'force_new' => array(
192 'description' => __( 'True to force the creation of a new receipt even if one already exists and has not expired yet.', 'woocommerce' ),
193 'type' => 'boolean',
194 'required' => false,
195 'context' => array( 'view', 'edit' ),
196 'readonly' => true,
197 'default' => false,
198 ),
199 );
200 }
201 }
202