t_params()['jwt_params'] ?? null; if ( null === $jwt ) { $this->add_error( self::get_error_response_data( self::ERROR_JWT_NOT_FOUND ) ); return $this->get_request_response( [], self::HTTP_BAD_REQUEST ); } $params = $this->get_decoded_request_params( $jwt ); if ( null === $params ) { return $this->get_request_response( [], self::HTTP_UNAUTHORIZED ); } $jwt_catalog_id = $params['aud'] ?? ''; if ( empty( $jwt_catalog_id ) || $jwt_catalog_id !== $fb_integration->get_product_catalog_id() ) { $this->add_error( self::get_error_response_data( self::ERROR_CATALOG_ID_MISMATCH, sprintf( 'Platform Catalog ID: %s, Request Catalog ID: %s', $fb_integration->get_product_catalog_id(), $jwt_catalog_id ) ) ); return $this->get_request_response( [], self::HTTP_FORBIDDEN ); } try { $response_data = $this->execute_endpoint( $params['payload'] ); return $this->get_request_response( $response_data ); } catch ( \Exception $ex ) { $this->add_error( self::get_error_response_data( self::ERROR_OFFER_MANAGEMENT_ERROR, $ex->getMessage() ) ); return $this->get_request_response( [], self::HTTP_ERROR ); } } protected function get_decoded_request_params( string $jwt ): ?array { try { $decoded_params = RequestVerification::decode_jwt_with_retries( $jwt ); } catch ( JWTExpiredException $ex ) { $this->add_error( self::get_error_response_data( self::ERROR_JWT_EXPIRED ) ); return null; } catch ( \Exception $ex ) { $this->add_error( self::get_error_response_data( self::ERROR_JWT_DECODE_FAILURE, $ex->getMessage() ) ); return null; } return $decoded_params; } protected static function get_offer_response_data( WC_Coupon $coupon ): array { $is_percent_off = 'percent' === $coupon->get_discount_type(); $percent_off = $is_percent_off ? (int) $coupon->get_amount() : null; $fixed_amount_off = $is_percent_off ? null : [ 'amount' => $coupon->get_amount(), 'currency' => get_woocommerce_currency(), ]; $offer_class = 'order'; return array( 'offer_id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'percent_off' => $percent_off, 'fixed_amount_off' => $fixed_amount_off, 'offer_class' => $offer_class, 'end_time' => $coupon->get_date_expires() ? $coupon->get_date_expires()->getTimestamp() : 0, 'usage_limit' => $coupon->get_usage_limit() ?? 0, 'usage_count' => $coupon->get_usage_count() ?? 0, ); } protected static function get_error_response_data( string $error_type, string $error_message = '', ?string $offer_code = null ): array { return [ 'error_type' => $error_type, 'error_message' => $error_message, 'offer_code' => $offer_code, ]; } protected static function get_params_value_enforced( string $field_name, array $params ) { if ( array_key_exists( $field_name, $params ) ) { return $params[ $field_name ]; } throw new \OutOfBoundsException( esc_html( sprintf( 'Field: %s does not exist in request params. Params fields: %s', $field_name, wp_json_encode( array_keys( $params ) ) ) ) ); } private function get_request_response( array $response_data, int $status_code = self::HTTP_OK ): WP_REST_Response { $response_data = [ 'data' => $response_data, 'errors' => $this->errors, ]; return new WP_REST_Response( $response_data, $status_code ); } }