|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class PilotApiClient |
|
4
|
|
|
* |
|
5
|
|
|
* @author Cristian Jimenez <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Zephia\PilotApiClient\Client; |
|
9
|
|
|
|
|
10
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
11
|
|
|
use Zephia\PilotApiClient\Exception\InvalidArgumentException; |
|
12
|
|
|
use Zephia\PilotApiClient\Model\LeadData; |
|
13
|
|
|
|
|
14
|
|
|
class PilotApiClient |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Pilot API URI |
|
18
|
|
|
*/ |
|
19
|
|
|
const BASE_URI = 'http://www.pilotsolution.com.ar/api/webhooks/welcome.php'; |
|
20
|
|
|
|
|
21
|
|
|
private $appKey; |
|
22
|
|
|
private $debug; |
|
23
|
|
|
private $notification_email; |
|
|
|
|
|
|
24
|
|
|
private $guzzleClient; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* PilotApi constructor. |
|
28
|
|
|
* @param array $config |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(array $config = []) |
|
31
|
|
|
{ |
|
32
|
|
|
$defaults = [ |
|
33
|
|
|
'debug' => false, |
|
34
|
|
|
'app_key' => null, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
$config = array_merge($config, $defaults); |
|
38
|
|
|
|
|
39
|
|
|
$this->appKey = $config['app_key']; |
|
40
|
|
|
$this->debug = $config['debug']; |
|
41
|
|
|
|
|
42
|
|
|
$this->guzzleClient = new GuzzleClient(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Store lead |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $lead_data Lead data array. |
|
49
|
|
|
* See documentation at http://www.pilotsolution.com.ar/home/api.php (Array keys without 'pilot_' prefix) |
|
50
|
|
|
* Example: |
|
51
|
|
|
* $lead_data = ['firstname' => 'John', 'lastname' = 'Doe', 'phone' => '+543512345678', 'email' => '[email protected]']; |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $business_type_id Código numérico del tipo negocio del dato (1: 0km, 2: Usados, 3: Plan de Ahorro) |
|
|
|
|
|
|
54
|
|
|
* @param int $contact_type_id Código numérico del tipo de contacto del dato (1: Electrónico, 2: Telefónico , 3: Entrevista) |
|
|
|
|
|
|
55
|
|
|
* @return string Pilot API response |
|
56
|
|
|
*/ |
|
57
|
|
|
public function storeLead(LeadData $lead_data, $notification_email = '') |
|
58
|
|
|
{ |
|
59
|
|
|
if (empty($lead_data)) { |
|
60
|
|
|
throw new InvalidArgumentException("Lead Data is empty."); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/*$pilot_lead_data = []; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
foreach ($lead_data as $key => $value) { |
|
66
|
|
|
$pilot_lead_data['pilot_' . $key] = $value; |
|
67
|
|
|
}*/ |
|
68
|
|
|
|
|
69
|
|
|
$form_params = [ |
|
70
|
|
|
'debug' => $this->debug, |
|
71
|
|
|
'action' => 'create', |
|
72
|
|
|
'appkey' => $this->appKey, |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
$form_params = array_merge($form_params, $lead_data->toArray()); |
|
76
|
|
|
|
|
77
|
|
|
if (!empty($notification_email)) { |
|
78
|
|
|
$form_params['notification_email'] = $notification_email; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$response = $this->guzzleClient->post(self::BASE_URI, [ |
|
82
|
|
|
'body' => $form_params |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
if ($response->getStatusCode() != 200) { |
|
86
|
|
|
throw new \InvalidArgumentException( |
|
87
|
|
|
$response->getBody()->getContents() |
|
88
|
|
|
); |
|
89
|
|
|
} else { |
|
90
|
|
|
return $response->getBody()->getContents(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return GuzzleClient |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getGuzzleClient() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->guzzleClient; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.