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