| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fiscal receipt service. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
/** |
| 11 |
* Fiscal_Receipt_Service class. |
| 12 |
*/ |
| 13 |
class Fiscal_Receipt_Service { |
| 14 |
/** |
| 15 |
* Submission status meta key. |
| 16 |
*/ |
| 17 |
const META_KEY_SUBMISSION_STATUS = '_wcpos_receipt_submission_status'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Submission status updated timestamp meta key. |
| 21 |
*/ |
| 22 |
const META_KEY_STATUS_UPDATED_AT = '_wcpos_receipt_submission_status_updated_at'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Valid submission statuses. |
| 26 |
*/ |
| 27 |
const VALID_STATUSES = array( 'pending', 'sent', 'failed' ); |
| 28 |
|
| 29 |
/** |
| 30 |
* Enrich fiscal payload via extension hook. |
| 31 |
* |
| 32 |
* @param array $snapshot Fiscal snapshot payload. |
| 33 |
* @param int $order_id Order ID. |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public function enrich_snapshot( array $snapshot, int $order_id ): array { |
| 38 |
$enriched = apply_filters( 'woocommerce_pos_fiscal_snapshot_enrich', $snapshot, $order_id ); |
| 39 |
|
| 40 |
return \is_array( $enriched ) ? $enriched : $snapshot; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Persist fiscal submission status. |
| 45 |
* |
| 46 |
* @param int $order_id Order ID. |
| 47 |
* @param string $status Submission status. |
| 48 |
*/ |
| 49 |
public function set_submission_status( int $order_id, string $status ): void { |
| 50 |
if ( ! in_array( $status, self::VALID_STATUSES, true ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
update_post_meta( $order_id, self::META_KEY_SUBMISSION_STATUS, $status ); |
| 55 |
update_post_meta( $order_id, self::META_KEY_STATUS_UPDATED_AT, current_time( 'mysql', true ) ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get submission status for an order. |
| 60 |
* |
| 61 |
* @param int $order_id Order ID. |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
public function get_submission_status( int $order_id ): string { |
| 66 |
$status = get_post_meta( $order_id, self::META_KEY_SUBMISSION_STATUS, true ); |
| 67 |
|
| 68 |
if ( in_array( $status, self::VALID_STATUSES, true ) ) { |
| 69 |
return $status; |
| 70 |
} |
| 71 |
|
| 72 |
return 'pending'; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Trigger retry hook and mark submission pending. |
| 77 |
* |
| 78 |
* @param int $order_id Order ID. |
| 79 |
*/ |
| 80 |
public function retry_submission( int $order_id ): void { |
| 81 |
$this->set_submission_status( $order_id, 'pending' ); |
| 82 |
do_action( 'woocommerce_pos_fiscal_submission_retry', $order_id ); |
| 83 |
} |
| 84 |
} |
| 85 |
|