← All changes
|
includes/Services/Cloud_Print_Submit_Service.php
+14
-119
1.9.16
→
1.10.20
View file →
| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | |
| 11 | 11 | namespace WCPOS\WooCommercePOS\Services; |
| 12 | 12 | |
| 13 | 13 | use WCPOS\WooCommercePOS\Logger; |
| 14 | +use WCPOS\WooCommercePOS\Interfaces\Push_Provider_Adapter_Interface; | |
| 14 | 15 | |
| 15 | 16 | /** |
| 16 | 17 | * Cloud_Print_Submit_Service class. |
| 17 | 18 | */ |
| @@ -93,144 +94,38 @@ | ||
| 93 | 94 | return; |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | $provider = (string) ( $printer['provider'] ?? '' ); |
| 97 | - if ( 'star-online' === $provider ) { | |
| 98 | - $this->submit_star_online( $job_id, $job, $printer ); | |
| 99 | - | |
| 100 | - return; | |
| 101 | - } | |
| 102 | - | |
| 103 | - if ( 'printnode' !== $provider ) { | |
| 98 | + $adapter = Provider::adapter( $provider ); | |
| 99 | + if ( ! $adapter instanceof Push_Provider_Adapter_Interface ) { | |
| 104 | 100 | $this->fail( $job_id, 'Cloud print: unsupported push provider for job.' ); |
| 105 | 101 | |
| 106 | 102 | return; |
| 107 | 103 | } |
| 108 | 104 | |
| 109 | - $api_key = (string) ( $printer['printnode_api_key'] ?? '' ); | |
| 110 | - $pn_printer_id = (int) ( $printer['printnode_printer_id'] ?? 0 ); | |
| 111 | - if ( '' === $api_key || 0 === $pn_printer_id ) { | |
| 112 | - $this->fail( $job_id, 'Cloud print: PrintNode printer is missing an API key or printer id.' ); | |
| 113 | - | |
| 114 | - return; | |
| 115 | - } | |
| 116 | - | |
| 117 | 105 | $payload = $this->jobs->render_payload( $job ); |
| 118 | - if ( '' === $payload ) { | |
| 119 | - $this->fail( $job_id, 'Cloud print: PrintNode job produced no printable content.' ); | |
| 106 | + $result = $adapter->submit( $printer, $job, $payload, $this->title_for( $job ) ); | |
| 107 | + if ( ! $result['success'] ) { | |
| 108 | + if ( $result['retryable'] ) { | |
| 109 | + $this->handle_submit_error( $job_id, $result['error'] ); | |
| 110 | + } else { | |
| 111 | + $this->fail( $job_id, $result['error'] ); | |
| 112 | + } | |
| 120 | 113 | |
| 121 | 114 | return; |
| 122 | 115 | } |
| 123 | 116 | |
| 124 | - $content_type = 'escpos' === $job['pn_kind'] ? 'raw_base64' : 'pdf_base64'; | |
| 125 | - $title = $this->title_for( $job ); | |
| 117 | + $this->jobs->record_external_submission( $job_id, $provider, $result['external_job_id'], 'submitted' ); | |
| 126 | 118 | |
| 127 | - $result = ( new PrintNode_Client( $api_key ) )->submit_job( | |
| 128 | - $pn_printer_id, | |
| 129 | - $title, | |
| 130 | - $content_type, | |
| 131 | - base64_encode( $payload ) | |
| 132 | - ); | |
| 133 | - | |
| 134 | - if ( is_wp_error( $result ) ) { | |
| 135 | - $this->handle_submit_error( $job_id, $result->get_error_message() ); | |
| 136 | - | |
| 137 | - return; | |
| 119 | + if ( '' !== $result['drawer_error'] ) { | |
| 120 | + update_post_meta( $job_id, Print_Job_Service::META_DRAWER_ERROR, sanitize_text_field( $result['drawer_error'] ) ); | |
| 121 | + Logger::log( sprintf( 'Cloud print: PrintNode drawer kick failed for job %d after receipt submission.', $job_id ) ); | |
| 138 | 122 | } |
| 139 | 123 | |
| 140 | - $this->jobs->record_external_submission( $job_id, 'printnode', (string) $result['id'], 'submitted' ); | |
| 141 | - | |
| 142 | - if ( 'pdf' === $job['pn_kind'] && ! empty( $job['auto_open_drawer'] ) ) { | |
| 143 | - $drawer_result = $this->submit_printnode_drawer_kick( $api_key, $pn_printer_id, $job ); | |
| 144 | - if ( is_wp_error( $drawer_result ) ) { | |
| 145 | - update_post_meta( $job_id, Print_Job_Service::META_DRAWER_ERROR, sanitize_text_field( $drawer_result->get_error_message() ) ); | |
| 146 | - Logger::log( sprintf( 'Cloud print: PrintNode drawer kick failed for job %d after receipt submission.', $job_id ) ); | |
| 147 | - } | |
| 148 | - } | |
| 149 | - | |
| 150 | 124 | $this->jobs->set_status( $job_id, Print_Job_Service::STATUS_PRINTED ); |
| 151 | 125 | } finally { |
| 152 | 126 | $this->jobs->release_lifecycle_lock( $job_id ); |
| 153 | 127 | } |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * Submit a best-effort raw ESC/POS drawer kick for a PrintNode PDF job. | |
| 158 | - * | |
| 159 | - * @param string $api_key PrintNode API key. | |
| 160 | - * @param int $pn_printer_id PrintNode printer id. | |
| 161 | - * @param array $job Job data. | |
| 162 | - * | |
| 163 | - * @return array|\WP_Error | |
| 164 | - */ | |
| 165 | - private function submit_printnode_drawer_kick( string $api_key, int $pn_printer_id, array $job ) { | |
| 166 | - $title = $this->title_for( $job ) . ' Cash Drawer'; | |
| 167 | - | |
| 168 | - return ( new PrintNode_Client( $api_key ) )->submit_job( | |
| 169 | - $pn_printer_id, | |
| 170 | - $title, | |
| 171 | - 'raw_base64', | |
| 172 | - base64_encode( $this->drawer_kick_bytes( (string) ( $job['drawer_connector'] ?? 'pin2' ) ) ) | |
| 173 | - ); | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Build ESC/POS drawer kick bytes for PrintNode raw jobs. | |
| 178 | - * | |
| 179 | - * @param string $connector pin2 or pin5. | |
| 180 | - * | |
| 181 | - * @return string | |
| 182 | - */ | |
| 183 | - private function drawer_kick_bytes( string $connector ): string { | |
| 184 | - $connector = Print_Job_Service::normalize_drawer_connector( $connector ); | |
| 185 | - $pin = 'pin5' === $connector ? "\x01" : "\x00"; | |
| 186 | - | |
| 187 | - return "\x1B\x70" . $pin . "\x19\xFA"; | |
| 188 | - } | |
| 189 | - | |
| 190 | - /** | |
| 191 | - * Submit a queued Star Online job: render Star markup and POST it to stario.online. | |
| 192 | - * | |
| 193 | - * @param int $job_id Job id. | |
| 194 | - * @param array $job Job array. | |
| 195 | - * @param array $printer Registered star-online printer. | |
| 196 | - */ | |
| 197 | - private function submit_star_online( int $job_id, array $job, array $printer ): void { | |
| 198 | - $api_key = (string) ( $printer['star_api_key'] ?? '' ); | |
| 199 | - $url = (string) ( $printer['star_cloudprnt_url'] ?? '' ); | |
| 200 | - $device_id = (string) ( $printer['star_device_id'] ?? '' ); | |
| 201 | - $api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url ); | |
| 202 | - $group = Star_Online_Client::group_from_cloudprnt_url( $url ); | |
| 203 | - | |
| 204 | - if ( '' === $api_key || null === $api_base || '' === $group || '' === $device_id ) { | |
| 205 | - $this->fail( $job_id, 'Cloud print: Star Online printer is misconfigured.' ); | |
| 206 | - | |
| 207 | - return; | |
| 208 | - } | |
| 209 | - | |
| 210 | - $payload = $this->jobs->render_payload( $job ); | |
| 211 | - if ( '' === $payload ) { | |
| 212 | - $this->fail( $job_id, 'Cloud print: Star Online job produced no printable content.' ); | |
| 213 | - | |
| 214 | - return; | |
| 215 | - } | |
| 216 | - | |
| 217 | - $result = ( new Star_Online_Client( $api_base, $api_key ) )->submit_job( | |
| 218 | - $group, | |
| 219 | - $device_id, | |
| 220 | - $this->title_for( $job ), | |
| 221 | - 'text/vnd.star.markup', | |
| 222 | - $payload | |
| 223 | - ); | |
| 224 | - | |
| 225 | - if ( is_wp_error( $result ) ) { | |
| 226 | - $this->handle_submit_error( $job_id, $result->get_error_message() ); | |
| 227 | - | |
| 228 | - return; | |
| 229 | - } | |
| 230 | - | |
| 231 | - $this->jobs->record_external_submission( $job_id, 'star-online', (string) $result['id'], 'submitted' ); | |
| 232 | - $this->jobs->set_status( $job_id, Print_Job_Service::STATUS_PRINTED ); | |
| 233 | 128 | } |
| 234 | 129 | |
| 235 | 130 | /** |
| 236 | 131 | * Whether a job's status still permits submission. |