20 public const TYPE_CODE = LeadAds\Service::TYPE_FACEBOOK;
22 public const URL_FORM_LIST =
'https://www.facebook.com/ads/manager/audiences/manage/';
25 [
'CRM_NAME' => LeadAds\Field::TYPE_COMPANY_NAME,
'ADS_NAME' =>
'COMPANY_NAME'],
26 [
'CRM_NAME' => LeadAds\Field::TYPE_NAME,
'ADS_NAME' =>
'FIRST_NAME'],
27 [
'CRM_NAME' => LeadAds\Field::TYPE_LAST_NAME,
'ADS_NAME' =>
'LAST_NAME'],
28 [
'CRM_NAME' => LeadAds\Field::TYPE_EMAIL,
'ADS_NAME' =>
'EMAIL'],
29 [
'CRM_NAME' => LeadAds\Field::TYPE_PHONE,
'ADS_NAME' =>
'PHONE'],
30 [
'CRM_NAME' => LeadAds\Field::TYPE_WORK_EMAIL,
'ADS_NAME' =>
'WORK_EMAIL'],
31 [
'CRM_NAME' => LeadAds\Field::TYPE_WORK_PHONE,
'ADS_NAME' =>
'WORK_PHONE_NUMBER'],
32 [
'CRM_NAME' => LeadAds\Field::TYPE_JOB_TITLE,
'ADS_NAME' =>
'JOB_TITLE'],
33 [
'CRM_NAME' => LeadAds\Field::TYPE_MILITARY_STATUS,
'ADS_NAME' =>
'MILITARY_STATUS'],
34 [
'CRM_NAME' => LeadAds\Field::TYPE_MARITIAL_STATUS,
'ADS_NAME' =>
'MARITIAL_STATUS'],
35 [
'CRM_NAME' => LeadAds\Field::TYPE_GENDER,
'ADS_NAME' =>
'GENDER'],
36 [
'CRM_NAME' => LeadAds\Field::TYPE_BIRTHDAY,
'ADS_NAME' =>
'DOB'],
37 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_COUNTRY,
'ADS_NAME' =>
'COUNTRY'],
38 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_STATE,
'ADS_NAME' =>
'STATE'],
39 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_CITY,
'ADS_NAME' =>
'CITY'],
40 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_STREET_ADDRESS,
'ADS_NAME' =>
'STREET_ADDRESS'],
41 [
'CRM_NAME' => LeadAds\Field::TYPE_FULL_NAME,
'ADS_NAME' =>
'FULL_NAME'],
42 [
'CRM_NAME' => LeadAds\Field::TYPE_LOCATION_ZIP,
'ADS_NAME' =>
'ZIP'],
43 [
'CRM_NAME' => LeadAds\Field::TYPE_RELATIONSHIP_STATUS,
'ADS_NAME' =>
'RELATIONSHIP_STATUS'],
44 [
'CRM_NAME' => LeadAds\Field::TYPE_CPF,
'ADS_NAME' =>
'ID_CPF'],
45 [
'CRM_NAME' => LeadAds\Field::TYPE_DNI_ARGENTINA,
'ADS_NAME' =>
'ID_AR_DNI'],
46 [
'CRM_NAME' => LeadAds\Field::TYPE_DNI_PERU,
'ADS_NAME' =>
'ID_PE_DNI'],
47 [
'CRM_NAME' => LeadAds\Field::TYPE_RUT,
'ADS_NAME' =>
'ID_CL_RUT'],
48 [
'CRM_NAME' => LeadAds\Field::TYPE_CC,
'ADS_NAME' =>
'ID_CO_CC'],
49 [
'CRM_NAME' => LeadAds\Field::TYPE_CI,
'ADS_NAME' =>
'ID_EC_CI'],
50 [
'CRM_NAME' => LeadAds\Field::TYPE_DATE_TIME,
'ADS_NAME' =>
'DATE_TIME'],
74 return mb_strtolower($languageId).
'_'.mb_strtoupper($languageId);
96 $mapper = static::getFieldMapper();
98 if ($adsName = $mapper->getAdsName($field->getName()))
100 return [
'type' => $adsName,
'key' => $field->getKey()];
105 'label' => $field->getLabel(),
106 'key' => $field->getKey()
108 if (!empty($field->getOptions()))
110 $item[
'options'] = array_map(
111 static function ($option)
114 'value' => $option[
'label'],
115 'key' => $option[
'key']
135 $questions = static::convertFields($data[
'FIELDS']);
136 $privacyPolicy = array(
137 'url' => $data[
'PRIVACY_POLICY_URL']
142 'style' =>
'PARAGRAPH_STYLE',
144 'button_text' => $data[
'BUTTON_CAPTION']
148 $contextCard[
'title'] = $data[
'TITLE'];
150 if ($data[
'DESCRIPTION'])
152 $contextCard[
'content'] = [$data[
'DESCRIPTION']];
154 elseif ($data[
'TITLE'])
156 $contextCard[
'content'] = [$data[
'TITLE']];
160 'methodName' =>
'leadads.form.create',
162 'page_id'=> $this->accountId,
164 'name' => $data[
'NAME'],
165 'privacy_policy' => $privacyPolicy,
166 'follow_up_action_url' => $data[
'SUCCESS_URL'],
167 'locale' => mb_strtoupper($locale),
168 'context_card' => $contextCard,
169 'questions' => $questions
174 if (!$response->isSuccess() || !
$formId = $response->getData()[
"id"] ??
null)
181 if (!$subscribeResult->isSuccess())
183 $response->addError(
new Error(
'Can not subscribe App to Page events.'));
189 $response->addError(
new Error(
'Can not register Form web hook.'));
203 public function unlink(
string $id) : bool
211 'methodName' =>
'leadads.event.subscribe',
213 'page_id' => $this->accountId,
215 'subscribed_fields' => [
'leadgen'],
227 'methodName' =>
'leadads.form.list',
229 'page_id' => $this->accountId,
231 'privacy_policy_url',
237 'follow_up_action_url',
238 'tracking_parameters',
250 ...iterator_to_array($paginator)
266 'methodName' =>
'leadads.lead.get',
268 'lead_id' => $item->getLeadId()
272 if (!$response->isSuccess())
274 foreach ($response->getErrors() as $error)
276 $result->addError(
new Error(
'Can not retrieve result. ' . $error->getMessage()));
282 if (!$responseData = $response->getData())
284 $result->addError(
new Error(
'Can not retrieve result. Empty data.'));
289 if (!isset($responseData[
'id']) || !$responseData[
'id'])
291 $result->addError(
new Error(
'Can not retrieve result. Empty `id`.'));
296 if (!isset($responseData[
'field_data']) || !is_array($responseData[
'field_data']) || !$responseData[
'field_data'])
298 $result->addError(
new Error(
'Can not retrieve result. Empty `field_data`.'));
303 $result->setId($item->getLeadId());
304 foreach ($responseData[
'field_data'] as $field)
306 if (!isset($field[
'name']) || !$field[
'name'])
311 if (!isset($field[
'values']) || !$field[
'values'])
316 if (!is_array($field[
'values']))
318 $field[
'values'] = array($field[
'values']);
321 $result->addFieldValues($field[
'name'], $field[
'values']);
339 'methodName' =>
'leadads.form.get',
341 'page_id' => $this->accountId,
344 'privacy_policy_url',
350 'follow_up_action_url',
351 'tracking_parameters',
373 ->addError(
new Error(
'Facebook lead ads form register: Empty formId.'))
376 if (!isset($this->accountId))
379 ->addError(
new Error(
'Facebook lead ads form register: Empty accountId.'))
384 if (!$subscribeResult->isSuccess())
386 return $subscribeResult;
392 $result->addError(
new Error(
'Can not register Form web hook.'));