PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.15
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.15
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / API / Receipts_Controller.php

Receipts_Controller.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.15, at includes/API/Receipts_Controller.php

232 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Receipts REST controller.
4 *
5 * @package WCPOS\WooCommercePOS\API
6 */
7
8 namespace WCPOS\WooCommercePOS\API;
9
10 use WCPOS\WooCommercePOS\Logger;
11 use WCPOS\WooCommercePOS\Services\Print_Job_Service;
12 use WCPOS\WooCommercePOS\Services\Receipt_Data_Builder;
13 use WCPOS\WooCommercePOS\Services\Receipt_Snapshot_Store;
14 use WCPOS\WooCommercePOS\Services\Fiscal_Receipt_Service;
15 use WCPOS\WooCommercePOS\Services\Template_Pdf_Service;
16 use WP_Error;
17 use WP_REST_Controller;
18 use WP_REST_Request;
19 use WP_REST_Server;
20
21 use const WCPOS\WooCommercePOS\SHORT_NAME;
22
23 /**
24 * Receipts_Controller class.
25 */
26 class Receipts_Controller extends WP_REST_Controller {
27 /**
28 * Endpoint namespace.
29 *
30 * @var string
31 */
32 protected $namespace = SHORT_NAME . '/v1';
33
34 /**
35 * Route base.
36 *
37 * @var string
38 */
39 protected $rest_base = 'receipts';
40
41 /**
42 * Register routes.
43 */
44 public function register_routes(): void {
45 register_rest_route(
46 $this->namespace,
47 '/' . $this->rest_base . '/(?P<order_id>[\\d]+)',
48 array(
49 'methods' => WP_REST_Server::READABLE,
50 'callback' => array( $this, 'get_item' ),
51 'permission_callback' => array( $this, 'get_item_permissions_check' ),
52 'args' => array(
53 'order_id' => array(
54 'type' => 'integer',
55 'required' => true,
56 'sanitize_callback' => 'absint',
57 ),
58 'mode' => array(
59 'type' => 'string',
60 'required' => false,
61 'enum' => array( 'fiscal', 'live' ),
62 'sanitize_callback' => 'sanitize_text_field',
63 ),
64 ),
65 )
66 );
67
68 register_rest_route(
69 $this->namespace,
70 '/' . $this->rest_base . '/(?P<order_id>[\\d]+)/pdf',
71 array(
72 'methods' => WP_REST_Server::READABLE,
73 'callback' => array( $this, 'get_pdf' ),
74 'permission_callback' => array( $this, 'get_item_permissions_check' ),
75 'args' => array(
76 'order_id' => array(
77 'type' => 'integer',
78 'required' => true,
79 'sanitize_callback' => 'absint',
80 ),
81 'template_id' => array(
82 'type' => 'string',
83 'required' => true,
84 'sanitize_callback' => 'sanitize_text_field',
85 ),
86 ),
87 )
88 );
89 }
90
91 /**
92 * Get receipt payload for an order.
93 *
94 * @param WP_REST_Request $request Request object.
95 *
96 * @return array|WP_Error
97 */
98 public function get_item( $request ) {
99 $order_id = (int) $request->get_param( 'order_id' );
100 $order = wc_get_order( $order_id );
101
102 if ( ! $order ) {
103 return new WP_Error(
104 'wcpos_receipt_invalid_order',
105 /* translators: REST API schema field label or error message. */
106 __( 'Invalid order.', 'woocommerce-pos' ),
107 array( 'status' => 404 )
108 );
109 }
110
111 $snapshot_store = Receipt_Snapshot_Store::instance();
112 $requested_mode = $request->get_param( 'mode' );
113 if ( null !== $requested_mode && ! \in_array( $requested_mode, array( 'fiscal', 'live' ), true ) ) {
114 return new WP_Error(
115 'wcpos_receipt_invalid_mode',
116 /* translators: REST API schema field label or error message. */
117 __( 'Invalid receipt mode.', 'woocommerce-pos' ),
118 array( 'status' => 400 )
119 );
120 }
121
122 $mode = $snapshot_store->resolve_mode( $requested_mode );
123 $payload = null;
124
125 if ( 'fiscal' === $mode ) {
126 $payload = $snapshot_store->get_snapshot( $order_id );
127 if ( ! $payload ) {
128 return new WP_Error(
129 'wcpos_receipt_snapshot_missing',
130 __( 'No fiscal snapshot found for this order.', 'woocommerce-pos' ),
131 array( 'status' => 404 )
132 );
133 }
134 } else {
135 $payload = ( new Receipt_Data_Builder() )->build( $order, 'live' );
136 }
137
138 return array(
139 'order_id' => $order_id,
140 'mode' => $mode,
141 'has_snapshot' => $snapshot_store->has_snapshot( $order_id ),
142 'submission_status' => ( new Fiscal_Receipt_Service() )->get_submission_status( $order_id ),
143 'data' => $payload,
144 );
145 }
146
147 /**
148 * Get receipt PDF bytes for an order.
149 *
150 * @param WP_REST_Request $request Request object.
151 *
152 * @return Raw_Response|WP_Error
153 */
154 public function get_pdf( $request ) {
155 $order = wc_get_order( (int) $request['order_id'] );
156 if ( ! $order ) {
157 return new WP_Error(
158 'wcpos_receipt_order_not_found',
159 __( 'Order not found.', 'woocommerce-pos' ),
160 array( 'status' => 404 )
161 );
162 }
163
164 $template_id = trim( (string) $request->get_param( 'template_id' ) );
165 if ( '' === $template_id ) {
166 return new WP_Error(
167 'wcpos_receipt_missing_template',
168 __( 'A template_id is required.', 'woocommerce-pos' ),
169 array( 'status' => 400 )
170 );
171 }
172
173 $template = Print_Job_Service::load_template( $template_id );
174 if ( null === $template ) {
175 return new WP_Error(
176 'wcpos_receipt_template_not_found',
177 __( 'Template not found.', 'woocommerce-pos' ),
178 array( 'status' => 404 )
179 );
180 }
181
182 try {
183 $pdf = ( new Template_Pdf_Service() )->render( $template, $order );
184 } catch ( \Throwable $e ) {
185 Logger::log( sprintf( 'Receipt PDF render failed for order %d: %s', $order->get_id(), $e->getMessage() ) );
186
187 return new WP_Error(
188 'wcpos_receipt_pdf_failed',
189 __( 'Could not generate the receipt PDF.', 'woocommerce-pos' ),
190 array( 'status' => 500 )
191 );
192 }
193
194 if ( '' === $pdf ) {
195 return new WP_Error(
196 'wcpos_receipt_pdf_empty',
197 __( 'Could not generate the receipt PDF.', 'woocommerce-pos' ),
198 array( 'status' => 500 )
199 );
200 }
201
202 return Raw_Response::serve(
203 $pdf,
204 'application/pdf',
205 array(
206 'Content-Disposition' => sprintf( 'attachment; filename="receipt-%d.pdf"', $order->get_id() ),
207 'Content-Length' => (string) \strlen( $pdf ),
208 'Cache-Control' => 'no-store',
209 )
210 );
211 }
212
213 /**
214 * Permissions check.
215 *
216 * @param WP_REST_Request $request Request object.
217 *
218 * @return bool|WP_Error
219 */
220 public function get_item_permissions_check( $request ) {
221 if ( ! current_user_can( 'access_woocommerce_pos' ) ) {
222 return new WP_Error(
223 'wcpos_rest_insufficient_permissions',
224 __( 'Sorry, you cannot view receipts.', 'woocommerce-pos' ),
225 array( 'status' => 403 )
226 );
227 }
228
229 return true;
230 }
231 }
232