posOrderId = $posOrderId; $this->status = $status; $this->currency = $currency; $this->paymentMethod = $paymentMethod; $this->setPaid = $setPaid; $this->total = $total; $this->totalTax = $totalTax; $this->lineItems = $lineItems; $this->metaData = $metaData; $this->taxLines = $taxLines; } public static function fromArray( array $data ): self { $posOrderId = self::requireNonEmptyString( $data, 'pos_order_id' ); $status = self::requireNonEmptyString( $data, 'status' ); $currency = self::requireNonEmptyString( $data, 'currency' ); $paymentMethod = self::requireNonEmptyString( $data, 'payment_method' ); $total = self::requireMoney( $data, 'total' ); $totalTax = self::optionalMoney( $data, 'total_tax', '0' ); $lineItems = self::requireLineItems( $data ); if ( 3 !== strlen( $currency ) ) { throw new InvalidArgumentException( 'currency must be a 3-letter ISO code.' ); } return new self( $posOrderId, $status, strtoupper( $currency ), $paymentMethod, (bool) ( $data['set_paid'] ?? true ), $total, $totalTax, $lineItems, is_array( $data['meta_data'] ?? null ) ? $data['meta_data'] : [], is_array( $data['tax_lines'] ?? null ) ? $data['tax_lines'] : [] ); } public function posOrderId(): string { return $this->posOrderId; } public function status(): string { return $this->status; } public function currency(): string { return $this->currency; } public function paymentMethod(): string { return $this->paymentMethod; } public function setPaid(): bool { return $this->setPaid; } public function total(): string { return $this->total; } public function totalTax(): string { return $this->totalTax; } public function lineItems(): array { return $this->lineItems; } public function metaData(): array { return $this->metaData; } /** * One entry per distinct WooCommerce tax rate applied to this order — * each: ['rate_id' => string, 'label' => string, 'tax_total' => string]. * Empty when akantio_pos couldn't resolve a real WooCommerce rate for * any line item (e.g. a locally-defined tax not synced from WooCommerce). */ public function taxLines(): array { return $this->taxLines; } private static function requireLineItems( array $data ): array { if ( empty( $data['line_items'] ) || ! is_array( $data['line_items'] ) ) { throw new InvalidArgumentException( 'line_items is required and must not be empty.' ); } return array_map( [ self::class, 'validateLineItem' ], $data['line_items'] ); } private static function validateLineItem( $item ): array { if ( ! is_array( $item ) ) { throw new InvalidArgumentException( 'Each line item must be an object.' ); } $hasProductId = ! empty( $item['product_id'] ); $hasSku = ! empty( $item['sku'] ); if ( ! $hasProductId && ! $hasSku ) { throw new InvalidArgumentException( 'Each line item requires product_id or sku.' ); } if ( (int) ( $item['quantity'] ?? 0 ) <= 0 ) { throw new InvalidArgumentException( 'Each line item requires a quantity greater than zero.' ); } foreach ( [ 'subtotal', 'subtotal_tax', 'total', 'total_tax' ] as $moneyField ) { if ( isset( $item[ $moneyField ] ) && ! self::isNonNegativeMoney( (string) $item[ $moneyField ] ) ) { // $moneyField is always one of the four fixed literals above, // never attacker-controlled, and this becomes a WP_Error's // JSON body (Rest\OrderController), never HTML. throw new InvalidArgumentException( "Line item {$moneyField} must be a non-negative number." ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } } return $item; } // $field in both methods below is always a fixed literal the caller // passes in (e.g. 'pos_order_id', 'total') — never attacker-controlled // — and the resulting message becomes a WP_Error's JSON body // (Rest\OrderController), never HTML. private static function requireNonEmptyString( array $data, string $field ): string { if ( empty( $data[ $field ] ) || ! is_string( $data[ $field ] ) ) { throw new InvalidArgumentException( "{$field} is required." ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } return $data[ $field ]; } private static function requireMoney( array $data, string $field ): string { if ( ! isset( $data[ $field ] ) || ! self::isNonNegativeMoney( (string) $data[ $field ] ) ) { throw new InvalidArgumentException( "{$field} is required and must be a non-negative number." ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } return (string) $data[ $field ]; } private static function optionalMoney( array $data, string $field, string $default ): string { if ( ! isset( $data[ $field ] ) ) { return $default; } return self::requireMoney( $data, $field ); } private static function isNonNegativeMoney( string $value ): bool { return is_numeric( $value ) && (float) $value >= 0; } }