class PipedriveHelper
{
protected $api_token;
protected $domain;
protected $headers = array(
‘Cache-Control: no-cache’,
‘Content-Type: application/json’,
);
public $org_id = 176;
public function getApiToken()
{
return $this->api_token;
}
public function setApiToken($token)
{
$this->api_token = $token;
return $this;
}
public function getDomain()
{
return $this->domain;
}
public function setDomain($domain)
{
$this->domain = $domain;
return $this;
}
public function getHeaders()
{
return $this->headers;
}
public function setHeaders($headers)
{
$this->headers = array_merge($this->headers, $headers);
return $this;
}
public function __construct()
{
$this->json = Loader::helper('json');
$this->setApiToken('18b8137d0626407cab01a88616003c815862ff11');
// fd25a0ae1634cd114d1b8778cfb75a529b319035 // OLD API KEY
$this->setDomain('iminsured');
}
/**
* Main cURL function
*
* @param $url
* @param $method
* @param $payload
* @param $url_params
* @param $request_headers
* @return $response
* @author Lee Jones
* @copyright 2020
*/
private function request($url, $method, $payload = false, $url_params = array(), $request_headers = array())
{
$curl_url = 'https://' . $this->getDomain() . '.pipedrive.com/v1/' . $url . '?api_token=' . $this->getApiToken();
foreach ($url_params as $key => $value) {
$curl_url = $curl_url . '&' . $key . '=' . $value;
}
$ch = curl_init($curl_url);
$this->curlSetopts($ch, $method, $payload, $request_headers);
$result = curl_exec($ch);
$resp_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerLen = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response = substr($result, $headerLen);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
return $response;
}
/**
* cURL Options
*
* @param $ch - cURL object
* @param $method - GET/POST/PUT/DELETE
* @param $payload - JSON payload
* @param $request_headers - custom request headers
* @return void
* @author Lee Jones
* @copyright 2020
*/
private function curlSetopts($ch, $method, $payload, $request_headers)
{
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($request_headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
}
if ($method == 'POST' && !empty($payload)) {
if (is_array($payload)) {
$payload = http_build_query($payload);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
if ($method == 'PUT' && !empty($payload)) {
if (is_array($payload)) {
$payload = http_build_query($payload);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
}
/**
* Get Pipedrive person by email in the custom field
*
* @param $email
*
* @return string
*/
public function getPersonByEmail($email)
{
$params = array(
'term' => $email,
'fields' => 'email',
'exact_match' => true
);
$response = $this->request('persons/search', 'GET', false, $params);
return $response;
}
/**
* Generate Person Payload
*
* @param object $customer
* @param object|boolean $customer2
* @param string|boolean $address
* @param string|boolean $occupation
* @param boolean $is_update
*
* @return array
*/
protected function generatePersonPayload($customer, $customer2 = false, $address = false, $occupation = false, $is_update)
{
$payload = array(
'name' => $customer->getFirstName() . ' ' . $customer->getLastName(),
'phone' => $customer->getTelephone(),
'4cfaf12aabc5b6ce35a29dfd535bdaf2c924b5fc' => ($customer->getGender() == 'M') ? 'Male' : 'Female', // gender
'5f6c9adbdecfb3b250213b73289e2be1855f1a7c' => $customer->getDob(), // dob
'7912ca6c170674562983dee76ab5709fe912455e' => ($customer->getSmoke() == 'N' ? 'No' : 'Yes'), // smoker
'7b342b9df19dc958364156921ba14ca20db574c3' => ($customer->getMedicalCondition() == 'N' ? 'No' : 'Yes'), // medical
'7bc95b72183bda6a2d46a8331dbac1fc708589fc' => $customer->getHeight(), // height
'f40d5840aa6bdf779b9c2b047b92b7f7d5d065bc' => $customer->getWeight(), // weight
'1eaba4f5051f8dfd10d67f3aa7287afe91a5cd40' => ($customer->getHighRisk() == 'N' ? 'No' : 'Yes') // high risk
);
if (!$is_update) {
$payload['email'] = $customer->getEmail();
}
if ($address) {
$payload['d6f0d515437c130bedd56fd1a9b8b467d1bd08b3'] = $address;
}
if ($occupation) {
$payload['99246b5fd81d2bcfab05b89a8086d826a49a15a4'] = $occupation;
}
if ($customer2) {
$payload['0b8fd351543da8df1d45ad2701f33f152f18937e'] = ($customer2) ? 'Yes' : 'No'; // has second life
$payload['569b53dd1a7b887d26117dc6c6dc013502f61604'] = $customer2->getFirstName() . ' ' . $customer2->getLastName();
$payload['a651654511899b0f1a3a7dc15784349446b5a5a2'] = ($customer2->getGender() == 'M') ? 'Male' : 'Female'; // gender
$payload['1758edf6ecc3ff97e8d8a3a69b6dfa66dce560df'] = $customer2->getDob(); // dob
$payload['56076d82ef0273eac48490ed6b30a52f374b25f8'] = $customer2->getSmoke(); // smoker
$payload['4bdd2407d2623871b8501198375f76ff52a8f239'] = ($customer2->getMedicalCondition() == 'N' ? 'No' : 'Yes'); // medical
$payload['a065bd78f90a6864f6b1cc1aafc331be8073cb7f'] = $customer2->getHeight(); // height
$payload['89d7a1ac47a57f657de9747845ee78d3db0d0b00'] = $customer2->getWeight(); // weight
$payload['64d0a0c0f2a30d6016a54bd4a5ccc1c98bab1023'] = ($customer2->getHighRisk() == 'N' ? 'No' : 'Yes'); // high risk
}
return $payload;
}
/**
* Create Pipedrive Person
*
* @param object $customer
* @param object|boolean $customer2
* @param string|boolean $address
* @param string|boolean $occupation
*
* @return string
*/
public function createPerson($customer, $customer2 = false, $address = false, $occupation = false)
{
$payload = $this->generatePersonPayload($customer, $customer2, $address, $occupation, false);
$response = $this->request('persons', 'POST', $payload);
return $response;
}
/**
* Update Pipedrive Person
*
* @param string $person_id
* @param object $customer
* @param object|boolean $customer2
* @param string|boolean $address
* @param string|boolean $occupation
*
* @return string
*/
public function updatePerson($person_id, $customer, $customer2 = false, $address = false, $occupation = false)
{
$payload = $this->generatePersonPayload($customer, $customer2, $address, $occupation, true);
$response = $this->request('persons/' . $person_id, 'PUT', $payload);
return $response;
}
/**
* Set the Initial Quote Deal
*
* @param string $email
* @param string $quote_id
*
* @return object|boolean
*/
public function setInitialDeal($email, $quote_id)
{
$person = null;
Loader::model('quote', 'life_assurance_w');
Loader::model('webline_quote', 'life_assurance_w');
$quote = Quote::getByID($quote_id);
$wl_quote = WeblineQuote::getByQuoteID($quote_id);
if ($wl_quote) {
$cust_1 = $quote->getCustomerOne();
$cust_2 = $quote->getCustomerTwo();
$person_response = $this->json->decode($this->getPersonByEmail($email), true);
if ($person_response->success < 1 || empty($person_response->data)) {
$person_response = $this->json->decode($this->createPerson($cust_1, $cust_2));
if ($person_response->success) {
$person = $person_response->data;
}
} else {
$person_response = $this->json->decode($this->updatePerson($person_response->data[0]->id, $cust_1, $cust_2));
if ($person_response->success) {
$person = $person_response->data;
}
}
if ($person == null) {
return false;
}
$data = array(
'title' => $wl_quote->getQuoteReference() . ' ' . $cust_1->getLastName(),
'person_id' => $person->id,
'org_id' => $this->org_id,
'8b3481c86963ef18546695dc952026f54f31f14a' => $_SESSION['la']['unique-id'], // form id
'a2c8d28983a1494bf6288953f7f606a522485950' => $wl_quote->getQuoteReference(),
'2637518334b6bcbe21b738da6d9e073c0ca89a14' => $quote->getCoverType()->getCoverTypeNice(),
'bf4d802e36430aeb2347ca3fa9ea2156a36a0a85' => $quote->getAmount(),
'ce39fa8bc1443e8e030a7c09acc143e676e13f06' => $quote->getTermYears(),
'e46f4ef0be33389090f5692c0315f7c1616817f0' => $_SESSION['la']['site_referer'],
'db4d13be9417711592189a684dd31ca2589695d9' => ($quote->getIndexation() == 'N' ? 'No' : 'Yes'),
);
if ($cust_2) {
$data['title'] = $wl_quote->getQuoteReference() . ' ' . $cust_1->getLastName() . ' & ' . $cust_2->getLastName();
}
$response = $this->request('deals', 'POST', $data);
$deal = $this->json->decode($response);
if (property_exists($deal, 'data') && property_exists($deal->data, 'id')) {
return $deal;
}
}
return false;
}
/**
* Set Application Deal
*
* I *think* this happens when someone has decided that they do want a policy and have gotten to the apply page
*
* @param object $data
* @param object $quote
* @param object $wqr - Webline quote result
* @param object $wl_quote - Webline quote
*
* @return object|boolean
*/
public function setApplicationDeal($data, $quote, $wqr, $wl_quote)
{
$person = null;
$object = $data['ins-life']['first-life'];
$benefit_type = $wl_quote->getBenefitType();
if ($wqr) {
$cust_1 = $quote->getCustomerOne();
$cust_2 = $quote->getCustomerTwo();
$address = implode(", ", array($object['house_no'], $object['street_one'], $object['street_two'], $object['city'], $object['postcode']));
$person_response = $this->json->decode($this->getPersonByEmail($email), true);
if ($person_response->success < 1 || empty($person_response->data)) {
$person_response = $this->json->decode($this->createPerson($cust_1, $cust_2, $address, $object['occupation']));
if ($person_response->success) {
$person = $person_response->data;
}
} else {
$person_response = $this->json->decode($this->updatePerson($person_response->data[0]->id, $cust_1, $cust_2, $address, $object['occupation']));
if ($person_response->success) {
$person = $person_response->data;
}
}
if ($person == null) {
return false;
}
$pd_data = array(
'id' => $object['pipedrive_id'],
'title' => $wqr->getResponseRef() . ' ' . $cust_1->getLastName(), // Now get the result ref
'stage_id' => 10,
'89a5051ca2d69391a86d8464b0cc727547ca18ff' => $_SESSION['la']['quote_results_url'], // quote results URL
'8b3481c86963ef18546695dc952026f54f31f14a' => $_SESSION['la']['unique-id'], // form id
'3f87fdab7fc1e6d0b5ed46db490a9679a35ff2b1' => $wqr->getResponseRef(),
'907bb1c4255473ce5275aaa80430763c82054270' => $wqr->getPremium(),
'bf4d802e36430aeb2347ca3fa9ea2156a36a0a85' => $quote->getAmount(),
'56c3286a748bd5ef234a037f4cddbd3d6c391e7b' => $wqr->getProductName(), // Need to check
'a857106379bdf9571fd98142ead489de83e8b1bb' => $wqr->getProviderName(),
'49a02ddb2f09bd63655d580ead559c1872f3a879' => (isset($benefit_type) && $benefit_type->getBenefitTypeHandle() == 'life_cic') ? 'Yes' : 'No',
);
if ($cust_2) {
$pd_data['title'] = $wqr->getResponseRef() . ' ' . $cust_1->getLastName() . ' & ' . $cust_2->getLastName();
}
$response = $this->request('deals', 'POST', $pd_data);
$deal = $this->json->decode($response);
if (property_exists($deal, 'data') && property_exists($deal->data, 'id')) {
return $deal;
}
}
return false;
}
}