286 lines
9 KiB
PHP
286 lines
9 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
use App\Models\Brand;
|
|
use App\Models\CustomerInfo;
|
|
use App\Models\SingleOptin;
|
|
use App\Exceptions\MailjetException;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Mailjet\Resources;
|
|
|
|
class MailjetHelper
|
|
{
|
|
/**
|
|
* Get a campaign by ID
|
|
*
|
|
* @param integer $campaignId
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function getCampaign(int $campaignId): array
|
|
{
|
|
return Cache::remember("mailjet_campaign_{$campaignId}", 86400, function () use ($campaignId) {
|
|
$response = app('mailjetClient')->get(Resources::$Campaign, [
|
|
'id' => $campaignId
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to retrieve campaign
|
|
(Campaign ID: %d | Error Code: %d | Message: %s)',
|
|
$campaignId,
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Retrieves contact lists
|
|
*
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function getContactLists(): array
|
|
{
|
|
$filters = [
|
|
'Limit' => 1000,
|
|
'Sort' => 'CreatedAt DESC',
|
|
];
|
|
|
|
$response = app('mailjetClient')->get(Resources::$Contactslist, [
|
|
'filters' => $filters
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to retrieve contact lists (Code: %d | Message: %s)',
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get unsubscribed contacts for a given contact list
|
|
*
|
|
* @param integer $contactListId
|
|
* @param \Carbon\Carbon|null $lastActivity
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function getUnsubscribedContacts(int $contactListId, \Carbon\Carbon|null $lastActivity = null): array
|
|
{
|
|
$filters = [
|
|
'ContactsList' => $contactListId,
|
|
'Unsub' => true,
|
|
'Limit' => 1000,
|
|
];
|
|
|
|
if (!is_null($lastActivity)) {
|
|
$filters = array_merge(
|
|
$filters,
|
|
[
|
|
'LastActivityAt' => $lastActivity->toRfc3339String()
|
|
]
|
|
);
|
|
}
|
|
|
|
$response = app('mailjetClient')->get(Resources::$Listrecipient, [
|
|
'filters' => $filters
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to retrieve unsubscribed contacts for list %d (Code: %d | Message: %s)',
|
|
$contactListId,
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get contact for a given contact id
|
|
*
|
|
* @param integer $contactId
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function getContact(int $contactId): array
|
|
{
|
|
$response = app('mailjetClient')->get(Resources::$Contact, [
|
|
'id' => $contactId
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to retrieve contact (Contact ID: %d | Error Code: %d | Message: %s)',
|
|
$contactId,
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a new Mailjet contact list
|
|
*
|
|
* @param string $listName
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function createContactList(string $listName): array
|
|
{
|
|
$response = app('mailjetClient')->post(Resources::$Contactslist, [
|
|
'body' => [
|
|
'Name' => $listName,
|
|
]
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to create contact list (List Name: %s | Error Code: %d | Message: %s)',
|
|
$listName,
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Uploads a CSV file
|
|
*
|
|
* @param integer $mailjetListId
|
|
* @param string $csvFile
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function uploadCsvFile(int $mailjetListId, string $csvFile): array
|
|
{
|
|
$response = app('mailjetClient')->post(Resources::$ContactslistCsvdata, [
|
|
'id' => $mailjetListId,
|
|
'body' => $csvFile,
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to upload csv file (List ID: %d | Error Code: %d | Message: %s)',
|
|
$mailjetListId,
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Imports contacts into a Mailjet contact list
|
|
*
|
|
* @param integer $listId
|
|
* @param integer $dataId
|
|
* @return array
|
|
* @throws \App\Exceptions\MailjetException If the Mailjet API call fails.
|
|
*/
|
|
public static function importContacts(int $listId, int $dataId): array
|
|
{
|
|
$response = app('mailjetClient')->post(Resources::$Csvimport, [
|
|
'body' => [
|
|
'ContactsListID' => $listId,
|
|
'DataID' => $dataId,
|
|
'Method' => "addnoforce"
|
|
]
|
|
]);
|
|
|
|
if ($response->success()) {
|
|
return $response->getData();
|
|
} else {
|
|
throw new MailjetException(
|
|
$response,
|
|
sprintf(
|
|
'Mailjet API Error: Unable to import contacts
|
|
(List ID: %d | Data ID: %d | Error Code: %d | Message: %s)',
|
|
$listId,
|
|
$dataId,
|
|
$response->getStatus(),
|
|
$response->getBody()['ErrorMessage'] ?? $response->getReasonPhrase()
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe a contact from a brand
|
|
*
|
|
* @param \App\Models\Brand $brandModel
|
|
* @param string $email
|
|
* @return void
|
|
* @throws \Exception When optin status cannot be changed.
|
|
*/
|
|
public static function unsubscribeContact(Brand $brandModel, string $email): void
|
|
{
|
|
$crmCustomer = CustomerInfo::getFromEmail($brandModel, $email)->first();
|
|
|
|
if ($crmCustomer) {
|
|
$crmCustomer->opt_in = false;
|
|
|
|
if (!$crmCustomer->saveOrFail()) {
|
|
throw new \Exception(
|
|
sprintf(
|
|
'Optin status not changed for customer (%s) and brand (%d).',
|
|
$crmCustomer->hashcode,
|
|
$brandModel->name
|
|
)
|
|
);
|
|
}
|
|
} else {
|
|
$singleOptin = SingleOptin::query()->where('email', $email)
|
|
->whereHas('brand', function ($q) use ($brandModel) {
|
|
$q->whereId($brandModel->getKey());
|
|
})->first();
|
|
|
|
if ($singleOptin) {
|
|
if (!$singleOptin->delete()) {
|
|
throw new \Exception(
|
|
sprintf(
|
|
'SingleOptin (%s) not deleted for brand (%d).',
|
|
$singleOptin->hashcode,
|
|
$brandModel->name
|
|
)
|
|
);
|
|
}
|
|
}
|
|
} //end if
|
|
}
|
|
}
|