Skip to content

Commit

Permalink
Bug fixes and improvements (#33)
Browse files Browse the repository at this point in the history
* Abandoned carts fixed not updating in AC when cart is edited

* Cron with delete capability 7 days previous entries

* add logger in catch

update event in events.xml checkout_onepage_controller_success_action for checkout_submit_all_after

update Data.php type

update OrderSync.php add doc and types

update doc AbandonedCartSync.php

update AbandonedCartSendData.php

update AbandonedCartSendData.php add create customer in AC

update PaymentInformationManagementPlugin.php delete black line

update OrderSyncCron.php, update filter and add Logger in Exception

rollback name events.xml

update Curl.php add types

delete logger debug in the code

* DS Store removed

Co-authored-by: YAIR GARCIA <[email protected]>
Co-authored-by: Yair García Torres <[email protected]>
  • Loading branch information
3 people authored Jul 14, 2022
1 parent d44a8a8 commit 3213296
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 289 deletions.
8 changes: 7 additions & 1 deletion AbandonedCart/Cron/AbandonedCartSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ActiveCampaign\AbandonedCart\Helper\Data as AbandonedCartHelper;
use ActiveCampaign\AbandonedCart\Model\AbandonedCartSendData;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class AbandonedCartSync
{
Expand All @@ -30,7 +32,11 @@ public function __construct(
$this->abandonedCartSendData = $abandonedCartSendData;
}

public function execute()
/**
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function execute(): void
{
if ($this->abandonedCartHelper->isAbandonedCartSyncingEnabled()) {
$this->abandonedCartSendData->sendAbandonedCartData();
Expand Down
2 changes: 1 addition & 1 deletion AbandonedCart/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Data extends AbstractHelper
* @param null $scopeCode
* @return bool
*/
public function isAbandonedCartSyncingEnabled($scopeCode = null)
public function isAbandonedCartSyncingEnabled($scopeCode = null): bool
{
return $this->scopeConfig->isSetFlag(
self::ACTIVE_CAMPAIGN_ABANDONED_CART_SYNC,
Expand Down
444 changes: 256 additions & 188 deletions AbandonedCart/Model/AbandonedCartSendData.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ public function afterSavePaymentInformation(
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {

$quoteRepository = $this->getCartRepository();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $quoteRepository->getActive($cartId);

if ($this->abandonedCartHelper->isAbandonedCartSyncingEnabled()) {
$response = $this->abandonedCartSendData->sendAbandonedCartData($quote->getId());
}
Expand All @@ -68,4 +67,4 @@ private function getCartRepository()
}
return $this->cartRepository;
}
}
}
2 changes: 1 addition & 1 deletion AbandonedCart/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"version": "2.0.2",
"require": {
"php": "~7.3.0||~7.4.0",
"php": "~7.3.0||~7.4.0||~8.0",
"activecampaign/core": "2.0.*"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion Core/Controller/Adminhtml/System/Config/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function curlRequestData($storeId)

return [
"connection" => [
"service" => "magento2",
"service" => "magento2-$store->getName()",
"externalid" => $store->getCode(),
"name" => $store->getName(),
"logoUrl" => $this->activeCampaignHelper->getStoreLogo($storeId),
Expand Down
108 changes: 60 additions & 48 deletions Core/Helper/Curl.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<?php

namespace ActiveCampaign\Core\Helper;

use ActiveCampaign\Core\Helper\Data as ActiveCampaignHelper;
use ActiveCampaign\Core\Logger\Logger;
use ActiveCampaign\SyncLog\Model\SyncLog;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use ActiveCampaign\SyncLog\Model\SyncLog;
use Magento\Framework\Phrase;

class Curl extends AbstractHelper
{
const API_VERSION = "/api/3/";
const HTTP_VERSION = "1.1";
const CONTENT_TYPE = "application/json";
public const API_VERSION = "/api/3/";
public const HTTP_VERSION = "1.1";
public const CONTENT_TYPE = "application/json";

/**
* @var Client|ClientInterface
Expand Down Expand Up @@ -52,13 +55,14 @@ class Curl extends AbstractHelper
* @param SyncLog $syncLog
*/
public function __construct(
Context $context,
ClientInterface $client = null,
JsonHelper $jsonHelper,
Logger $logger,
Context $context,
ClientInterface $client = null,
JsonHelper $jsonHelper,
Logger $logger,
ActiveCampaignHelper $activeCampaignHelper,
SyncLog $syncLog
) {
SyncLog $syncLog
)
{
$this->client = $client ?: new Client();
$this->jsonHelper = $jsonHelper;
$this->logger = $logger;
Expand Down Expand Up @@ -94,70 +98,71 @@ public function createConnection($method, $urlEndpoint, $request, $data = [])
* @param $method
* @param $urlEndpoint
* @param array $data
* @param $request
* @return array
* @throws GuzzleException
*/
public function orderDataSend($method, $urlEndpoint, $data = [])
public function orderDataSend($method, $urlEndpoint, array $data = []): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
$url = $apiUrl . self::API_VERSION . $urlEndpoint;
$bodyData = (!empty($data)) ? $this->jsonHelper->jsonEncode($data) : '';
$headers = $this->getHeaders($apiKey);
$result = $this->sendRequest($urlEndpoint, $method, $url, $headers, $bodyData);
return $result;
return $this->sendRequest($urlEndpoint, $method, $url, $headers, $bodyData);
}

/**
* @param $method
* @param $urlEndpoint
* @param array $data
* @return array
* @throws GuzzleException
*/
public function orderDataUpdate($method, $urlEndpoint, $data = [])
public function orderDataUpdate($method, $urlEndpoint, array $data = []): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
$url = $apiUrl . self::API_VERSION . $urlEndpoint;
$bodyData = (!empty($data)) ? $this->jsonHelper->jsonEncode($data) : '';
$headers = $this->getHeaders($apiKey);
$result = $this->sendRequest($urlEndpoint, $method, $url, $headers, $bodyData);
return $result;
return $this->sendRequest($urlEndpoint, $method, $url, $headers, $bodyData);
}

/**
* @param $method
* @param $urlEndpoint
* @param $orderId
* @return array
* @throws GuzzleException
*/
public function orderDataDelete($method, $urlEndpoint, $orderId)
public function orderDataDelete($method, $urlEndpoint, $orderId): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
$url = $apiUrl . self::API_VERSION . $urlEndpoint . $orderId;
$headers = $this->getHeaders($apiKey);
$result = $this->sendRequest($urlEndpoint, $method, $url, $headers);
return $result;
return $this->sendRequest($urlEndpoint, $method, $url, $headers);
}

/**
* @param $method
* @param $urlEndpoint
* @return array
* @throws GuzzleException
*/
public function deleteConnection($method, $urlEndpoint)
public function deleteConnection($method, $urlEndpoint): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
$url = $apiUrl . self::API_VERSION . $urlEndpoint;
$headers = $this->getHeaders($apiKey);

$result = $this->sendRequest($urlEndpoint, $method, $url, $headers);
return $result;
return $this->sendRequest($urlEndpoint, $method, $url, $headers);
}

public function createContacts($method, $urlEndpoint, $data = [])
/**
* @throws GuzzleException
*/
public function createContacts($method, $urlEndpoint, $data = []): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
Expand All @@ -166,27 +171,28 @@ public function createContacts($method, $urlEndpoint, $data = [])
$bodyData = (!empty($data)) ? $this->jsonHelper->jsonEncode($data) : '';
$headers = $this->getHeaders($apiKey);

$result = $this->sendRequest($urlEndpoint, $method, $url, $headers, $bodyData);
return $result;
return $this->sendRequest($urlEndpoint, $method, $url, $headers, $bodyData);
}

public function getAllConnections($method, $urlEndpoint)
/**
* @throws GuzzleException
*/
public function getAllConnections($method, $urlEndpoint): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();

$url = $apiUrl . self::API_VERSION . $urlEndpoint;
$headers = $this->getHeaders($apiKey);

$result = $this->sendRequest($urlEndpoint, $method, $url, $headers);
return $result;
return $this->sendRequest($urlEndpoint, $method, $url, $headers);
}

/**
* @param $apiKey
* @return array
*/
private function getHeaders($apiKey)
private function getHeaders($apiKey): array
{
$headers = [];
$headers['Content-Type'] = self::CONTENT_TYPE;
Expand All @@ -195,23 +201,25 @@ private function getHeaders($apiKey)
}

/**
* @param $urlEndpoint
* @param $method
* @param $url
* @param $headers
* @param $bodyData
* @param string $bodyData
* @return array
* @throws GuzzleException
*/
private function sendRequest($urlEndpoint, $method, $url, $headers, $bodyData = '')
private function sendRequest($urlEndpoint, $method, $url, $headers, string $bodyData = ''): array
{
$result = [];
$synclog = $this->syncLog;
try {
$request = [
"METHOD"=>$method,
"URL"=>$url,
"METHOD" => $method,
"URL" => $url,
"HTTP VERSION" => self::HTTP_VERSION,
"HEADERS"=>$headers,
"BODY DATA"=>$bodyData
"HEADERS" => $headers,
"BODY DATA" => $bodyData
];
$synclog->setType($urlEndpoint);
$synclog->setEndpoint($urlEndpoint);
Expand Down Expand Up @@ -252,7 +260,7 @@ private function sendRequest($urlEndpoint, $method, $url, $headers, $bodyData =
} catch (\Exception $e) {
$synclog->setStatus(0);
$synclog->setErrors($e->getMessage());
$this->logger->critical($e);
$this->logger->critical("MODULE Core" . $e);
$result['success'] = false;
$result['message'] = $e->getMessage();
}
Expand All @@ -264,7 +272,7 @@ private function sendRequest($urlEndpoint, $method, $url, $headers, $bodyData =
/**
* @return string[]
*/
private function successCodes()
private function successCodes(): array
{
return [
200 => "OK",
Expand All @@ -275,7 +283,7 @@ private function successCodes()
/**
* @return string[]
*/
private function failureCodes()
private function failureCodes(): array
{
return [
400 => "Bad Request",
Expand All @@ -286,9 +294,9 @@ private function failureCodes()

/**
* @param $response
* @return \Magento\Framework\Phrase
* @return Phrase
*/
private function getMessage($response)
private function getMessage($response): Phrase
{
if (isset($response['message'])) {
return $response['message'];
Expand All @@ -301,7 +309,10 @@ private function getMessage($response)
return __("Something was wrong Please try again later");
}

public function sendRequestAbandonedCart($method, $urlEndpoint, $data = [])
/**
* @throws GuzzleException
*/
public function sendRequestAbandonedCart($method, $urlEndpoint, $data = []): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
Expand All @@ -312,11 +323,13 @@ public function sendRequestAbandonedCart($method, $urlEndpoint, $data = [])

$headers = $this->getHeaders($apiKey);
$type = 'ecomAbandonedCarts';
$result = $this->sendRequest($type, $method, $url, $headers, $bodyData);
return $result;
return $this->sendRequest($type, $method, $url, $headers, $bodyData);
}

public function listAllCustomers($method, $urlEndpoint, $customerEmail)
/**
* @throws GuzzleException
*/
public function listAllCustomers($method, $urlEndpoint, $customerEmail): array
{
$apiUrl = $this->activeCampaignHelper->getApiUrl();
$apiKey = $this->activeCampaignHelper->getApiKey();
Expand All @@ -325,7 +338,6 @@ public function listAllCustomers($method, $urlEndpoint, $customerEmail)

$headers = $this->getHeaders($apiKey);
$type = 'ecomCustomers';
$result = $this->sendRequest($type, $method, $url, $headers);
return $result;
return $this->sendRequest($type, $method, $url, $headers);
}
}
4 changes: 2 additions & 2 deletions Core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"version": "2.0.3",
"require": {
"php": "~7.3.0||~7.4.0"
"php": "~7.3.0||~7.4.0||~8.0"
},
"autoload": {
"files": [
Expand All @@ -21,4 +21,4 @@
"ActiveCampaign\\Core\\": ""
}
}
}
}
2 changes: 1 addition & 1 deletion Customer/Controller/Adminhtml/Customer/MassSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function massAction(AbstractCollection $collection)

$this->customer->saveResult($customerId, $syncStatus, $contactId, $ecomCustomerId);
} catch (\Exception $exception) {
$this->logger->critical($exception);
$this->logger->critical("MODULE: Customer " . $exception);
}
}
$customersSynced++;
Expand Down
Loading

0 comments on commit 3213296

Please sign in to comment.