Client.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Apiconnector;
  3. /**
  4. * dotmailer REST V2 api client.
  5. *
  6. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  7. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  8. */
  9. class Client extends \Dotdigitalgroup\Email\Model\Apiconnector\Rest
  10. {
  11. const APICONNECTOR_VERSION = 'V2';
  12. const REST_WAIT_UPLOAD_TIME = 5;
  13. //rest api data
  14. const REST_ACCOUNT_INFO = 'https://r1-api.dotmailer.com/v2/account-info';
  15. const REST_CONTACTS = '/v2/contacts/';
  16. const REST_CONTACT_WITH_CONSENT = '/v2/contacts/with-consent';
  17. const REST_CONTACT_WITH_CONSENT_AND_PREFERENCES = '/v2/contacts/with-consent-and-preferences';
  18. const REST_CONTACTS_IMPORT = '/v2/contacts/import/';
  19. const REST_ADDRESS_BOOKS = '/v2/address-books/';
  20. const REST_DATA_FILEDS = '/v2/data-fields';
  21. const REST_TRANSACTIONAL_DATA_IMPORT = '/v2/contacts/transactional-data/import/';
  22. const REST_TRANSACTIONAL_DATA = '/v2/contacts/transactional-data/';
  23. const REST_CAMPAIGN_SEND = '/v2/campaigns/send';
  24. const REST_CONTACTS_SUPPRESSED_SINCE = '/v2/contacts/suppressed-since/';
  25. const REST_DATA_FIELDS_CAMPAIGNS = '/v2/campaigns';
  26. const REST_CONTACTS_RESUBSCRIBE = '/v2/contacts/resubscribe';
  27. const REST_CAMPAIGN_FROM_ADDRESS_LIST = '/v2/custom-from-addresses';
  28. const REST_CREATE_CAMPAIGN = '/v2/campaigns';
  29. const REST_PROGRAM = '/v2/programs/';
  30. const REST_PROGRAM_ENROLMENTS = '/v2/programs/enrolments';
  31. const REST_TEMPLATES = '/v2/templates';
  32. const REST_SEND_TRANSACTIONAL_EMAIL = '/v2/email';
  33. const REST_CAMPAIGNS_WITH_PREPARED_CONTENT = 'prepared-for-transactional-email';
  34. //rest error responces
  35. const API_ERROR_API_EXCEEDED = 'Your account has generated excess API activity and is being temporarily capped.
  36. Please contact support. ERROR_APIUSAGE_EXCEEDED';
  37. const API_ERROR_TRANS_ALLOWANCE = 'TransactionalDataAllowanceInMegabytes';
  38. const API_ERROR_EMAIL_NOT_VALID = 'Email is not a valid email address. ERROR_PARAMETER_INVALID';
  39. const API_ERROR_FEATURENOTACTIVE = 'Error: ERROR_FEATURENOTACTIVE';
  40. const API_ERROR_REPORT_NOT_FOUND =
  41. 'Import is not processed yet or completed with error. ERROR_IMPORT_REPORT_NOT_FOUND';
  42. const API_ERROR_TRANS_NOT_EXISTS = 'Error: ERROR_TRANSACTIONAL_DATA_DOES_NOT_EXIST';
  43. const API_ERROR_DATAFIELD_EXISTS = 'Field already exists. ERROR_NON_UNIQUE_DATAFIELD';
  44. const API_ERROR_CONTACT_NOT_FOUND = 'Error: ERROR_CONTACT_NOT_FOUND';
  45. const API_ERROR_PROGRAM_NOT_ACTIVE = 'Error: ERROR_PROGRAM_NOT_ACTIVE';
  46. const API_ERROR_ENROLMENT_EXCEEDED = 'Error: ERROR_ENROLMENT_ALLOWANCE_EXCEEDED ';
  47. const API_ERROR_SEND_NOT_PERMITTED = 'Send not permitted at this time. ERROR_CAMPAIGN_SENDNOTPERMITTED';
  48. const API_ERROR_CONTACT_SUPPRESSED = 'Contact is suppressed. ERROR_CONTACT_SUPPRESSED';
  49. const API_ERROR_AUTHORIZATION_DENIED = 'Authorization has been denied for this request.';
  50. const API_ERROR_ADDRESSBOOK_NOT_FOUND = 'Error: ERROR_ADDRESSBOOK_NOT_FOUND';
  51. const API_ERROR_ADDRESSBOOK_DUPLICATE
  52. = 'That name is in use already, please choose another. ERROR_ADDRESSBOOK_DUPLICATE';
  53. const REST_ACCOUNT_PREFERENCES = "/v2/preferences";
  54. /**
  55. * @var \Dotdigitalgroup\Email\Helper\File
  56. */
  57. private $fileHelper;
  58. /**
  59. * @var \Dotdigitalgroup\Email\Helper\Data
  60. */
  61. private $helper;
  62. /**
  63. * @var string
  64. */
  65. private $apiEndpoint;
  66. /**
  67. * Client constructor.
  68. *
  69. * @param \Dotdigitalgroup\Email\Helper\Data $data
  70. * @param \Dotdigitalgroup\Email\Helper\File $fileHelper
  71. */
  72. public function __construct(
  73. \Dotdigitalgroup\Email\Helper\Data $data,
  74. \Dotdigitalgroup\Email\Helper\File $fileHelper
  75. ) {
  76. $this->helper = $data;
  77. $this->fileHelper = $fileHelper;
  78. parent::__construct($this->helper);
  79. }
  80. /**
  81. * Excluded api response that we don't want to send.
  82. *
  83. * @var array
  84. */
  85. public $exludeMessages
  86. = [
  87. self::API_ERROR_FEATURENOTACTIVE,
  88. self::API_ERROR_PROGRAM_NOT_ACTIVE,
  89. self::API_ERROR_CONTACT_SUPPRESSED,
  90. self::API_ERROR_DATAFIELD_EXISTS,
  91. self::API_ERROR_AUTHORIZATION_DENIED,
  92. self::API_ERROR_ENROLMENT_EXCEEDED,
  93. self::API_ERROR_SEND_NOT_PERMITTED,
  94. self::API_ERROR_TRANS_NOT_EXISTS,
  95. self::API_ERROR_ADDRESSBOOK_NOT_FOUND,
  96. ];
  97. /**
  98. * @param string $apiEndpoint
  99. * @return void
  100. */
  101. public function setApiEndpoint($apiEndpoint)
  102. {
  103. $this->apiEndpoint = trim($apiEndpoint);
  104. }
  105. /**
  106. * @return string
  107. * @throws \Magento\Framework\Exception\LocalizedException
  108. */
  109. public function getApiEndpoint()
  110. {
  111. if (!isset($this->apiEndpoint)) {
  112. throw new \Magento\Framework\Exception\LocalizedException(
  113. __('Dotmailer connector API endpoint cannot be empty.')
  114. );
  115. }
  116. return $this->apiEndpoint;
  117. }
  118. /**
  119. * Api validation.
  120. *
  121. * @param string $apiUsername
  122. * @param string $apiPassword
  123. *
  124. * @return bool|mixed
  125. */
  126. public function validate($apiUsername, $apiPassword)
  127. {
  128. if ($apiUsername && $apiPassword) {
  129. $this->setApiUsername($apiUsername)
  130. ->setApiPassword($apiPassword);
  131. $accountInfo = $this->getAccountInfo();
  132. if (isset($accountInfo->message)) {
  133. $message = 'VALIDATION ERROR : ' . $accountInfo->message;
  134. $this->helper->debug('validate', [$message]);
  135. return false;
  136. }
  137. return $accountInfo;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Gets a contact by ID. Unsubscribed or suppressed contacts will not be retrieved.
  143. *
  144. * @param string $id
  145. *
  146. * @return null
  147. * @throws \Exception
  148. */
  149. public function getContactById($id)
  150. {
  151. $url = $this->getApiEndpoint() . self::REST_CONTACTS . $id;
  152. $this->setUrl($url)
  153. ->setVerb('GET');
  154. $response = $this->execute();
  155. if (isset($response->message)) {
  156. $message = 'GET CONTACT INFO ID ' . $url . ', ' . $response->message;
  157. $this->helper->debug('getContactById', [$message]);
  158. }
  159. return $response;
  160. }
  161. /**
  162. * Bulk creates, or bulk updates, contacts. Import format can either be CSV or Excel.
  163. * Must include one column called "Email". Any other columns will attempt to map to your custom data fields.
  164. * The ID of returned object can be used to query import progress.
  165. *
  166. * @param string|int $filename
  167. * @param string|int $addressBookId
  168. *
  169. * @return mixed
  170. */
  171. public function postAddressBookContactsImport($filename, $addressBookId)
  172. {
  173. $url = $this->getApiEndpoint() . "/v2/address-books/{$addressBookId}/contacts/import";
  174. $ch = curl_init($url);
  175. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  176. curl_setopt(
  177. $ch,
  178. CURLOPT_USERPWD,
  179. $this->getApiUsername() . ':' . $this->getApiPassword()
  180. );
  181. //case the deprication of @filename for uploading
  182. if (function_exists('curl_file_create')) {
  183. $args['file']
  184. = curl_file_create(
  185. $this->fileHelper->getFilePathWithFallback($filename),
  186. 'text/csv'
  187. );
  188. curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
  189. } else {
  190. //standart use of curl file
  191. curl_setopt($ch, CURLOPT_POSTFIELDS, [
  192. 'file' => '@' . $this->fileHelper->getFilePathWithFallback($filename),
  193. ]);
  194. }
  195. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  196. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  197. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  198. 'Content-Type: multipart/form-data',
  199. ]);
  200. // send contacts to address book
  201. $result = curl_exec($ch);
  202. $result = json_decode($result);
  203. if (isset($result->message)) {
  204. $message = 'postAddressBookContactsImport' . $addressBookId . ' file : ' . $filename
  205. . ' ,user : ' . $this->getApiUsername() . '. ' . $result->message;
  206. $this->helper->debug('postAddressBookContactsImport', [$message]);
  207. }
  208. return $result;
  209. }
  210. /**
  211. * Adds a contact to a given address book.
  212. *
  213. * @param string|int $addressBookId
  214. * @param string|int $apiContact
  215. *
  216. * @return mixed|null
  217. */
  218. public function postAddressBookContacts($addressBookId, $apiContact)
  219. {
  220. $url = $this->getApiEndpoint() . self::REST_ADDRESS_BOOKS . $addressBookId
  221. . '/contacts';
  222. $this->setUrl($url)
  223. ->setVerb('POST')
  224. ->buildPostBody($apiContact);
  225. $response = $this->execute();
  226. if (isset($response->message)) {
  227. $message = 'POST ADDRESS BOOK CONTACTS ' . $url . ', '
  228. . $response->message;
  229. $this->helper->debug('postAddressBookContacts', [$message]);
  230. }
  231. return $response;
  232. }
  233. /**
  234. * Deletes all contacts from a given address book.
  235. *
  236. * @param string|int $addressBookId
  237. * @param string|int $contactId
  238. *
  239. * @return null
  240. */
  241. public function deleteAddressBookContact($addressBookId, $contactId)
  242. {
  243. //Only if there is a contact id and address book id
  244. if ($addressBookId && $contactId) {
  245. $url = $this->getApiEndpoint() . self::REST_ADDRESS_BOOKS . $addressBookId
  246. . '/contacts/' . $contactId;
  247. $this->setUrl($url)
  248. ->setVerb('DELETE');
  249. $this->execute();
  250. $this->helper->log(sprintf('Delete-contact %s from addressbook %s', $contactId, $addressBookId));
  251. }
  252. }
  253. /**
  254. * Gets a report with statistics about what was successfully imported, and what was unable to be imported.
  255. *
  256. * @param string|int $importId
  257. *
  258. * @return mixed
  259. */
  260. public function getContactsImportReport($importId)
  261. {
  262. $url = $this->getApiEndpoint() . self::REST_CONTACTS_IMPORT . $importId
  263. . '/report';
  264. $this->setUrl($url)
  265. ->setVerb('GET');
  266. $response = $this->execute();
  267. if (isset($response->message)) {
  268. $message = 'GET CONTACTS IMPORT REPORT . ' . $url . ' message : '
  269. . $response->message;
  270. $this->helper->debug('getContactsImportReport', [$message]);
  271. }
  272. return $response;
  273. }
  274. /**
  275. * Gets a contact by email address.
  276. *
  277. * @param string $email
  278. *
  279. * @return mixed
  280. */
  281. public function getContactByEmail($email)
  282. {
  283. $url = $this->getApiEndpoint() . self::REST_CONTACTS . $email;
  284. $this->setUrl($url)
  285. ->setVerb('GET');
  286. $response = $this->execute();
  287. if (isset($response->message)) {
  288. $message = 'GET CONTACT BY email : ' . $email . ' '
  289. . $response->message;
  290. $this->helper->debug('getContactByEmail', [$message]);
  291. }
  292. return $response;
  293. }
  294. /**
  295. * Get all address books.
  296. *
  297. * @return mixed
  298. *
  299. * @throws \Exception
  300. */
  301. public function getAddressBooks()
  302. {
  303. $url = $this->getApiEndpoint() . self::REST_ADDRESS_BOOKS;
  304. $this->setUrl($url)
  305. ->setVerb('GET');
  306. $response = $this->execute();
  307. if (isset($response->message)) {
  308. $message = 'GET ALL ADDRESS BOOKS : ' . $url . ', '
  309. . $response->message;
  310. $this->helper->debug('getAddressBooks', [$message]);
  311. }
  312. return $response;
  313. }
  314. /**
  315. * Gets an address book by ID.
  316. *
  317. * @param int $id
  318. *
  319. * @return null
  320. * @throws \Exception
  321. */
  322. public function getAddressBookById($id)
  323. {
  324. $url = $this->getApiEndpoint() . self::REST_ADDRESS_BOOKS . $id;
  325. $this->setUrl($url)
  326. ->setVerb('GET');
  327. $response = $this->execute();
  328. if (isset($response->message)) {
  329. $message = 'GET ADDRESS BOOK BY ID ' . $id . ', ' . $response->message;
  330. $this->helper->debug('getAddressBookById', [$message]);
  331. }
  332. return $response;
  333. }
  334. /**
  335. * Creates an address book.
  336. *
  337. * @param string $name
  338. * @param string $visibility
  339. *
  340. * @return null
  341. * @throws \Exception
  342. */
  343. public function postAddressBooks($name, $visibility = 'Public')
  344. {
  345. $data = [
  346. 'Name' => $name,
  347. 'Visibility' => $visibility,
  348. ];
  349. $url = $this->getApiEndpoint() . self::REST_ADDRESS_BOOKS;
  350. $this->setUrl($url)
  351. ->setVerb('POST')
  352. ->buildPostBody($data);
  353. $response = $this->execute();
  354. if (isset($response->message)) {
  355. $this->helper->debug('postAddressBooks', $data);
  356. }
  357. return $response;
  358. }
  359. /**
  360. * Get list of all campaigns.
  361. *
  362. * @return mixed
  363. *
  364. * @throws \Exception
  365. */
  366. public function getCampaigns()
  367. {
  368. $url = $this->getApiEndpoint() . self::REST_DATA_FIELDS_CAMPAIGNS;
  369. $this->setUrl($url)
  370. ->setVerb('GET');
  371. $response = $this->execute();
  372. if (isset($response->message)) {
  373. $message = 'GET CAMPAIGNS ' . $response->message . ' api user : '
  374. . $this->getApiUsername();
  375. $this->helper->debug('getCampaigns', [$message]);
  376. }
  377. return $response;
  378. }
  379. /**
  380. * @param int $campaignId
  381. * @return mixed
  382. */
  383. public function getCampaignById($campaignId)
  384. {
  385. $url = $this->getApiEndpoint() . self::REST_DATA_FIELDS_CAMPAIGNS . '/' . $campaignId;
  386. $this->setUrl($url)
  387. ->setVerb('GET');
  388. $response = $this->execute();
  389. if (isset($response->message)) {
  390. $message = 'GET CAMPAIGN BY ID ' . $response->message;
  391. $this->helper->log($message);
  392. }
  393. return $response;
  394. }
  395. /**
  396. * @param int $campaignId
  397. * @return mixed
  398. */
  399. public function getCampaignByIdWithPreparedContent($campaignId)
  400. {
  401. $url = $this->getApiEndpoint() . self::REST_DATA_FIELDS_CAMPAIGNS
  402. . '/' . $campaignId
  403. . '/' . self::REST_CAMPAIGNS_WITH_PREPARED_CONTENT
  404. . '/' . 'anonymouscontact@emailsim.io';
  405. $this->setUrl($url)
  406. ->setVerb('GET');
  407. $response = $this->execute();
  408. if (isset($response->message)) {
  409. $message = 'GET CAMPAIGN BY ID WITH PREPARED CONTENT' . $response->message;
  410. $this->helper->log($message);
  411. }
  412. return $response;
  413. }
  414. /**
  415. * Creates a data field within the account.
  416. *
  417. * @param $data string/array
  418. * @param string $type string, numeric, date, boolean
  419. * @param string $visibility public, private
  420. * @param bool $defaultValue
  421. *
  422. * @return object
  423. */
  424. public function postDataFields(
  425. $data,
  426. $type = 'String',
  427. $visibility = 'public',
  428. $defaultValue = false
  429. ) {
  430. $url = $this->getApiEndpoint() . self::REST_DATA_FILEDS;
  431. //set default value for the numeric datatype
  432. if ($type == 'numeric' && !$defaultValue) {
  433. $defaultValue = 0;
  434. }
  435. //set data for the string datatype
  436. if (is_string($data)) {
  437. $data = [
  438. 'Name' => $data,
  439. 'Type' => $type,
  440. 'Visibility' => $visibility,
  441. ];
  442. //default value
  443. if ($defaultValue) {
  444. $data['DefaultValue'] = $defaultValue;
  445. }
  446. }
  447. $this->setUrl($url)
  448. ->buildPostBody($data)
  449. ->setVerb('POST');
  450. $response = $this->execute();
  451. if (isset($response->message)) {
  452. $message = 'POST CREATE DATAFIELDS ' . $response->message;
  453. $this->helper->debug('postDataFields', [$message]);
  454. $this->helper->debug('postDataFields', $data);
  455. }
  456. return $response;
  457. }
  458. /**
  459. * Lists the data fields within the account.
  460. *
  461. * @return mixed
  462. */
  463. public function getDataFields()
  464. {
  465. $url = $this->getApiEndpoint() . self::REST_DATA_FILEDS;
  466. $this->setUrl($url)
  467. ->setVerb('GET');
  468. $response = $this->execute();
  469. if (isset($response->message)) {
  470. $message = 'GET ALL DATAFIELDS ' . $response->message;
  471. $this->helper->debug('getDataFields', [$message]);
  472. }
  473. return $response;
  474. }
  475. /**
  476. * Updates a contact.
  477. *
  478. * @param string|int $contactId
  479. * @param array $data
  480. *
  481. * @return object
  482. */
  483. public function updateContact($contactId, $data)
  484. {
  485. $url = $this->getApiEndpoint() . self::REST_CONTACTS . $contactId;
  486. $this->setUrl($url)
  487. ->setVerb('PUT')
  488. ->buildPostBody($data);
  489. $response = $this->execute();
  490. if (isset($response->message)) {
  491. $message = 'ERROR : UPDATE SINGLE CONTACT : ' . $url . ' message : '
  492. . $response->message;
  493. $this->helper->debug('updateContact', [$message]);
  494. $this->helper->debug('updateContact', $data);
  495. }
  496. return $response;
  497. }
  498. /**
  499. * Deletes a contact.
  500. *
  501. * @param int $contactId
  502. *
  503. * @return null
  504. * @throws \Exception
  505. */
  506. public function deleteContact($contactId)
  507. {
  508. if ($contactId) {
  509. $url = $this->getApiEndpoint() . self::REST_CONTACTS . $contactId;
  510. $this->setUrl($url)
  511. ->setVerb('DELETE');
  512. $response = $this->execute();
  513. if (isset($response->message)) {
  514. $message = ' url : ' . $url . ', ' . $response->message;
  515. $this->helper->debug('deleteContact', [$message]);
  516. }
  517. return $response;
  518. }
  519. }
  520. /**
  521. * Update contact datafields by email.
  522. *
  523. * @param string $email
  524. * @param array $dataFields
  525. *
  526. * @return mixed|null
  527. *
  528. * @throws \Exception
  529. */
  530. public function updateContactDatafieldsByEmail($email, $dataFields)
  531. {
  532. $apiContact = $this->postContacts($email);
  533. //do not create for non contact id set
  534. if (!isset($apiContact->id)) {
  535. return $apiContact;
  536. } else {
  537. //get the contact id for this email
  538. $contactId = $apiContact->id;
  539. }
  540. $data = [
  541. 'Email' => $email,
  542. 'EmailType' => 'Html',
  543. ];
  544. $data['DataFields'] = $dataFields;
  545. $url = $this->getApiEndpoint() . self::REST_CONTACTS
  546. . $contactId;
  547. $this->setUrl($url)
  548. ->setVerb('PUT')
  549. ->buildPostBody($data);
  550. $response = $this->execute();
  551. if (isset($response->message)) {
  552. $message = 'ERROR: UPDATE CONTACT DATAFIELD ' . $url . ' message : '
  553. . $response->message;
  554. $this->helper->debug('updateContactDatafieldsByEmail', [$message]);
  555. $this->helper->debug('updateContactDatafieldsByEmail', $data);
  556. }
  557. return $response;
  558. }
  559. /**
  560. * Sends a specified campaign to one or more address books, segments or contacts at a specified time.
  561. * Leave the address book array empty to send to All Contacts.
  562. *
  563. * @param int $campaignId
  564. * @param array $contacts
  565. *
  566. * @return mixed
  567. */
  568. public function postCampaignsSend($campaignId, $contacts)
  569. {
  570. $data = [
  571. 'username' => $this->getApiUsername(),
  572. 'password' => $this->getApiPassword(),
  573. 'campaignId' => $campaignId,
  574. 'ContactIds' => $contacts,
  575. ];
  576. $this->setUrl($this->getApiEndpoint() . self::REST_CAMPAIGN_SEND)
  577. ->setVerb('POST')
  578. ->buildPostBody($data);
  579. $response = $this->execute();
  580. if (isset($response->message)) {
  581. unset($data['password']);
  582. $message = 'SENDING CAMPAIGN ' . $response->message;
  583. $this->helper->debug('postCampaignsSend', [$message]);
  584. $this->helper->debug('postCampaignsSend', $data);
  585. }
  586. return $response;
  587. }
  588. /**
  589. * Creates a contact.
  590. *
  591. * @param string $email
  592. *
  593. * @return null
  594. * @throws \Exception
  595. */
  596. public function postContacts($email)
  597. {
  598. $url = $this->getApiEndpoint() . self::REST_CONTACTS;
  599. $data = [
  600. 'Email' => $email,
  601. 'EmailType' => 'Html',
  602. ];
  603. $this->setUrl($url)
  604. ->setVerb('POST')
  605. ->buildPostBody($data);
  606. $response = $this->execute();
  607. if (isset($response->message)) {
  608. $message = $email . ' , url ' . $url . ', ' . $response->message;
  609. $this->helper->debug('postContacts', [$message]);
  610. }
  611. return $response;
  612. }
  613. /**
  614. * Gets a list of suppressed contacts after a given date along with the reason for suppression.
  615. *
  616. * @param string $dateString
  617. * @param int $select
  618. * @param int $skip
  619. *
  620. * @return object
  621. */
  622. public function getContactsSuppressedSinceDate(
  623. $dateString,
  624. $select = 1000,
  625. $skip = 0
  626. ) {
  627. $url = $this->getApiEndpoint() . self::REST_CONTACTS_SUPPRESSED_SINCE
  628. . $dateString . '?select=' . $select . '&skip=' . $skip;
  629. $this->setUrl($url)
  630. ->setVerb('GET');
  631. $response = $this->execute();
  632. if (isset($response->message)) {
  633. $message = 'GET CONTACTS SUPPRESSED SINSE : ' . $dateString
  634. . ' select ' . $select . ' skip : ' . $skip . ' response : '
  635. . $response->message;
  636. $this->helper->debug('getContactsSuppressedSinceDate', [$message]);
  637. }
  638. return $response;
  639. }
  640. /**
  641. * Adds multiple pieces of transactional data to contacts asynchronously,
  642. * returning an identifier that can be used to check for import progress.
  643. * @param array $transactionalData
  644. * @param string $collectionName
  645. *
  646. * @return null
  647. * @throws \Exception
  648. */
  649. public function postContactsTransactionalDataImport(
  650. $transactionalData,
  651. $collectionName = 'Orders'
  652. ) {
  653. $orders = [];
  654. foreach ($transactionalData as $one) {
  655. if (isset($one['email'])) {
  656. $orders[] = [
  657. 'Key' => $one['id'],
  658. 'ContactIdentifier' => $one['email'],
  659. 'Json' => json_encode($one),
  660. ];
  661. }
  662. }
  663. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA_IMPORT
  664. . $collectionName;
  665. $this->setUrl($url)
  666. ->setVerb('POST')
  667. ->buildPostBody($orders);
  668. $response = $this->execute();
  669. if (isset($response->message)) {
  670. $message = ' SEND MULTI TRANSACTIONAL DATA ' . $response->message;
  671. $this->helper->debug('postContactsTransactionalDataImport', [$message]);
  672. }
  673. return $response;
  674. }
  675. /**
  676. * Adds a single piece of transactional data to a contact.
  677. *
  678. * @param array $data
  679. * @param string $collectionName
  680. *
  681. * @return null
  682. * @throws \Exception
  683. */
  684. public function postContactsTransactionalData(
  685. $data,
  686. $collectionName = 'Orders'
  687. ) {
  688. $order = $this->getContactsTransactionalDataByKey(
  689. $collectionName,
  690. $data['id']
  691. );
  692. if (!isset($order->key) || isset($order->message)
  693. && $order->message == self::API_ERROR_TRANS_NOT_EXISTS
  694. ) {
  695. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA
  696. . $collectionName;
  697. } else {
  698. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA
  699. . $collectionName . '/' . $order->key;
  700. }
  701. $apiData = [
  702. 'Key' => $data['id'],
  703. 'ContactIdentifier' => $data['email'],
  704. 'Json' => json_encode($data),
  705. ];
  706. $this->setUrl($url)
  707. ->setVerb('POST')
  708. ->buildPostBody($apiData);
  709. $response = $this->execute();
  710. if (isset($response->message)) {
  711. $message = 'POST CONTACTS TRANSACTIONAL DATA '
  712. . $response->message;
  713. $this->helper->debug('postContactsTransactionalData', [$message]);
  714. $this->helper->debug('postContactsTransactionalData', $apiData);
  715. }
  716. return $response;
  717. }
  718. /**
  719. * Adds a single piece of transactional data to account.
  720. *
  721. * @param array $data
  722. * @param string $collectionName
  723. *
  724. * @return null
  725. * @throws \Exception
  726. */
  727. public function postAccountTransactionalData(
  728. $data,
  729. $collectionName
  730. ) {
  731. $item = $this->getContactsTransactionalDataByKey(
  732. $collectionName,
  733. $data['id']
  734. );
  735. if (!isset($item->key) || isset($item->message)
  736. && $item->message == self::API_ERROR_TRANS_NOT_EXISTS
  737. ) {
  738. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA
  739. . $collectionName;
  740. } else {
  741. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA
  742. . $collectionName . '/' . $item->key;
  743. }
  744. $apiData = [
  745. 'Key' => $data['id'],
  746. 'ContactIdentifier' => 'account',
  747. 'Json' => json_encode($data),
  748. ];
  749. $this->setUrl($url)
  750. ->setVerb('POST')
  751. ->buildPostBody($apiData);
  752. $response = $this->execute();
  753. if (isset($response->message)) {
  754. $message = 'POST ACCOUNT TRANSACTIONAL DATA '
  755. . $response->message;
  756. $this->helper->debug('postAccountTransactionalData', [$message]);
  757. $this->helper->debug('postAccountTransactionalData', $apiData);
  758. }
  759. return $response;
  760. }
  761. /**
  762. * Gets a piece of transactional data by key.
  763. *
  764. * @param string $name
  765. * @param int $key
  766. *
  767. * @return null
  768. * @throws \Exception
  769. */
  770. public function getContactsTransactionalDataByKey($name, $key)
  771. {
  772. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA . $name . '/'
  773. . $key;
  774. $this->setUrl($url)
  775. ->setVerb('GET');
  776. return $this->execute();
  777. }
  778. /**
  779. * Deletes all transactional data for a contact.
  780. *
  781. * @param string $email
  782. * @param string $collectionName
  783. *
  784. * @return null
  785. * @throws \Exception
  786. */
  787. public function deleteContactTransactionalData(
  788. $email,
  789. $collectionName = 'Orders'
  790. ) {
  791. if ($email && $collectionName) {
  792. $url = $this->getApiEndpoint() . '/v2/contacts/' . $email
  793. . '/transactional-data/' . $collectionName;
  794. $this->setUrl($url)
  795. ->setVerb('DELETE');
  796. return $this->execute();
  797. }
  798. }
  799. /**
  800. * Gets a summary of information about the current status of the account.
  801. *
  802. * @return object
  803. *
  804. * @throws \Exception
  805. */
  806. public function getAccountInfo()
  807. {
  808. $url = self::REST_ACCOUNT_INFO;
  809. $this->setUrl($url)
  810. ->setVerb('GET');
  811. $response = $this->execute();
  812. if (isset($response->message)) {
  813. $message = 'GET ACCOUNT INFO for api user : '
  814. . $this->getApiUsername() . ' ' . $response->message;
  815. $this->helper->debug('getAccountInfo', [$message]);
  816. }
  817. return $response;
  818. }
  819. /**
  820. * Resubscribes a previously unsubscribed contact.
  821. *
  822. * @param array $apiContact
  823. *
  824. * @return object
  825. *
  826. * @throws \Exception
  827. */
  828. public function postContactsResubscribe($apiContact)
  829. {
  830. $url = $this->getApiEndpoint() . self::REST_CONTACTS_RESUBSCRIBE;
  831. $data = [
  832. 'UnsubscribedContact' => $apiContact,
  833. ];
  834. $this->setUrl($url)
  835. ->setVerb('POST')
  836. ->buildPostBody($data);
  837. $response = $this->execute();
  838. if (isset($response->message)) {
  839. $message = 'Resubscribe : ' . $url . ', message :'
  840. . $response->message;
  841. $this->helper->debug('postContactsResubscribe', [$message]);
  842. $this->helper->debug('postContactsResubscriber', $data);
  843. }
  844. return $response;
  845. }
  846. /**
  847. * Gets all custom from addresses which can be used in a campaign.
  848. *
  849. * @return object
  850. *
  851. * @throws \Exception
  852. */
  853. public function getCustomFromAddresses()
  854. {
  855. $url = $this->getApiEndpoint() . self::REST_CAMPAIGN_FROM_ADDRESS_LIST;
  856. $this->setUrl($url)
  857. ->setVerb('GET');
  858. $response = $this->execute();
  859. if (isset($response->message)) {
  860. $message = 'GET CampaignFromAddressList ' . $response->message
  861. . ' api user : ' . $this->getApiUsername();
  862. $this->helper->debug('getCustomFromAddresses', [$message]);
  863. }
  864. return $response;
  865. }
  866. /**
  867. * Creates a campaign.
  868. *
  869. * @param array $data
  870. *
  871. * @return null
  872. * @throws \Exception
  873. */
  874. public function postCampaign($data)
  875. {
  876. $url = $this->getApiEndpoint() . self::REST_CREATE_CAMPAIGN;
  877. $this->setUrl($url)
  878. ->setVerb('POST')
  879. ->buildPostBody($data);
  880. $response = $this->execute();
  881. if (isset($response->message)) {
  882. $message = $response->message;
  883. $this->helper->debug('postCampaign', [$message]);
  884. }
  885. return $response;
  886. }
  887. /**
  888. * Gets all programs.
  889. * https://apiconnector.com/v2/programs?select={select}&skip={skip}.
  890. *
  891. * @return object
  892. *
  893. * @throws \Exception
  894. */
  895. public function getPrograms()
  896. {
  897. $url = $this->getApiEndpoint() . self::REST_PROGRAM;
  898. $this->setUrl($url)
  899. ->setVerb('GET');
  900. $response = $this->execute();
  901. if (isset($response->message)) {
  902. $message = 'Get programs : ' . $response->message;
  903. $this->helper->debug('getPrograms', [$message]);
  904. }
  905. return $response;
  906. }
  907. /**
  908. * Creates an enrolment.
  909. *
  910. * @param array $data
  911. *
  912. * @return null
  913. * @throws \Exception
  914. */
  915. public function postProgramsEnrolments($data)
  916. {
  917. $url = $this->getApiEndpoint() . self::REST_PROGRAM_ENROLMENTS;
  918. $this->setUrl($url)
  919. ->setVerb('POST')
  920. ->buildPostBody($data);
  921. $response = $this->execute();
  922. if (isset($response->message)) {
  923. $message = 'Post programs enrolments : ' . $response->message;
  924. $this->helper->debug('postProgramsEnrolments', [$message]);
  925. $this->helper->debug('postProgramsEnrolments', $data);
  926. }
  927. return $response;
  928. }
  929. /**
  930. * Gets a program by id.
  931. *
  932. * @param int $id
  933. *
  934. * @return null
  935. * @throws \Exception
  936. */
  937. public function getProgramById($id)
  938. {
  939. $url = $this->getApiEndpoint() . self::REST_PROGRAM . $id;
  940. $this->setUrl($url)
  941. ->setVerb('GET');
  942. $response = $this->execute();
  943. if (isset($response->message)) {
  944. $message = 'Get program by id ' . $id . ', ' . $response->message;
  945. $this->helper->debug('getProgramById', [$message]);
  946. }
  947. return $response;
  948. }
  949. /**
  950. * Gets a summary of reporting information for a specified campaign.
  951. *
  952. * @param int $campaignId
  953. *
  954. * @return null
  955. * @throws \Exception
  956. */
  957. public function getCampaignSummary($campaignId)
  958. {
  959. $url = $this->getApiEndpoint() . '/v2/campaigns/' . $campaignId
  960. . '/summary';
  961. $this->setUrl($url)
  962. ->setVerb('GET');
  963. $response = $this->execute();
  964. if (isset($response->message)) {
  965. $message = 'Get Campaign Summary ' . $response->message
  966. . ' ,url : ' . $url;
  967. $this->helper->debug('title', [$message]);
  968. }
  969. return $response;
  970. }
  971. /**
  972. * Deletes a piece of transactional data by key.
  973. *
  974. * @param int $key
  975. * @param string $collectionName
  976. *
  977. * @return null
  978. * @throws \Exception
  979. */
  980. public function deleteContactsTransactionalData(
  981. $key,
  982. $collectionName = 'Orders'
  983. ) {
  984. if ($key && $collectionName) {
  985. $url = $this->getApiEndpoint() . '/v2/contacts/transactional-data/'
  986. . $collectionName . '/' . $key;
  987. $this->setUrl($url)
  988. ->setVerb('DELETE');
  989. $response = $this->execute();
  990. if (isset($response->message)) {
  991. $this->helper->debug(
  992. 'deleteContactsTransactionalData',
  993. ['DELETE CONTACTS TRANSACTIONAL DATA : ' . $url
  994. . ' ' . $response->message]
  995. );
  996. }
  997. return $response;
  998. }
  999. }
  1000. /**
  1001. * Adds a document to a campaign as an attachment.
  1002. *
  1003. * @param int $campaignId
  1004. * @param array $data
  1005. *
  1006. * @return null
  1007. * @throws \Exception
  1008. */
  1009. public function postCampaignAttachments($campaignId, $data)
  1010. {
  1011. $url = $this->getApiEndpoint() . self::REST_CREATE_CAMPAIGN
  1012. . "/$campaignId/attachments";
  1013. $this->setUrl($url)
  1014. ->setVerb('POST')
  1015. ->buildPostBody($data);
  1016. $result = $this->execute();
  1017. if (isset($result->message)) {
  1018. $this->helper->debug('postCampaignAttachments', [' CAMPAIGN ATTACHMENT ' .
  1019. $result->message]);
  1020. }
  1021. return $result;
  1022. }
  1023. /**
  1024. * Get contact address books.
  1025. *
  1026. * @param int $contactId
  1027. *
  1028. * @return object
  1029. */
  1030. public function getContactAddressBooks($contactId)
  1031. {
  1032. $url = $this->getApiEndpoint() . '/v2/contacts/' . $contactId
  1033. . '/address-books';
  1034. $this->setUrl($url)
  1035. ->setVerb('GET');
  1036. $response = $this->execute();
  1037. if (isset($response->message)) {
  1038. $message = 'GET CONTACTS ADDRESS BOOKS contact: ' . $contactId
  1039. . $response->message;
  1040. $this->helper->debug('getContactAddressBooks', [$message]);
  1041. }
  1042. return $response;
  1043. }
  1044. /**
  1045. * Gets list of all templates.
  1046. *
  1047. * @return object
  1048. */
  1049. public function getApiTemplateList()
  1050. {
  1051. $url = $this->getApiEndpoint() . self::REST_TEMPLATES;
  1052. $this->setUrl($url)
  1053. ->setVerb('GET');
  1054. $response = $this->execute();
  1055. if (isset($response->message)) {
  1056. $message = 'GET API CONTACT LIST ' . $response->message;
  1057. $this->helper->debug('getApiTemplateList', [$message]);
  1058. }
  1059. return $response;
  1060. }
  1061. /**
  1062. * Gets a template by ID.
  1063. *
  1064. * @param string $templateId
  1065. *
  1066. * @return object
  1067. */
  1068. public function getApiTemplate($templateId)
  1069. {
  1070. $url = $this->getApiEndpoint() . self::REST_TEMPLATES . '/' . $templateId;
  1071. $this->setUrl($url)
  1072. ->setVerb('GET');
  1073. $response = $this->execute();
  1074. if (isset($response->message)) {
  1075. $message = 'GET API CONTACT LIST ' . $response->message;
  1076. $this->helper->debug('getApiTemplate', [$message]);
  1077. }
  1078. return $response;
  1079. }
  1080. /**
  1081. * Adds multiple pieces of transactional data to account asynchronously,
  1082. * returning an identifier that can be used to check for import progress.
  1083. *
  1084. * @param array $transactionalData
  1085. * @param string $collectionName
  1086. *
  1087. * @return null
  1088. * @throws \Exception
  1089. */
  1090. public function postAccountTransactionalDataImport(
  1091. $transactionalData,
  1092. $collectionName = 'Catalog_Default'
  1093. ) {
  1094. $orders = [];
  1095. foreach ($transactionalData as $one) {
  1096. if (isset($one['id'])) {
  1097. $orders[] = [
  1098. 'Key' => $one['id'],
  1099. 'ContactIdentifier' => 'account',
  1100. 'Json' => json_encode($one),
  1101. ];
  1102. }
  1103. }
  1104. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA_IMPORT
  1105. . $collectionName;
  1106. $this->setUrl($url)
  1107. ->setVerb('POST')
  1108. ->buildPostBody($orders);
  1109. $response = $this->execute();
  1110. if (isset($response->message)) {
  1111. $message = ' SEND MULTI TRANSACTIONAL DATA TO ACCOUNT'
  1112. . $response->message;
  1113. $this->helper->debug('postAccountTransactionalDataImport', [$message]);
  1114. }
  1115. return $response;
  1116. }
  1117. /**
  1118. * Gets the import status of a previously started contact import.
  1119. *
  1120. * @param string $importId
  1121. *
  1122. * @return null
  1123. * @throws \Exception
  1124. */
  1125. public function getContactsImportByImportId($importId)
  1126. {
  1127. $url = $this->getApiEndpoint() . self::REST_CONTACTS_IMPORT . $importId;
  1128. $this->setUrl($url)
  1129. ->setVerb('GET');
  1130. $response = $this->execute();
  1131. if (isset($response->message)) {
  1132. $message = 'GET CONTACTS IMPORT BY IMPORT ID ' . $response->message;
  1133. $this->helper->debug('getContactsImportByImportId', [$message]);
  1134. }
  1135. return $response;
  1136. }
  1137. /**
  1138. * Gets the import status of a previously started transactional import.
  1139. *
  1140. * @param string $importId
  1141. *
  1142. * @return object
  1143. */
  1144. public function getContactsTransactionalDataImportByImportId($importId)
  1145. {
  1146. $url = $this->getApiEndpoint() . self::REST_TRANSACTIONAL_DATA_IMPORT
  1147. . $importId;
  1148. $this->setUrl($url)
  1149. ->setVerb('GET');
  1150. $response = $this->execute();
  1151. if (isset($response->message)) {
  1152. $message = 'GET CONTACTS TRANSACTIONAL DATA IMPORT BY IMPORT ID '
  1153. . $response->message;
  1154. $this->helper->debug('getContactsTransactionalDataImportByImportId', [$message]);
  1155. }
  1156. return $response;
  1157. }
  1158. /**
  1159. * Get contact import report faults.
  1160. *
  1161. * @param string $id
  1162. *
  1163. * @return bool|null
  1164. *
  1165. * @throws \Exception
  1166. */
  1167. public function getContactImportReportFaults($id)
  1168. {
  1169. $this->isNotJson = true;
  1170. $url = $this->getApiEndpoint() . self::REST_CONTACTS_IMPORT . $id . '/report-faults';
  1171. $this->setUrl($url)
  1172. ->setVerb('GET');
  1173. $response = $this->execute();
  1174. //if string is JSON than there is a error message
  1175. if (json_decode($response)) {
  1176. //log error
  1177. if (isset($response->message)) {
  1178. $message = 'GET CONTACT IMPORT REPORT FAULTS: ' . $response->message;
  1179. $this->helper->log($message);
  1180. }
  1181. return false;
  1182. }
  1183. return $response;
  1184. }
  1185. /**
  1186. * Gets the send status using send ID.
  1187. *
  1188. * @param string $id
  1189. * @return object
  1190. */
  1191. public function getSendStatus($id)
  1192. {
  1193. $url = $this->getApiEndpoint() . self::REST_CAMPAIGN_SEND . '/' . $id;
  1194. $this->setUrl($url)
  1195. ->setVerb('GET');
  1196. $response = $this->execute();
  1197. //log error
  1198. if (isset($response->message)
  1199. && !in_array(
  1200. $response->message,
  1201. $this->exludeMessages
  1202. )
  1203. ) {
  1204. $message = 'GETS THE SEND STATUS USING SEND ID: '
  1205. . $response->message;
  1206. $this->helper->log($message);
  1207. }
  1208. return $response;
  1209. }
  1210. /**
  1211. * Get access token.
  1212. *
  1213. * @param string $url
  1214. * @param array|string $params
  1215. *
  1216. * @return string/object
  1217. */
  1218. public function getAccessToken($url, $params)
  1219. {
  1220. $ch = curl_init();
  1221. curl_setopt($ch, CURLOPT_URL, $url);
  1222. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1223. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  1224. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  1225. curl_setopt($ch, CURLOPT_POST, true);
  1226. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  1227. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
  1228. $response = json_decode(curl_exec($ch));
  1229. if ($response === false) {
  1230. $this->helper->error('Error Number: ' . curl_errno($ch), []);
  1231. }
  1232. if (isset($response->error)) {
  1233. $this->helper->error('OAUTH failed. Error - ' . $response->error, []);
  1234. if (isset($response->error_description)) {
  1235. $this->helper->error('OAUTH failed. Error description - ' . $response->error_description, []);
  1236. }
  1237. } elseif (isset($response->access_token)) {
  1238. return $response->access_token;
  1239. }
  1240. return $response;
  1241. }
  1242. /**
  1243. * Sends a transactional email.
  1244. *
  1245. * @param string $content
  1246. * @return mixed
  1247. */
  1248. public function sendApiTransactionalEmail($content)
  1249. {
  1250. $url = $this->getApiEndpoint() . self::REST_SEND_TRANSACTIONAL_EMAIL;
  1251. $this->setUrl($url)
  1252. ->setVerb('POST')
  1253. ->buildPostBody($content);
  1254. $this->execute();
  1255. }
  1256. /**
  1257. * Gets transactional email reporting statistics for a specified time period.
  1258. *
  1259. * @param string $date
  1260. * @param null $endDate
  1261. * @param null $aggregatedBy 'AllTime', 'Month', 'Week', 'Day'
  1262. *
  1263. * @return mixed
  1264. */
  1265. public function getEmailStats($date, $endDate = null, $aggregatedBy = null)
  1266. {
  1267. $url = $this->getApiEndpoint() . '/v2/email/stats/since-date/' . $date;
  1268. if ($endDate && $aggregatedBy) {
  1269. $url .= '?endDate=' . $endDate . '&aggregatedBy=' . $aggregatedBy;
  1270. }
  1271. $response = $this->setUrl($url)
  1272. ->setVerb('GET')
  1273. ->execute();
  1274. if (isset($response->message)) {
  1275. $this->helper->log('GET EMAIL STATS : ' . $response->message);
  1276. }
  1277. return $response;
  1278. }
  1279. /**
  1280. * Gets all preferences that a given contact is opted into
  1281. *
  1282. * @param $contactId
  1283. * @return mixed
  1284. */
  1285. public function getPreferencesForContact($contactId)
  1286. {
  1287. $url = $this->getApiEndpoint() . "/v2/contact/" . $contactId . "/preferences";
  1288. $response = $this->setUrl($url)
  1289. ->setVerb('GET')
  1290. ->execute();
  1291. if (isset($response->message)) {
  1292. $this->helper->log('GET PREFERENCES FOR CONTACT: ' . $response->message);
  1293. }
  1294. return $response;
  1295. }
  1296. /**
  1297. * Opts in a given contact to preferences, or opts out a given contact from preferences
  1298. *
  1299. * @param $contactId
  1300. * @param $preferences
  1301. *
  1302. * @return mixed
  1303. */
  1304. public function setPreferencesForContact($contactId, $preferences)
  1305. {
  1306. $url = $this->getApiEndpoint() . "/v2/contact/" . $contactId . "/preferences";
  1307. $this->setUrl($url)
  1308. ->setVerb('PUT')
  1309. ->buildPostBody($preferences);
  1310. $response = $this->execute();
  1311. if (isset($response->message)) {
  1312. $this->helper->log('SET PREFERENCES FOR CONTACT: ' . $response->message);
  1313. }
  1314. return $response;
  1315. }
  1316. /**
  1317. * Create contact with consent
  1318. *
  1319. * @param array $contact
  1320. * @param array $consentFields
  1321. *
  1322. * @return mixed
  1323. *
  1324. * @throws \Magento\Framework\Exception\LocalizedException
  1325. */
  1326. public function postContactWithConsent($contact, $consentFields)
  1327. {
  1328. $url = $this->getApiEndpoint() . self::REST_CONTACT_WITH_CONSENT;
  1329. $data = [
  1330. 'Contact' => $contact,
  1331. 'ConsentFields' => [['fields' => $consentFields]]
  1332. ];
  1333. $this->setUrl($url)
  1334. ->setVerb('POST')
  1335. ->buildPostBody($data);
  1336. $response = $this->execute();
  1337. if (isset($response->message)) {
  1338. $message = $contact['Email'] . ' , url ' . $url . ', ' . $response->message;
  1339. $this->helper->debug('postContactWithConsent', [$message]);
  1340. return $response;
  1341. }
  1342. return $response->contact;
  1343. }
  1344. /**
  1345. * Gets the preferences, as a tree structure
  1346. *
  1347. * @return mixed
  1348. */
  1349. public function getPreferences()
  1350. {
  1351. $url = $this->getApiEndpoint() . "/v2/preferences";
  1352. $response = $this->setUrl($url)
  1353. ->setVerb('GET')
  1354. ->execute();
  1355. if (isset($response->message)) {
  1356. $this->helper->log('GET PREFERENCES: ' . $response->message);
  1357. }
  1358. return $response;
  1359. }
  1360. /**
  1361. * Create contact with consent
  1362. *
  1363. * @param array $contact
  1364. * @param array $consentFields
  1365. * @param array $preferences
  1366. *
  1367. * @return mixed
  1368. *
  1369. * @throws \Magento\Framework\Exception\LocalizedException
  1370. */
  1371. public function postContactWithConsentAndPreferences($contact, $consentFields, $preferences)
  1372. {
  1373. $url = $this->getApiEndpoint() . self::REST_CONTACT_WITH_CONSENT_AND_PREFERENCES;
  1374. $data = [
  1375. 'Contact' => $contact,
  1376. 'ConsentFields' => [['fields' => $consentFields]],
  1377. 'Preferences' => [$preferences]
  1378. ];
  1379. $this->setUrl($url)
  1380. ->setVerb('POST')
  1381. ->buildPostBody($data);
  1382. $response = $this->execute();
  1383. if (isset($response->message)) {
  1384. $message = $contact['Email'] . ' , url ' . $url . ', ' . $response->message;
  1385. $this->helper->debug('postContactWithConsentAndPreferences', [$message]);
  1386. return $response;
  1387. }
  1388. return $response;
  1389. }
  1390. }