-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathSalesforce.class.php
76 lines (63 loc) · 2.12 KB
/
Salesforce.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
class Salesforce
{
const
API_URL = 'https://api.salesforceiq.com/v2/',
DEFAULT_LIST_ID = '58387a94e4b0a1fea2c76f4a';
protected static $curlOptions = [
'headers' => ['Accept: application/json', 'Content-type: application/json'],
'json_response' => true,
'json_data' => true,
'timeout' => 10
];
public static function createContact(string $email, string $initialListId = null, string $acquisitionChannel = null)
{
$contactData = [
'properties' => [
'email' => [[
'value' => $email,
'metadata' => ['primary' => true]
]]
]
];
$contactId = static::post('contacts?_upsert=email', $contactData)['id'] ?? null;
if ($initialListId) {
if (!$contactId) {
throw new SalesforceException('Failed to generate or update contact');
}
static::post('lists/' . $initialListId . '/listitems?_upsert=contactIds', [
'contactIds' => [$contactId],
'fieldValues' => (object)[
'2' => [['raw' => $acquisitionChannel]]
]
]);
}
}
protected static function getApiUserPassword()
{
$userpw = Config::get(Config::SALESFORCE_KEY) . ':' . Config::get(Config::SALESFORCE_SECRET);
if ($userpw[0] === ':' || substr($userpw, -1) === ':') {
throw new SalesforceException('Salesforce key and/or secret not configured correctly');
}
return $userpw;
}
public static function get($endpoint, array $data = [])
{
$options = [
'password' => static::getApiUserPassword(),
] + static::$curlOptions;
$responseData = Curl::get(static::API_URL . $endpoint, $data, $options);
return $responseData ?? [];
}
public static function post($endpoint, array $data = [], array $options = [])
{
$options += [
'password' => static::getApiUserPassword(),
] + static::$curlOptions;
$responseData = Curl::post(static::API_URL . $endpoint, $data, $options);
return $responseData ?? [];
}
}
class SalesforceException extends Exception
{
}