use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\record\APIException;
use com\zoho\api\authenticator\store\TokenStore;
use com\zoho\api\authenticator\store\FileStore;
use com\zoho\crm\api\record\BodyWrapper;
use com\zoho\crm\api\record\RecordOperations;
use com\zoho\crm\api\record\Record;
use com\zoho\crm\api\users\User;
require_once "vendor/autoload.php";
add_action('user_register', 'send_data_to_zoho_crm', 10, 1);
// Create directory for token storage
register_activation_hook(__FILE__, 'zoho_crm_integration_activate');
function zoho_crm_integration_activate() {
$token_directory = plugin_dir_path(__FILE__) . 'tokens.txt';
if (!file_exists($token_directory)) {
wp_mkdir_p($token_directory);
}
}
function send_data_to_zoho_crm($user_id) {
// Schedule the function to run 5 seconds later
wp_schedule_single_event(time() + 5, 'zoho_crm_integration_send_data', [$user_id]);
}
add_action('zoho_crm_integration_send_data', 'zoho_crm_integration_send_data_now', 10, 1);
function zoho_crm_integration_send_data_now($user_id) {
$moduleAPIName = "Accounts";
$user_info = get_userdata($user_id);
$phone = get_user_meta($user_id, '___META FIELD_____', true);
$createRecords = new CreateRecords();
$createRecords->createRecords($moduleAPIName,
$user_info->first_name,
$user_info->last_name,
$user_info->user_email,
$phone
);
}
class CreateRecords
{
public function __construct()
{
$user = new UserSignature('_____User Email ID_____');
$environment = USDataCenter::PRODUCTION();
$token = (new OAuthBuilder())
->clientId("_____Client Id_____")
->clientSecret("_____Client Secret ____")
->refreshToken("_____Refresh TOken______")
->build();
$tokenstore = new FileStore(plugin_dir_path(__FILE__) . 'tokens.txt'); // File storage for tokens
(new InitializeBuilder())
->user($user)
->environment($environment)
->token($token)
->store($tokenstore)
->initialize(); // Use the initialize() method
}
public function createRecords(
string $moduleAPIName,
string $first_name,
string $last_name,
string $user_email,
string $phone
) {
$recordOperations = new RecordOperations();
$bodyWrapper = new BodyWrapper();
$recordInstance = new Record();
$current_datetime = date('Y-m-d\TH:i:sP');
$recordInstance->addKeyValue("First_Name", $first_name);
$recordInstance->addKeyValue("Last_Name", $last_name);
$recordInstance->addKeyValue("Phone_1", $phone);
$bodyWrapper->setData([$recordInstance]);
try {
$response = $recordOperations->createRecords($moduleAPIName, $bodyWrapper);
error_log("API response: " . print_r($response, true));
} catch (APIException $exception) {
error_log("APIException: " . print_r($exception, true));
} catch (\Exception $exception) {
error_log("Exception: " . $exception->getMessage());
}
}
}
?>