/** * * Get api client. * * @return bool|mixed|object */ public function _getApiClient() { if (empty($this->client)) { $website = $this->getCustomer()->getStore()->getWebsite(); $client = $this->helper->getWebsiteApiClient($website); $client->setApiUsername($this->helper->getApiUsername($website))->setApiPassword($this->helper->getApiPassword($website)); $this->client = $client; } return $this->client; }
/** * Rest constructor. * * @param int $website * @param \Dotdigitalgroup\Email\Helper\Data $data */ public function __construct($website = 0, \Dotdigitalgroup\Email\Helper\Data $data) { $this->helper = $data; $this->url = null; $this->verb = 'GET'; $this->requestBody = null; $this->requestLength = 0; $this->apiUsername = (string) $this->helper->getApiUsername($website); $this->apiPassword = (string) $this->helper->getApiPassword($website); $this->acceptType = 'application/json'; $this->responseBody = null; $this->responseInfo = null; if ($this->requestBody !== null) { $this->buildPostBody(); } }
/** * Check if api end point exist in DB. * * @throws \Magento\Framework\Exception\LocalizedException */ public function _checkApiEndPoint() { $apiEndpoint = $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::PATH_FOR_API_ENDPOINT); if (!$apiEndpoint) { if (!$this->getApiUsername() && !$this->getApiPassword()) { $this->setApiUsername($this->helper->getApiUsername())->setApiPassword($this->helper->getApiPassword()); } $accountInfo = $this->getAccountInfo(); if (is_object($accountInfo) && !isset($accountInfo->message)) { //save endpoint for account foreach ($accountInfo->properties as $property) { if ($property->name == 'ApiEndpoint' && !empty($property->value)) { $apiEndpoint = $property->value; $this->_saveApiEndpoint($property->value); break; } } } //check api endpoint again if (!$apiEndpoint) { throw new \Magento\Framework\Exception\LocalizedException(__('API endpoint cannot be empty. Re-save api credentials to retrieve API endpoint.')); } } $this->apiEndpoint = $apiEndpoint; }
/** * Get the api creds are valid. * @return string */ public function getApiValid() { $apiUsername = $this->data->getApiUsername(); $apiPassword = $this->data->getApiPassword(); $result = $this->test->validate($apiUsername, $apiPassword); return $result ? '<span class="message message-success">Valid</span>' : '<span class="message message-error">Not Valid</span>'; }
/** * Search the configuration data per website. * * @throws \Magento\Framework\Exception\LocalizedException */ public function _searchAccounts() { $this->orderIds = []; $this->orderIdsForSingleSync = []; $websites = $this->helper->getWebsites(true); foreach ($websites as $website) { $apiEnabled = $this->helper->isEnabled($website); $storeIds = $website->getStoreIds(); // api and order sync should be enabled, skip website with no store ids if ($apiEnabled && $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_ENABLED, $website) && !empty($storeIds)) { $this->apiUsername = $this->helper->getApiUsername($website); $this->apiPassword = $this->helper->getApiPassword($website); // limit for orders included to sync $limit = $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_TRANSACTIONAL_DATA_SYNC_LIMIT, $website); if (!isset($this->accounts[$this->apiUsername])) { $account = $this->accountFactory->create()->setApiUsername($this->apiUsername)->setApiPassword($this->apiPassword); $this->accounts[$this->apiUsername] = $account; } $this->accounts[$this->apiUsername]->setOrders($this->getConnectorOrders($website, $limit)); $this->accounts[$this->apiUsername]->setOrderIds($this->orderIds); $this->accounts[$this->apiUsername]->setWebsites($website->getId()); $this->accounts[$this->apiUsername]->setOrdersForSingleSync($this->getConnectorOrders($website, $limit, true)); $this->accounts[$this->apiUsername]->setOrderIdsForSingleSync($this->orderIdsForSingleSync); } } }
/** * @return \Magento\Framework\App\ResponseInterface */ public function execute() { if (!$this->formKeyValidator->validate($this->getRequest()) or !$this->customerSession->getConnectorContactId()) { return $this->_redirect('customer/account/'); } //params $additionalSubscriptions = $this->getRequest()->getParam('additional_subscriptions'); $paramDataFields = $this->getRequest()->getParam('data_fields'); $customerId = $this->customerSession->getConnectorContactId(); $customerEmail = $this->customerSession->getCustomer()->getEmail(); //client $website = $this->customerSession->getCustomer()->getStore()->getWebsite(); //if enabled if ($this->helper->isEnabled($website)) { $client = $this->helper->getWebsiteApiClient($website); $client->setApiUsername($this->helper->getApiUsername($website))->setApiPassword($this->helper->getApiPassword($website)); $contact = $client->getContactById($customerId); if (isset($contact->id)) { //contact address books $bookError = false; $addressBooks = $client->getContactAddressBooks($contact->id); $subscriberAddressBook = $this->helper->getSubscriberAddressBook($website); $processedAddressBooks = []; if (is_array($addressBooks)) { foreach ($addressBooks as $addressBook) { if ($subscriberAddressBook != $addressBook->id) { $processedAddressBooks[$addressBook->id] = $addressBook->name; } } } if (isset($additionalSubscriptions)) { foreach ($additionalSubscriptions as $additionalSubscription) { if (!isset($processedAddressBooks[$additionalSubscription])) { $bookResponse = $client->postAddressBookContacts($additionalSubscription, $contact); if (isset($bookResponse->message)) { $bookError = true; } } } foreach ($processedAddressBooks as $bookId => $name) { if (!in_array($bookId, $additionalSubscriptions)) { $bookResponse = $client->deleteAddressBookContact($bookId, $contact->id); if (isset($bookResponse->message)) { $bookError = true; } } } } else { foreach ($processedAddressBooks as $bookId => $name) { $bookResponse = $client->deleteAddressBookContact($bookId, $contact->id); if (isset($bookResponse->message)) { $bookError = true; } } } //contact data fields $data = []; $dataFields = $client->getDataFields(); $processedFields = []; foreach ($dataFields as $dataField) { $processedFields[$dataField->name] = $dataField->type; } foreach ($paramDataFields as $key => $value) { if (isset($processedFields[$key]) && $value) { if ($processedFields[$key] == 'Numeric') { $paramDataFields[$key] = (int) $value; } if ($processedFields[$key] == 'String') { $paramDataFields[$key] = (string) $value; } if ($processedFields[$key] == 'Date') { $paramDataFields[$key] = $this->localeDate->date($value)->format(\Zend_Date::ISO_8601); } $data[] = ['Key' => $key, 'Value' => $paramDataFields[$key]]; } } $contactResponse = $client->updateContactDatafieldsByEmail($customerEmail, $data); if (isset($contactResponse->message) && $bookError) { $this->messageManager->addErrorMessage(__('An error occurred while saving your subscription preferences.')); } else { $this->messageManager->addSuccessMessage(__('The subscription preferences has been saved.')); } } else { $this->messageManager->addErrorMessage(__('An error occurred while saving your subscription preferences.')); } } $this->_redirect('customer/account/'); }