| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * This script allows to get additional data of contact from SALESmanago system |
|---|
| 5 | * before sending email via campaigns module |
|---|
| 6 | * |
|---|
| 7 | * @author Wojciech Niestolik <wniestolik@betasoft.pl> |
|---|
| 8 | * @version 1.0 |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * SALESmanago API client |
|---|
| 15 | */ |
|---|
| 16 | class SalesManagoApiClient { |
|---|
| 17 | |
|---|
| 18 | /** Api server end point */ |
|---|
| 19 | private $endpoint = 'http://sales-manago-endpoint'; |
|---|
| 20 | |
|---|
| 21 | /** Api client identifier */ |
|---|
| 22 | private $apiClient = 'api-client'; |
|---|
| 23 | |
|---|
| 24 | /** Api secret value */ |
|---|
| 25 | private $apiSecret = 'api-secret'; |
|---|
| 26 | |
|---|
| 27 | /** API key */ |
|---|
| 28 | private $apiKey = 'some-api-key'; |
|---|
| 29 | |
|---|
| 30 | /** Proxy server (host:port) */ |
|---|
| 31 | private $proxy; |
|---|
| 32 | |
|---|
| 33 | /** Timeout for requests */ |
|---|
| 34 | private $timeout = 5; |
|---|
| 35 | |
|---|
| 36 | /** API SHA (automaticaly generated) */ |
|---|
| 37 | private $apiSha; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Call API method |
|---|
| 43 | */ |
|---|
| 44 | public function call($method, $params) { |
|---|
| 45 | $data = array( |
|---|
| 46 | 'apiKey' => $this->apiKey, |
|---|
| 47 | 'clientId' => $this->apiClient, |
|---|
| 48 | 'sha' => sha1($this->apiKey.$this->apiClient.$this->apiSecret), |
|---|
| 49 | 'requestTime' => time(), |
|---|
| 50 | ); |
|---|
| 51 | $data = array_merge($data, $params); |
|---|
| 52 | |
|---|
| 53 | $curl = curl_init(); |
|---|
| 54 | |
|---|
| 55 | curl_setopt($curl, CURLOPT_URL, $this->endpoint.$method); |
|---|
| 56 | curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json, application/json', 'Content-Type: application/json;charset=UTF-8')); |
|---|
| 57 | curl_setopt($curl, CURLOPT_POST, 1); |
|---|
| 58 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); |
|---|
| 59 | curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); |
|---|
| 60 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|---|
| 61 | curl_setopt($curl, CURLOPT_HEADER, false); |
|---|
| 62 | |
|---|
| 63 | if(!empty($this->proxy)) { |
|---|
| 64 | curl_setopt($curl, CURLOPT_PROXY, $this->proxy); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | $hres = curl_exec($curl); |
|---|
| 68 | |
|---|
| 69 | $res = json_decode($hres); |
|---|
| 70 | |
|---|
| 71 | if(isset($res->success)) { |
|---|
| 72 | if(!$res->success) { |
|---|
| 73 | $msg = isset($res->message) && !empty($res->message) ? implode(';', $res->message) : 'API method execution failure'; |
|---|
| 74 | throw new Exception($msg); |
|---|
| 75 | } |
|---|
| 76 | } else { |
|---|
| 77 | error_log($res); |
|---|
| 78 | throw new Exception('SALESmanago API service is not available'); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return $res; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * SALESmanago API service |
|---|
| 89 | */ |
|---|
| 90 | class SalesManagoApiService { |
|---|
| 91 | |
|---|
| 92 | /** Last message */ |
|---|
| 93 | private $lastMessage; |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Set last message |
|---|
| 99 | */ |
|---|
| 100 | private function setLastMessage($msg = null) { |
|---|
| 101 | $this->lastMessage = strlen($msg) ? $msg : null; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Get last message |
|---|
| 108 | */ |
|---|
| 109 | public function getLastMessage() { |
|---|
| 110 | return $this->lastMessage; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Get contact id by email |
|---|
| 117 | */ |
|---|
| 118 | public function getContactIdByEmail($email) { |
|---|
| 119 | $contactId = null; |
|---|
| 120 | |
|---|
| 121 | $this->setLastMessage(); |
|---|
| 122 | |
|---|
| 123 | $apiClient = new SalesManagoApiClient(); |
|---|
| 124 | |
|---|
| 125 | $params = array( |
|---|
| 126 | 'email' => $email, |
|---|
| 127 | 'owner' => 'aneta.kepka@isl.pl', |
|---|
| 128 | ); |
|---|
| 129 | |
|---|
| 130 | try { |
|---|
| 131 | $res = $apiClient->call('contact/hasContact', $params); |
|---|
| 132 | |
|---|
| 133 | if(isset($res->contactId) && !empty($res->contactId)) { |
|---|
| 134 | $contactId = $res->contactId; |
|---|
| 135 | } |
|---|
| 136 | } catch(Exception $e) { |
|---|
| 137 | $this->setLastMessage($e->getMessage()); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return $contactId; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | // Check if container object exists |
|---|
| 148 | if(!isset($args['container']) || !is_a($args['container'], 'FlexyTemplatePage')) { |
|---|
| 149 | throw new Exception(__CLASS__.':'.__LINE__.': '.sprintf('Brak obiektu %s', 'container')); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | // Check if email param exists |
|---|
| 153 | if(!isset($args['email']) || !strlen($args['email'])) { |
|---|
| 154 | throw new Exception(__CLASS__.':'.__LINE__.': '.sprintf('Brak parametru %s', 'email')); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | // Add object salesmanago to obj |
|---|
| 158 | $args['container']->salesmanago = new stdClass(); |
|---|
| 159 | |
|---|
| 160 | $api = new SalesManagoApiService(); |
|---|
| 161 | |
|---|
| 162 | // Get contactId from SALESmanago and pass it to external object |
|---|
| 163 | $contactId = $api->getContactIdByEmail($args['email']); |
|---|
| 164 | $args['container']->salesmanago->contactId = $contactId ? $contactId : null; |
|---|
| 165 | |
|---|
| 166 | ?> |
|---|