Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resubscribe functionality via API #20

Merged
merged 14 commits into from
May 22, 2024
63 changes: 60 additions & 3 deletions src/Api/ZohoCampaignsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function __construct(
* code: int,
* }
*/
public function listSubscribe(string $listKey, string $email, array $contactInfo = []): array
public function listSubscribe(string $listKey, string $email, array $contactInfo = [], array $additionalParams = []): array
{
$params = [
$params = array_merge([
'listkey' => $listKey,
'resfmt' => 'JSON',
'contactinfo' => json_encode(array_merge([
'Contact Email' => $email,
], $contactInfo)),
];
], $additionalParams);

return Http::baseUrl($this->endpoint())
->withToken($this->accessToken->get(), 'Zoho-oauthtoken')
Expand Down Expand Up @@ -64,6 +64,63 @@ public function listUnsubscribe(string $listKey, string $email): array
->json();
}

/**
* Retrieves the list of subscribers for a given list key with various options.
*
* @param string $listKey The list key.
* @param array $options An array of options for the request. Possible keys include:
* - 'sort': The sort order of the results. Possible values are 'asc' for ascending order and 'desc' for descending order.
* - 'fromindex': The starting index for the results. This is a number.
* - 'range': The range of results to retrieve. This is a number.
* - 'status': The status of the subscribers to retrieve. Possible values are 'active', 'recent', 'mostrecent', 'unsub', and 'bounce'.
* @return array The list of subscribers.
*/
public function getListSubscribers(string $listKey, array $options = []): array
{
$params = array_merge([
'listkey' => $listKey,
'resfmt' => 'JSON',
], $options);

$response = Http::baseUrl($this->endpoint())
->withToken($this->accessToken->get(), 'Zoho-oauthtoken')
->withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
])
->get(sprintf('/getlistsubscribers?%s', http_build_query($params)))
->json();

return $response['list_of_details'] ?? [];
}


/**
* Retrieves the count of subscribers for a given list key and status.
*
* @param string $listKey The list key.
* @param string $status The status of the subscribers to count. Possible values are 'active', 'unsub', 'bounce', and 'spam'.
* @return int The count of subscribers.
*/
public function getListSubscribersCount(string $listKey, string $status='active'): int
{
$params = [
'listkey' => $listKey,
'resfmt' => 'JSON',
'status' => $status,
];

$response = Http::baseUrl($this->endpoint())
->withToken($this->accessToken->get(), 'Zoho-oauthtoken')
->withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
])
->get(sprintf('/listsubscriberscount?%s', http_build_query($params)))
->json();

return $response['no_of_contacts'] ?? 0;
}


protected function endpoint(): string
{
return sprintf('https://campaigns.%s/api/v1.1', $this->region->domain());
Expand Down
52 changes: 52 additions & 0 deletions src/Campaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,58 @@ public function unsubscribe(string $email, ?string $listName = null): array
];
}


/**
* @return array{success: bool, message?: string}
*/
public function resubscribe(string $email, ?array $contactInfo = [], string $listName = null): array
{
$listKey = $this->resolveListKey($listName);

$additionalParams = ['donotmail_resub' => 'true'];

$response = $this->zohoApi->listSubscribe($listKey, $email, $contactInfo, $additionalParams);

return [
'success' => Arr::get($response, 'status') === 'success',
'message' => Arr::get($response, 'message'),
];
}

/**
* Retrieves subscribers for a given list name.
*
* @param string|null $listName The name of the list. If null, the default list name will be used.
* @param array $options An array of options for the request. Possible keys include:
* - 'sort': The sort order of the results. Possible values are 'asc' for ascending order and 'desc' for descending order.
* - 'fromindex': The starting index for the results. This is a number.
* - 'range': The range of results to retrieve. This is a number.
* - 'status': The status of the subscribers to retrieve. Possible values are 'active', 'recent', 'mostrecent', 'unsub', and 'bounce'.
* @return array The list of subscribers.
*/
public function getSubscribers(?string $listName = null, array $options = []): array
{
$listKey = $this->resolveListKey($listName);

return $this->zohoApi->getListSubscribers($listKey, $options);
}


/**
* Retrieves the count of subscribers for a given list name and status.
*
* @param string|null $listName The name of the list. If null, the default list name will be used.
* @param string $status The status of the subscribers to count. Possible values are 'active', 'unsub', 'bounce', and 'spam'.
* @return int The count of subscribers.
*/
public function countSubscribers(?string $listName = null, string $status = 'active'): int
{
$listKey = $this->resolveListKey($listName);

return $this->zohoApi->getListSubscribersCount($listKey, $status);
}


protected function resolveListKey(?string $listName = null): string
{
$listName = $listName ?? $this->defaultListName;
Expand Down
3 changes: 3 additions & 0 deletions src/Facades/Campaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

/**
* @method static array|null subscribe(string $email, ?array $contactInfo = [], string $listName = null)
* @method static array|null resubscribe(string $email, ?array $contactInfo = [], string $listName = null)
* @method static array|null unsubscribe(string $email, string $listName = null)
* @method static array|null getSubscribers(?string $listName = null, array $options = [])
* @method static array|null countSubscribers(?string $listName = null, string $status = 'active')
*
* @see \Keepsuit\Campaigns\Campaigns
*/
Expand Down