diff --git a/src/Api/ZohoCampaignsApi.php b/src/Api/ZohoCampaignsApi.php index 040498a..f130a3c 100644 --- a/src/Api/ZohoCampaignsApi.php +++ b/src/Api/ZohoCampaignsApi.php @@ -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') @@ -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()); diff --git a/src/Campaigns.php b/src/Campaigns.php index bb62ae9..572c451 100755 --- a/src/Campaigns.php +++ b/src/Campaigns.php @@ -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; diff --git a/src/Facades/Campaigns.php b/src/Facades/Campaigns.php index 4641e31..e996bb0 100644 --- a/src/Facades/Campaigns.php +++ b/src/Facades/Campaigns.php @@ -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 */