|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmoCRM\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use AmoCRM\Client; |
|
6
|
|
|
use AmoCRM\Request\ParamsBag; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class B2BFamily |
|
10
|
|
|
* |
|
11
|
|
|
* Хелпер для отправки письма через B2BFamily с привязкой к сделке в amoCRM |
|
12
|
|
|
* |
|
13
|
|
|
* @package AmoCRM\Helpers |
|
14
|
|
|
* @version 0.3.1 |
|
15
|
|
|
* @author dotzero <[email protected]> |
|
16
|
|
|
* @link http://www.dotzero.ru/ |
|
17
|
|
|
* @link https://github.com/dotzero/amocrm-php |
|
18
|
|
|
* |
|
19
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
20
|
|
|
* file that was distributed with this source code. |
|
21
|
|
|
*/ |
|
22
|
|
|
class B2BFamily |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @const string Тип HTTP запроса GET |
|
26
|
|
|
*/ |
|
27
|
|
|
const METHOD_GET = 'GET'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @const string Тип HTTP запроса POST |
|
31
|
|
|
*/ |
|
32
|
|
|
const METHOD_POST = 'POST'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @const string Тип HTTP запроса PUT |
|
36
|
|
|
*/ |
|
37
|
|
|
const METHOD_PUT = 'PUT'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @const string Тип HTTP запроса DELETE |
|
41
|
|
|
*/ |
|
42
|
|
|
const METHOD_DELETE = 'DELETE'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var ParamsBag|null Экземпляр ParamsBag для хранения аргументов |
|
46
|
|
|
*/ |
|
47
|
|
|
private $parameters = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var null|string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $apikey = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* B2BFamily constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Client $client экземпляр класса Client |
|
58
|
|
|
* @param $appkey B2BFamily API appkey |
|
59
|
|
|
* @param $secret B2BFamily API secret |
|
60
|
|
|
* @param $email B2BFamily e-mail клиента |
|
61
|
|
|
* @param $password B2BFamily пароль клиента |
|
62
|
|
|
*/ |
|
63
|
4 |
|
public function __construct(Client $client, $appkey, $secret, $email, $password) |
|
64
|
|
|
{ |
|
65
|
4 |
|
$this->parameters = $client->parameters; |
|
66
|
|
|
|
|
67
|
4 |
|
$this->parameters->addAuth('appkey', $appkey); |
|
68
|
4 |
|
$this->parameters->addAuth('secret', $secret); |
|
69
|
4 |
|
$this->parameters->addAuth('email', $email); |
|
70
|
4 |
|
$this->parameters->addAuth('password', $password); |
|
71
|
4 |
|
$this->parameters->addAuth('hash', $this->getHash()); |
|
72
|
4 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Авторизация в сервисе B2BFamily |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function login() |
|
80
|
|
|
{ |
|
81
|
4 |
|
$response = $this->request(self::METHOD_POST, '/user/login', [ |
|
82
|
4 |
|
'appkey' => $this->parameters->getAuth('appkey'), |
|
83
|
4 |
|
'email' => $this->parameters->getAuth('email'), |
|
84
|
4 |
|
'password' => $this->parameters->getAuth('password'), |
|
85
|
4 |
|
'hash' => $this->parameters->getAuth('hash'), |
|
86
|
4 |
|
]); |
|
87
|
|
|
|
|
88
|
4 |
|
if (isset($response['apikey'])) { |
|
89
|
|
|
$this->apikey = $response['apikey']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
return $response; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Подписка amoCRM на получение Web Hook от B2BFamily |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function subscribe() |
|
101
|
|
|
{ |
|
102
|
1 |
|
if ($this->apikey === null) { |
|
103
|
1 |
|
$this->login(); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
return $this->request(self::METHOD_PUT, '/subscribers/add', [ |
|
107
|
1 |
|
'apikey' => $this->apikey, |
|
108
|
1 |
|
'path' => 'http://b2bf.cloudapp.net/post/', |
|
109
|
1 |
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Отписка amoCRM на получение Web Hook от B2BFamily |
|
114
|
|
|
* |
|
115
|
|
|
* @param integer $id Номер подписки для отключения |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function unsubscribe($id) |
|
119
|
|
|
{ |
|
120
|
1 |
|
if ($this->apikey === null) { |
|
121
|
1 |
|
$this->login(); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return $this->request(self::METHOD_DELETE, '/subscribers/delete', [ |
|
125
|
1 |
|
'apikey' => $this->apikey, |
|
126
|
1 |
|
'id' => $id, |
|
127
|
1 |
|
]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Отправка письма через B2BFamily с привязкой к сделке в amoCRM |
|
132
|
|
|
* |
|
133
|
|
|
* @param $lead_id |
|
134
|
|
|
* @param $params |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function mail($lead_id, $params) |
|
138
|
|
|
{ |
|
139
|
1 |
|
if ($this->apikey === null) { |
|
140
|
1 |
|
$this->login(); |
|
141
|
1 |
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
$parameters = array_merge($params, [ |
|
144
|
1 |
|
'apikey' => $this->apikey, |
|
145
|
|
|
'custom_data' => [ |
|
146
|
1 |
|
'userDomainAmo' => $this->parameters->getAuth('domain'), |
|
147
|
1 |
|
'userLoginAmo' => $this->parameters->getAuth('login'), |
|
148
|
1 |
|
'userHashAmo' => $this->parameters->getAuth('apikey'), |
|
149
|
1 |
|
'userTypeAmo' => 2, |
|
150
|
1 |
|
'userIdAmo' => $lead_id, |
|
151
|
1 |
|
], |
|
152
|
|
|
'notification_settings' => [ |
|
153
|
1 |
|
'sms_enable' => false, |
|
154
|
1 |
|
'email_enable' => false, |
|
155
|
1 |
|
'webhook_enable' => true, |
|
156
|
|
|
] |
|
157
|
1 |
|
]); |
|
158
|
|
|
|
|
159
|
1 |
|
return $this->request(self::METHOD_POST, '/mail', $parameters); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Выполнить HTTP запрос и вернуть тело ответа |
|
164
|
|
|
* |
|
165
|
|
|
* @param $method |
|
166
|
|
|
* @param string $url Запрашиваемый URL |
|
167
|
|
|
* @param array $parameters |
|
168
|
|
|
* @return mixed |
|
169
|
|
|
* @throws B2BFamilyException |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function request($method, $url, $parameters = []) |
|
172
|
|
|
{ |
|
173
|
|
|
$endpoint = 'https://api.b2bfamily.com' . $url; |
|
174
|
|
|
|
|
175
|
|
|
if (in_array($method, [self::METHOD_GET, self::METHOD_DELETE])) { |
|
176
|
|
|
$endpoint .= '?' . http_build_query($parameters); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$ch = curl_init(); |
|
180
|
|
|
|
|
181
|
|
|
curl_setopt($ch, CURLOPT_URL, $endpoint); |
|
182
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
183
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
|
184
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
185
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
186
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
|
187
|
|
|
|
|
188
|
|
|
if (in_array($method, [self::METHOD_POST, self::METHOD_PUT])) { |
|
189
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$result = curl_exec($ch); |
|
193
|
|
|
$error = curl_error($ch); |
|
194
|
|
|
$errno = curl_errno($ch); |
|
195
|
|
|
|
|
196
|
|
|
curl_close($ch); |
|
197
|
|
|
|
|
198
|
|
|
if ($error) { |
|
199
|
|
|
throw new B2BFamilyException($error, $errno); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$result = json_decode($result, true); |
|
203
|
|
|
|
|
204
|
|
|
if (isset($result['error'])) { |
|
205
|
|
|
throw new B2BFamilyException($result['error']['message'], $result['error']['code']); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $result; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Возвращает md5 хеш параметров для авторизации |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
4 |
|
private function getHash() |
|
217
|
|
|
{ |
|
218
|
4 |
|
return md5(implode('&', [ |
|
219
|
4 |
|
'appkey=' . $this->parameters->getAuth('appkey'), |
|
220
|
4 |
|
'secret=' . $this->parameters->getAuth('secret'), |
|
221
|
4 |
|
'email=' . $this->parameters->getAuth('email'), |
|
222
|
4 |
|
'password=' . $this->parameters->getAuth('password'), |
|
223
|
4 |
|
])); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
class B2BFamilyException extends \Exception |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.