| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Encharge Record Api |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\Encharge; |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
|
| 12 |
/** |
| 13 |
* Provide functionality for Record insert |
| 14 |
*/ |
| 15 |
class RecordApiHelper |
| 16 |
{ |
| 17 |
private $_defaultHeader; |
| 18 |
private $_integrationID; |
| 19 |
private $_logID; |
| 20 |
private $_logResponse; |
| 21 |
|
| 22 |
public function __construct($api_key, $integId, $logID, $entryID) |
| 23 |
{ |
| 24 |
// wp_send_json_success($tokenDetails); |
| 25 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 26 |
$this->_defaultHeader['X-Encharge-Token'] = $api_key; |
| 27 |
$this->_integrationID = $integId; |
| 28 |
$this->_logID = $logID; |
| 29 |
$this->_logResponse = new UtilApiResponse(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* serd data to api |
| 34 |
* @return json response |
| 35 |
*/ |
| 36 |
public function insertRecord($data) |
| 37 |
{ |
| 38 |
$insertRecordEndpoint = 'https://api.encharge.io/v1/people'; |
| 39 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 40 |
} |
| 41 |
|
| 42 |
public function executeRecordApi($fieldValues, $fieldMap, $tags) |
| 43 |
{ |
| 44 |
$fieldData = []; |
| 45 |
|
| 46 |
foreach ($fieldMap as $fieldKey => $fieldPair) { |
| 47 |
if (!empty($fieldPair->enChargeFields)) { |
| 48 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 49 |
$fieldData[$fieldPair->enChargeFields] = $fieldPair->customValue; |
| 50 |
} else { |
| 51 |
$fieldData[$fieldPair->enChargeFields] = $fieldValues[$fieldPair->formField]; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
if (null !== $tags) { |
| 56 |
$fieldData['tags'] = $tags; |
| 57 |
} |
| 58 |
// wp_send_json_success($fieldData); |
| 59 |
$recordApiResponse = $this->insertRecord(json_encode($fieldData)); |
| 60 |
$type = 'insert'; |
| 61 |
|
| 62 |
if ($recordApiResponse && isset($recordApiResponse->user)) { |
| 63 |
$recordApiResponse = [ |
| 64 |
'status' => 'success', |
| 65 |
'email' => $recordApiResponse->user->email |
| 66 |
]; |
| 67 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $recordApiResponse); |
| 68 |
} else { |
| 69 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse); |
| 70 |
} |
| 71 |
return $recordApiResponse; |
| 72 |
} |
| 73 |
} |
| 74 |
|