Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanleslie committed Sep 18, 2023
1 parent 6899d85 commit 92375ee
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 692 deletions.
85 changes: 19 additions & 66 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Klaviyo\Reclaim\Helper\Logger;
use KlaviyoV3Sdk\KlaviyoV3Api;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
class Data extends KlaviyoV3Api
{
const USER_AGENT = 'Klaviyo/1.0';
const KLAVIYO_HOST = 'https://a.klaviyo.com/';
const LIST_V2_API = 'api/v2/list/';
const LIST_V3_API = 'api/list';

/**
* Klaviyo logger helper
Expand Down Expand Up @@ -62,9 +62,9 @@ public function getKlaviyoLists($api_key = null)
if (!$api_key) {
$api_key = $this->_klaviyoScopeSetting->getPrivateApiKey();
}

$v3_list_api = self::KLAVIYO_HOST . self::LIST_V3_API . '?api_key=' . $api_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://a.klaviyo.com/api/v2/lists?api_key=' . $api_key);
curl_setopt($ch, CURLOPT_URL, $v3_list_api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


Expand All @@ -74,11 +74,11 @@ public function getKlaviyoLists($api_key = null)

if ($statusCode !== 200) {
if ($statusCode === 403) {
$reason = 'The Private Klaviyo API Key you have set is invalid.';
$reason = self::ERROR_INVALID_API_KEY;
} elseif ($statusCode === 401) {
$reason = 'The Private Klaviyo API key you have set is no longer valid.';
$reason = self::ERROR_INVALID_API_KEY;
} else {
$reason = 'Unable to verify Klaviyo Private API Key.';
$reason = self::ERROR_NON_200_STATUS;
}

$result = [
Expand All @@ -104,7 +104,7 @@ public function getKlaviyoLists($api_key = null)
* @param string|null $firstName
* @param string|null $lastName
* @param string|null $source
* @return bool|string
* @return array|false|null|string
*/
public function subscribeEmailToKlaviyoList($email, $firstName = null, $lastName = null, $source = null)
{
Expand All @@ -128,10 +128,13 @@ public function subscribeEmailToKlaviyoList($email, $firstName = null, $lastName

$propertiesVal = ['profiles' => $properties];

$path = self::LIST_V2_API . $listId . $optInSetting;

if ($optInSetting == ScopeSetting::API_SUBSCRIBE) {
$path = self::LIST_V3_API . $listId . $optInSetting;
} else {
$path = self::LIST_V3_API . $optInSetting;
}
try {
$response = $this->sendApiRequest($path, $propertiesVal, 'POST');
$response = $this->subscribeMembersToList($path, $listId, $propertiesVal);
} catch (\Exception $e) {
$this->_klaviyoLogger->log(sprintf('Unable to subscribe %s to list %s: %s', $email, $listId, $e));
$response = false;
Expand All @@ -142,19 +145,13 @@ public function subscribeEmailToKlaviyoList($email, $firstName = null, $lastName

/**
* @param string $email
* @return bool|string
* @return array|string|null
*/
public function unsubscribeEmailFromKlaviyoList($email)
public function unsubscribeEmailFromKlaviyoList($email): array|string|null
{
$listId = $this->_klaviyoScopeSetting->getNewsletter();

$path = self::LIST_V2_API . $listId . ScopeSetting::API_SUBSCRIBE;
$fields = [
'emails' => [(string)$email],
];

try {
$response = $this->sendApiRequest($path, $fields, 'DELETE');
$response = $this->unsubscribeEmailFromKlaviyoList($email);
} catch (\Exception $e) {
$this->_klaviyoLogger->log(sprintf('Unable to unsubscribe %s from list %s: %s', $email, $listId, $e));
$response = false;
Expand All @@ -173,58 +170,14 @@ public function klaviyoTrackEvent($event, $customer_properties = [], $properties
return 'You must identify a user by email or ID.';
}
$params = array(
'token' => $this->_klaviyoScopeSetting->getPublicApiKey($storeId),
'event' => $event,
'metric' => $event,
'properties' => $properties,
'customer_properties' => $customer_properties
);

if (!is_null($timestamp)) {
$params['time'] = $timestamp;
}
return $this->make_request('api/track', $params);
}

/**
* @param string $path
* @param array $params
* @param string $method
* @return mixed[]
* @throws \Exception
*/
private function sendApiRequest(string $path, array $params, string $method = null)
{
$url = self::KLAVIYO_HOST . $path;

//Add API Key to params
$params['api_key'] = $this->_klaviyoScopeSetting->getPrivateApiKey();

$curl = curl_init();
$encodedParams = json_encode($params);

curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => (!empty($method)) ? $method : 'POST',
CURLOPT_POSTFIELDS => $encodedParams,
CURLOPT_USERAGENT => self::USER_AGENT,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Content-Length: ' . strlen($encodedParams)
],
]);

// Submit the request
$response = curl_exec($curl);
$err = curl_errno($curl);

if ($err) {
throw new \Exception(curl_error($curl));
}

// Close cURL session handle
curl_close($curl);

return $response;
return $this->track($params);
}
}
4 changes: 2 additions & 2 deletions Helper/ScopeSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ScopeSetting extends \Magento\Framework\App\Helper\AbstractHelper

const NEWSLETTER = 'klaviyo_reclaim_newsletter/newsletter/newsletter';
const USING_KLAVIYO_LIST_OPT_IN = 'klaviyo_reclaim_newsletter/newsletter/using_klaviyo_list_opt_in';
const API_MEMBERS = '/members';
const API_SUBSCRIBE = '/subscribe';
const API_MEMBERS = '/relationships/profiles/';
const API_SUBSCRIBE = '/profile-subscription-bulk-create-jobs';

const CONSENT_AT_CHECKOUT_EMAIL_IS_ACTIVE = 'klaviyo_reclaim_consent_at_checkout/email_consent/is_active';
const CONSENT_AT_CHECKOUT_EMAIL_LIST_ID = 'klaviyo_reclaim_consent_at_checkout/email_consent/list_id';
Expand Down
Loading

0 comments on commit 92375ee

Please sign in to comment.