| 1 |
<?php |
| 2 |
/** |
| 3 |
* MailChimp Record Api |
| 4 |
* |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\MailChimp; |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provide functionality for Record insert,upsert |
| 15 |
*/ |
| 16 |
class RecordApiHelper |
| 17 |
{ |
| 18 |
private $_defaultHeader; |
| 19 |
private $_tokenDetails; |
| 20 |
private $_integrationID; |
| 21 |
private $_logID; |
| 22 |
private $_logResponse; |
| 23 |
private $_entryID; |
| 24 |
|
| 25 |
public function __construct($tokenDetails, $integId, $logID, $entryID) |
| 26 |
{ |
| 27 |
$this->_defaultHeader['Authorization'] = "Bearer {$tokenDetails->access_token}"; |
| 28 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 29 |
$this->_tokenDetails = $tokenDetails; |
| 30 |
$this->_integrationID = $integId; |
| 31 |
$this->_logID = $logID; |
| 32 |
$this->_logResponse = new ApiResponse(); |
| 33 |
$this->_entryID = $entryID; |
| 34 |
} |
| 35 |
|
| 36 |
private function _apiEndPoint() |
| 37 |
{ |
| 38 |
return "https://{$this->_tokenDetails->dc}.api.mailchimp.com/3.0"; |
| 39 |
} |
| 40 |
|
| 41 |
public function insertRecord($listId, $data) |
| 42 |
{ |
| 43 |
$insertRecordEndpoint = $this->_apiEndPoint() . "/lists/{$listId}/members"; |
| 44 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 45 |
} |
| 46 |
|
| 47 |
public function updateRecord($listId, $contactId, $data) |
| 48 |
{ |
| 49 |
$updateRecordEndpoint = $this->_apiEndPoint() . "/lists/{$listId}/members/{$contactId}"; |
| 50 |
return HttpHelper::request($updateRecordEndpoint, 'put', $data, $this->_defaultHeader); |
| 51 |
} |
| 52 |
|
| 53 |
public function existContact($listId, $queryParam) |
| 54 |
{ |
| 55 |
$existSearchEnpoint = $this->_apiEndPoint() . "/search-members?query={$queryParam}&list_id={$listId}"; |
| 56 |
return HttpHelper::get($existSearchEnpoint, $queryParam, $this->_defaultHeader); |
| 57 |
} |
| 58 |
|
| 59 |
public function executeRecordApi($listId, $tags, $defaultConf, $fieldValues, $fieldMap, $actions, $addressFields) |
| 60 |
{ |
| 61 |
$fieldData = []; |
| 62 |
$mergeFields = []; |
| 63 |
foreach ($fieldMap as $fieldPair) { |
| 64 |
if (!empty($fieldPair->mailChimpField)) { |
| 65 |
if ('email_address' === $fieldPair->mailChimpField) { |
| 66 |
$fieldData['email_address'] = $fieldValues[$fieldPair->formField]; |
| 67 |
} elseif ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 68 |
$mergeFields[$fieldPair->mailChimpField] = $fieldPair->customValue; |
| 69 |
} else { |
| 70 |
$mergeFields[$fieldPair->mailChimpField] = $fieldValues[$fieldPair->formField]; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$doubleOptIn = !empty($actions->double_opt_in) && $actions->double_opt_in ? true : false; |
| 76 |
|
| 77 |
$fieldData['merge_fields'] = (object) $mergeFields; |
| 78 |
$fieldData['email_type'] = 'text'; |
| 79 |
$fieldData['tags'] = !empty($tags) ? $tags : []; |
| 80 |
$fieldData['status'] = $doubleOptIn ? 'pending' : 'subscribed'; |
| 81 |
$fieldData['double_optin'] = $doubleOptIn; |
| 82 |
|
| 83 |
$model = new FormEntryLogModel(); |
| 84 |
|
| 85 |
$recordApiResponse = null; |
| 86 |
if ($this->_entryID) { |
| 87 |
$result = $model->entryLogCheck($this->_entryID, $this->_integrationID); |
| 88 |
if (!count($result) || isset($result->errors['result_empty'])) { |
| 89 |
if (!empty($actions->address)) { |
| 90 |
$fvalue = []; |
| 91 |
foreach ($addressFields as $key) { |
| 92 |
foreach ($fieldValues as $k => $v) { |
| 93 |
if ($key->formField === $k) { |
| 94 |
$fvalue[$key->mailChimpAddressField] = $v; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
$fieldData['merge_fields']->ADDRESS = (object) $fvalue; |
| 99 |
} |
| 100 |
|
| 101 |
$recordApiResponse = $this->insertRecord($listId, wp_json_encode($fieldData)); |
| 102 |
$type = 'insert'; |
| 103 |
if (!empty($actions->update) && !empty($recordApiResponse->title) && 'Member Exists' === $recordApiResponse->title) { |
| 104 |
$contactEmail = $fieldData['email_address']; |
| 105 |
$foundContact = $this->existContact($listId, $contactEmail); |
| 106 |
if (count($foundContact->exact_matches->members)) { |
| 107 |
$contactId = $foundContact->exact_matches->members[0]->id; |
| 108 |
$recordApiResponse = $this->updateRecord($listId, $contactId, wp_json_encode($fieldData)); |
| 109 |
$type = 'update'; |
| 110 |
} |
| 111 |
} |
| 112 |
} else { |
| 113 |
if (!empty($actions->address)) { |
| 114 |
$fvalue = []; |
| 115 |
foreach ($addressFields as $key) { |
| 116 |
foreach ($fieldValues as $k => $v) { |
| 117 |
if ($key->formField === $k) { |
| 118 |
$fvalue[$key->mailChimpAddressField] = $v; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
$fieldData['merge_fields']->ADDRESS = (object) $fvalue; |
| 123 |
} |
| 124 |
$contactId = json_decode($result[0]->response_obj); |
| 125 |
$recordApiResponse = $this->updateRecord($listId, $contactId, wp_json_encode($fieldData)); |
| 126 |
$type = 'update'; |
| 127 |
} |
| 128 |
|
| 129 |
if (400 === $recordApiResponse->status) { |
| 130 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse); |
| 131 |
} else { |
| 132 |
$responseInfo = (object) [ |
| 133 |
'success' => true, |
| 134 |
'id' => $recordApiResponse->id, |
| 135 |
'status' => $recordApiResponse->status, |
| 136 |
'email_address' => $recordApiResponse->email_address, |
| 137 |
]; |
| 138 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $responseInfo); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $recordApiResponse; |
| 143 |
} |
| 144 |
} |
| 145 |
|