HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/quadcode/app/Helpers/PipedriveHelper.php
<?php

namespace App\Helpers;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class PipedriveHelper
{

    private string $url;
    private string $token;

    const LOG_CHANNEL = 'pipedrive';

    const CUSTOM_FIELD_LANDING_URL = '60d21dc88f7166c7d9c9581d960d5e0a1d7c0700';
    const CUSTOM_FIELD_UTM_CAMPAIGN = '326a053a02f760969835c6d66320ba8146971532';
    const CUSTOM_FIELD_UTM_MEDIUM = '06b0e97e69412bf996c7cbf3b0eb7259d2117e6b';
    const CUSTOM_FIELD_UTM_SOURCE = '3e1cca698c4828ffa2545d72fa47250f1db952f9';
    const CUSTOM_FIELD_TEXT = '921534be957ee8cf90d2b5d394f121856020a92a';
    const CUSTOM_FIELD_GCLID = 'b011c07cae8c07827c38a04ef5a11d481b510c62';
    const CUSTOM_FIELD_LANG = '94f45dcab632c59ee4d51e7cb8b97440d228fd6d';
    const CUSTOM_FIELD_ADDITIONAL = '7737f00c7861b969d3a888a1ee51ab72bbc265df';
    const CUSTOM_FIELD_BUSINESS_IDENTIFICATION = '8ef4834cd30972e74ef404a671ae3c760f0a7d40';
    const CUSTOM_FIELD_INITIAL_INVESTMENT = '41be2a605ac5c07491b38614095b6bbef81d4ad5';
    const CUSTOM_FIELD_REGION = 'b0bb331b1e7fba2c621219cc290a2e72acdfb4de';
    const CUSTOM_FIELD_CALENDLY_SCHEDULED = '00e5e40c0247bc7eae2aadfd9a406ad373b7ac28';
    const CUSTOM_FIELD_COUNTRY_CODE = 'b02af6430abeb6867d44b54d73859e7349edc240';

    private static array $businessIdentifications = [
        'Brokerage business' => 56,
        'Introducing broker' => 69,
        'Affiliate' => 70,
        'Trader' => 59,
        'Entrepreneur' => 71,
        'Other' => 53,
    ];

    private static array $initialInvestments = [
        '$0' => 46,
        '$1000' => 47,
        '$5000' => 48,
        '$10000' => 49,
        '$25000' => 50,
        '$50000' => 51,
        'More then $50000' => 52,
    ];

    private static array $regions = [
        'North America' => 60,
        'Europe' => 64,
        'Asia-Pacific' => 62,
        'South America' => 61,
        'Africa' => 63,
    ];

    private static array $calendlyScheduled = [
        'yes' => 91,
        'no' => 92,
    ];

    private static array $customFields = [
        'landing_url' => self::CUSTOM_FIELD_LANDING_URL,
        'utm_campaign' => self::CUSTOM_FIELD_UTM_CAMPAIGN,
        'utm_medium' => self::CUSTOM_FIELD_UTM_MEDIUM,
        'utm_source' => self::CUSTOM_FIELD_UTM_SOURCE,
        'message' => self::CUSTOM_FIELD_TEXT,
        'gclid' => self::CUSTOM_FIELD_GCLID,
        'lang' => self::CUSTOM_FIELD_LANG,
        'business_identification' => self::CUSTOM_FIELD_BUSINESS_IDENTIFICATION,
        'initial_investment' => self::CUSTOM_FIELD_INITIAL_INVESTMENT,
        'region' => self::CUSTOM_FIELD_REGION,
        // 'calendly_scheduled' => self::CUSTOM_FIELD_CALENDLY_SCHEDULED,
        'country_code' => self::CUSTOM_FIELD_COUNTRY_CODE,
    ];

    public function __construct()
    {
        $this->url = env('PIPEDRIVE_URL');
        $this->token = env('PIPEDRIVE_TOKEN');
    }

    public function searchPerson(string $term)
    {
        $data = [
            'api_token' => $this->token,
            'exact_match' => true,
            'term' => $term,
        ];

        return Http::get($this->url . '/persons/search', $data)->json();
    }

    public function addPerson(string $name, string $email, ?string $phone)
    {
        $data = [
            // The name of the person
            'name' => $name,
            // The ID of the user who will be marked as the owner of this person. When omitted, the authorized user ID will be used.
            // 'owner_id' => '',
            // The ID of the organization this person will belong to
            // 'org_id' => '',
            // An email address as a string or an array of email objects related to the person. The structure of the array is as follows: [{ "value": "mail@example.com", "primary": "true", "label": "main" }]. Please note that only value is required.
            'email' => $email,
            // A phone number supplied as a string or an array of phone objects related to the person. The structure of the array is as follows: [{ "value": "12345", "primary": "true", "label": "mobile" }]. Please note that only value is required.
            'phone' => $phone ?? '',
            // The visibility of the person. If omitted, the visibility will be set to the default visibility setting of this item type for the authorized user. Read more about visibility groups here. https://support.pipedrive.com/en/article/visibility-groups
            // 'visible_to' => '',
            // If the person does not have a valid email address, then the marketing status is not set and no_consent is returned for the marketing_status value when the new person is created. If the change is forbidden, the status will remain unchanged for every call that tries to modify the marketing status. Please be aware that it is only allowed once to change the marketing status from an old status to a new one.
            // 'marketing_status' => '',
            // The optional creation date & time of the person in UTC. Requires admin user API token. Format: YYYY-MM-DD HH:MM:SS
            'add_time' => date('Y-m-d H:i:s'),
        ];

        return Http::post($this->url . '/persons?api_token=' . $this->token, $data)->json();
    }

    public function convertLead(string $title, int $personId, array $additional = []): array
    {
        $data = [
            'title' => $title,
            'person_id' => $personId,
        ];

        foreach (self::$customFields as $field => $externalKey) {
            if (!empty($additional[$field]) || 'calendly_scheduled' === $field) {
                switch ($field) {
                    case 'business_identification':
                        $data[$externalKey] = self::$businessIdentifications[$additional[$field]];
                        break;
                    case 'initial_investment':
                        $data[$externalKey] = self::$initialInvestments[$additional[$field]];
                        break;
                    case 'region':
                        $data[$externalKey] = [self::$regions[$additional[$field]]];
                        break;
                    case 'gclid':
                    case 'country_code':
                        $data[$externalKey] = (string) $additional[$field];
                        break;
                    // Disabled because field delete from Pipedrive
                    // case 'calendly_scheduled':
                    //     $value = empty($additional[$field]) ? 'no' : $additional[$field];
                    //     $data[$externalKey] = self::$calendlyScheduled[strtolower($value)];
                    //     break;
                    default:
                        $data[$externalKey] = $additional[$field];
                        break;
                }
                unset($additional[$field]);
            }
        }

        if (!empty($additional)) {
            $data[self::CUSTOM_FIELD_ADDITIONAL] = json_encode($additional);
        }

        return $data;
    }

    public function addLead(string $title, int $personId, array $additional = [])
    {
        $data = $this->convertLead($title, $personId, $additional);

        return Http::post($this->url . '/leads?api_token=' . $this->token, $data)->json();
    }

    public function getLead(string $leadId)
    {
        Log::channel(self::LOG_CHANNEL)->info('Get lead request', ['lead_id' => $leadId]);
        $response = Http::get($this->url . '/leads/' . $leadId . '?api_token=' . $this->token)->json();
        Log::channel(self::LOG_CHANNEL)->info('Get lead response', $response);

        return $response;
    }

    public function getUser(string $externalId)
    {
        Log::channel(self::LOG_CHANNEL)->info('Get user request', ['external_id' => $externalId]);
        $response = Http::get($this->url . '/users/' . $externalId . '?api_token=' . $this->token)->json();
        Log::channel(self::LOG_CHANNEL)->info('Get user response', $response);

        return $response;
    }

}