Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanleslie committed Sep 15, 2023
1 parent 325dde2 commit 6899d85
Show file tree
Hide file tree
Showing 9 changed files with 773 additions and 23 deletions.
65 changes: 65 additions & 0 deletions Api/KlaviyoApiWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use Configuration;
use KlaviyoV3Sdk\Exception\KlaviyoApiException;
use KlaviyoV3Sdk\Klaviyo;
use KlaviyoV3Sdk\KlaviyoV3Api;
use KlaviyoV3Sdk\Exception;

class KlaviyoApiWrapper
{
/** @var Klaviyo Client for Klaviyo's Api. */
protected $client;

public function __construct()
{
$this->client = new KlaviyoV3Api(Configuration::get('KLAVIYO_PRIVATE_API'), Configuration::get('KLAVIYO_PUBLIC_API'));
}

/**
* Get all lists for specific Klaviyo account.
*
* @return mixed
*/
public function getLists()
{
return $this->client->getLists();
}

/**
* Subscribe email to the Subscriber List selected on configuration page (if selected).
*
* @param string $email
* @throws KlaviyoApiException
*/
public function subscribeCustomer($email, $customProperties = [])
{
$profile = array(
'type' => 'profile',
'attributes' => array(
'email' => $email,
'subscriptions' => array(
'email' => [
'MARKETING'
]
)
)
);

$listId = Configuration::get('KLAVIYO_SUBSCRIBER_LIST');

if ($listId) {
$this->client->subscribeMembersToList($listId, array($profile));
}
}

/**
* Send event to Klaviyo using the Track endpoint.
*
* @param array $event
* @return bool
* @throws KlaviyoApiException
*/
public function trackEvent(array $eventConfig)
{
return (bool) $this->client->track($eventConfig);
}
}
24 changes: 1 addition & 23 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Klaviyo\Reclaim\Helper\ScopeSetting;
use Magento\Framework\App\Helper\Context;
use Klaviyo\Reclaim\Helper\Logger;
use KlaviyoV3Sdk\KlaviyoV3Api;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand Down Expand Up @@ -184,29 +185,6 @@ public function klaviyoTrackEvent($event, $customer_properties = [], $properties
return $this->make_request('api/track', $params);
}

protected function make_request($path, $params)
{
$url = self::KLAVIYO_HOST . $path;

$dataString = json_encode($params);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $dataString,
),
);

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

if ($response == '0') {
$this->_klaviyoLogger->log("Unable to send event to Track API with data: $dataString");
}

return $response == '1';
}

/**
* @param string $path
* @param array $params
Expand Down
7 changes: 7 additions & 0 deletions KlaviyoV3/Exception/KlaviyoApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace KlaviyoV3Sdk\Exception;

class KlaviyoApiException extends KlaviyoException
{
}
7 changes: 7 additions & 0 deletions KlaviyoV3/Exception/KlaviyoAuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace KlaviyoV3Sdk\Exception;

class KlaviyoAuthenticationException extends KlaviyoApiException
{
}
7 changes: 7 additions & 0 deletions KlaviyoV3/Exception/KlaviyoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace KlaviyoV3Sdk\Exception;

class KlaviyoResourceNotFoundException extends KlaviyoApiException
{
}
7 changes: 7 additions & 0 deletions KlaviyoV3/Exception/KlaviyoRateLimitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace KlaviyoV3Sdk\Exception;

class KlaviyoRateLimitException extends KlaviyoApiException
{
}
7 changes: 7 additions & 0 deletions KlaviyoV3/Exception/KlaviyoResourceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace KlaviyoV3Sdk\Exception;

class KlaviyoResourceNotFoundException extends KlaviyoApiException
{
}
49 changes: 49 additions & 0 deletions KlaviyoV3/KlaviyoV3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace KlaviyoV3Sdk;

class Klaviyo
{
/**
* @var string
*/
protected $private_key;

/**
* @var string
*/
protected $public_key;

/**
* @var string
*/
const VERSION = '1.0.0';

/**
* Constructor for Klaviyo V3 SDK wrapper
*
* @param string $private_key Private Key for Klaviyo account
* @param string $public_key Public Key for Klaviyo Account
*/
public function __construct($private_key, $public_key)
{
$this->private_key = $private_key;
$this->public_key = $public_key;
}

/**
* @return string
*/
public function getPrivateKey(): string
{
return $this->private_key;
}

/**
* @return string
*/
public function getPublicKey(): string
{
return $this->public_key;
}
}
Loading

0 comments on commit 6899d85

Please sign in to comment.