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 |