# booktics/1.0.24/core/module/fluentcrm/fluentcrm-service.php

Booktics – Appointment Booking Calendar for Service Businesses, version 1.0.24. 101 lines.

- Page: https://pluginprobe.com/plugins/booktics/1.0.24/code/core/module/fluentcrm/fluentcrm-service.php
- Raw: https://pluginprobe.com/plugins/booktics/1.0.24/raw/core/module/fluentcrm/fluentcrm-service.php
- Modified: 2026-08-25T17:58:46+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/booktics/1.0.24/code/core/module/fluentcrm/fluentcrm-service.php#L10-L20`.

```php
<?php
/**
 * FluentCRM Service
 *
 * Handles FluentCRM integration for Booktics orders
 */

namespace Booktics\Module\FluentCrm;

if ( ! defined( 'ABSPATH' ) ) exit;

use Booktics\Contracts\Hookable_Service_Contract;
use Booktics\Abstracts\Booktics_Database;
use Booktics\Models\Guest_Model;

class FluentCRM_Service implements Hookable_Service_Contract {
	/**
	 * Register hooks
	 */
	public function register() {
		add_action( 'booktics_after_order_created', array( $this, 'add_contact_to_fluentcrm' ), 10, 2 );
	}

	/**
	 * Add contact to FluentCRM
	 *
	 * @param object $order_model   The order model
	 * @param array  $appointments  Created appointments (unused).
	 */
	public function add_contact_to_fluentcrm( $order_model, $appointments = array() ) {
		// Check if FluentCRM is active
		if ( ! defined( 'FLUENTCRM' ) || ! function_exists( 'FluentCrmApi' ) ) {
			return;
		}

		// Get customer details. customer_id is a Booktics guest record id, not a WP user id.
		$customer = ( new Guest_Model( new Booktics_Database() ) )->find( array( 'id' => $order_model->customer_id ) );
		if ( ! $customer || is_wp_error( $customer ) ) {
			return;
		}

		$name_parts = preg_split( '/\s+/', trim( (string) $customer->name ), 2 );

        $customer_data = array(
            'email' => $customer->email,
            'first_name' => $name_parts[0] ?? '',
            'last_name' => $name_parts[1] ?? '',
            'status' => 'subscribed',
        );

		try {
			// Check if FluentCRM Pro is active (has webhooks)
			$is_pro_active = is_plugin_active('fluent-crm-pro/fluentcampaign-pro.php');
            $fluentcrm_webhook_exists = !empty( booktics_get_option( 'fluentcrm_webhook_url') );
			
            if ( $is_pro_active && $fluentcrm_webhook_exists ) {
				// Use webhook if Pro is active
				$webhook_url = booktics_get_option( 'fluentcrm_webhook_url');

				if ( ! empty( $webhook_url ) ) {
                    $this->send_webhook(
						$webhook_url,
						$customer_data,
					);

                    // Returns if fluentCRM pro exists and is working properly
					return;
				}
			}

			// Fallback to direct API for free version, gets used when fluentCRM pro is not active or webhook is not set.
			$contact_api = FluentCrmApi( 'contacts' );
			$contact_api->createOrUpdate( $customer_data );
		} catch ( \Throwable $e ) {
			// Ensures silent failure in case of any error
			if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
				error_log( 'FluentCRM Error: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
			}
		}
	}

	private function send_webhook( $url, $data ) {
		$response = wp_remote_post(
			$url,
			array(
				'body'    => wp_json_encode( $data ),
				'headers' => array(
					'Content-Type' => 'application/json',
				),
				'timeout' => 15,
			)
		);

		if ( is_wp_error( $response ) ) {
			if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
				error_log( 'FluentCRM Webhook Error: ' . $response->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
			}
		}
	}
}

```
