← All changes
|
includes/Services/Providers/Printnode_Adapter.php
+61
-0
1.10.4
→
1.10.18
View file →
| @@ -7,8 +7,11 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace WCPOS\WooCommercePOS\Services\Providers; |
| 9 | 9 | |
| 10 | 10 | use WCPOS\WooCommercePOS\Interfaces\Push_Provider_Adapter_Interface; |
| 11 | +use WCPOS\WooCommercePOS\Logger; | |
| 12 | +use WP_Error; | |
| 13 | +use WP_REST_Response; | |
| 11 | 14 | use WCPOS\WooCommercePOS\Services\Pdf_Renderer; |
| 12 | 15 | use WCPOS\WooCommercePOS\Services\Print_Job_Service; |
| 13 | 16 | use WCPOS\WooCommercePOS\Services\PrintNode_Client; |
| 14 | 17 | |
| @@ -180,6 +183,64 @@ | ||
| 180 | 183 | private function drawer_kick_bytes( string $connector ): string { |
| 181 | 184 | $pin = 'pin5' === Print_Job_Service::normalize_drawer_connector( $connector ) ? "\x01" : "\x00"; |
| 182 | 185 | |
| 183 | 186 | return "\x1B\x70" . $pin . "\x19\xFA"; |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Submit a diagnostic PDF to a PrintNode printer. | |
| 191 | + * | |
| 192 | + * @param array $printer Registered PrintNode printer. | |
| 193 | + * | |
| 194 | + * @return \WP_REST_Response|WP_Error | |
| 195 | + */ | |
| 196 | + public function test_print( array $printer ) { | |
| 197 | + $api_key = (string) ( $printer['printnode_api_key'] ?? '' ); | |
| 198 | + $pn_printer_id = (int) ( $printer['printnode_printer_id'] ?? 0 ); | |
| 199 | + if ( '' === $api_key || 0 === $pn_printer_id ) { | |
| 200 | + return new WP_Error( | |
| 201 | + 'wcpos_print_job_printnode_unconfigured', | |
| 202 | + __( 'This PrintNode printer is missing its API key or printer id.', 'woocommerce-pos' ), | |
| 203 | + array( 'status' => 400 ) | |
| 204 | + ); | |
| 205 | + } | |
| 206 | + | |
| 207 | + try { | |
| 208 | + $pdf = base64_decode( $this->diagnostic( (string) $printer['name'] )['payload'], true ); | |
| 209 | + } catch ( \Throwable $e ) { | |
| 210 | + // Defense in depth: a Dompdf/font-cache/temp-dir failure must not | |
| 211 | + // surface as an uncaught 500. Mirror the render_payload() guard. | |
| 212 | + Logger::log( 'Cloud print: PrintNode diagnostic PDF render failed: ' . $e->getMessage() ); | |
| 213 | + | |
| 214 | + return new WP_Error( | |
| 215 | + 'wcpos_print_job_diagnostic_failed', | |
| 216 | + __( 'Could not generate the test print.', 'woocommerce-pos' ), | |
| 217 | + array( 'status' => 500 ) | |
| 218 | + ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + $result = ( new PrintNode_Client( $api_key ) )->submit_job( | |
| 222 | + $pn_printer_id, | |
| 223 | + 'WCPOS Test Print', | |
| 224 | + 'pdf_base64', | |
| 225 | + base64_encode( $pdf ) | |
| 226 | + ); | |
| 227 | + | |
| 228 | + if ( is_wp_error( $result ) ) { | |
| 229 | + return new WP_Error( | |
| 230 | + 'wcpos_print_job_printnode_failed', | |
| 231 | + $result->get_error_message(), | |
| 232 | + array( 'status' => 502 ) | |
| 233 | + ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + return new WP_REST_Response( | |
| 237 | + array( | |
| 238 | + 'submitted' => true, | |
| 239 | + 'external_provider' => 'printnode', | |
| 240 | + 'external_job_id' => (string) $result['id'], | |
| 241 | + 'external_state' => 'submitted', | |
| 242 | + ), | |
| 243 | + 201 | |
| 244 | + ); | |
| 184 | 245 | } |
| 185 | 246 | } |