diff --git a/README.md b/README.md index 01a82b8..6c9e877 100644 --- a/README.md +++ b/README.md @@ -173,20 +173,20 @@ $promotion = Trustpilot::businessUnit()->promotion(); ### Products ```php -// Get all products +// Get all products. $products = Trustpilot::products()->get(); -// Get the product with a sku of "samsung-galaxy-s10" +// Get the product with a sku of "samsung-galaxy-s10". $product = Trustpilot::products() ->where('skus', 'samsung-galaxy-s10') ->first(); -// Get all products with a sku of "samsung-galaxy-s8", "samsung-galaxy-s9" or "samsung-galaxy-s10" +// Get all products with a sku of "samsung-galaxy-s8", "samsung-galaxy-s9" or "samsung-galaxy-s10". $products = Trustpilot::products() ->where('skus', ['samsung-galaxy-s8', 'samsung-galaxy-s9', 'samsung-galaxy-s10']) ->get(); -// Updating a product +// Updating a product. $product = Trustpilot::products() ->where('skus', 'samsung-galaxy-s10') ->first(); @@ -196,20 +196,82 @@ $product->currency = 'USD'; $product->save(); ``` +### Inivitations +```php +// Create a service invitation to John Doe from Business Name now. +Trustpilot::businessUnit()->invitation()->service( + '#123456', 'John Doe', 'john.doe@example.com', + 'no-reply@business.com', 'Business Name' +); + +// Create a service invitation to John Doe from Business Name now to be sent +// in 5 days using a custom template id and to redirect to our website afterwards. +Trustpilot::businessUnit()->invitation()->service( + '#123456', 'John Doe', 'john.doe@example.com', + 'no-reply@business.com', 'Business Name', 'support@business.com', + \Carbon\Carbon::now()->addDays(5), '507f191e810c19729de860ea', + 'https://example.com/', +); + +// Create a product and service invitation from product skus. +Trustpilot::businessUnit()->invitation()->products( + ['samsung-galaxy-s8', 'samsung-galaxy-s9', 'samsung-galaxy-s10'], + '#123456', 'John Doe', 'john.doe@example.com', + 'no-reply@business.com', 'Business Name' +); + +// Create a product and service invitation from products. +Trustpilot::businessUnit()->invitation()->products( + [ + new Product([ + 'sku' => 'samsung-galaxy-s8', + 'name' => 'Samsung Galaxy S8', + 'brand' => 'Samsung Galaxy', + 'productUrl' => 'https://example.com/products/samsung-galaxy-s8', + 'imageUrl' => 'https://example.com/products/images/samsung-galaxy-s8.jpg', + ]), + new Product([ + 'sku' => 'samsung-galaxy-s10', + 'name' => 'Samsung Galaxy S10', + 'brand' => 'Samsung Galaxy', + 'productUrl' => 'https://example.com/products/samsung-galaxy-s10', + 'imageUrl' => 'https://example.com/products/images/samsung-galaxy-s10.jpg', + ]), + ], + '#123456', 'John Doe', 'john.doe@example.com', + 'no-reply@business.com', 'Business Name' +); + +// Get a list of templates available to be used in invitations. +$templates = Trustpilot::businessUnit()->invitation()->templates(); + +// Generate a service review invitation link. +$inviteUrl = Trustpilot::businessUnit()->invitation()->generateLink('#123456', 'John Doe', 'john.doe@example.com', 'https://example.com/'); + +// Delete all invitation data related to the given e-mails. +Trustpilot::businessUnit()->invitation()->deleteByEmails([ + 'john.doe@example.com', + 'jane.doe@example.com', +]); + +// Delete all invitation data older than 5 years. +Trustpilot::businessUnit()->invitation()->deleteBeforeDate(\Carbon\Carbon::now()->subYears(5)); +``` + ### Categories ```php -// Get all categories +// Get all categories. $categories = Trustpilot::categories()->get(); -// Get categories with a parent of "banking_money" +// Get categories with a parent of "banking_money". $categories = Trustpilot::categories() ->where('parentId', 'banking_money') ->get(); -// Get category by id +// Get category by id. $category = Trustpilot::categories()->find('trust_bank'); -// Get first three business units with the same category as your business unit +// Get first three business units with the same category as your business unit. $businessUnits = Trustpilot::businessUnit()->categories()->first()->businessUnits()->limit(3)->get(); ``` diff --git a/src/API/Api.php b/src/API/Api.php index f1fbb31..e2bbcc4 100644 --- a/src/API/Api.php +++ b/src/API/Api.php @@ -82,6 +82,17 @@ public static function __callStatic($name, $arguments) return call_user_func_array([$instance, $name], $arguments); } + /** + * Set the endpoint of the api. + * + * @return self + */ + protected function setEndpoint(string $endpoint): self + { + $this->endpoint = $endpoint; + return $this; + } + /** * Set the path of the api. * diff --git a/src/API/BusinessUnit/BusinessUnit.php b/src/API/BusinessUnit/BusinessUnit.php index 86d5c08..545d066 100644 --- a/src/API/BusinessUnit/BusinessUnit.php +++ b/src/API/BusinessUnit/BusinessUnit.php @@ -2,6 +2,7 @@ namespace McCaulay\Trustpilot\API\BusinessUnit; use McCaulay\Trustpilot\API\BusinessUnit\Product\ProductApi; +use McCaulay\Trustpilot\API\BusinessUnit\Review\Invitation\InvitationApi; use McCaulay\Trustpilot\API\Resource; use McCaulay\Trustpilot\Query\Builder; @@ -42,7 +43,17 @@ public function reviews(): Builder */ public function products(): ProductApi { - return new Product\ProductApi($this->id); + return new ProductApi($this->id); + } + + /** + * Get the invitation api. + * + * @return \McCaulay\Trustpilot\API\BusinessUnit\Review\Invitation\InvitationApi + */ + public function invitation(): InvitationApi + { + return new InvitationApi($this->id); } /** diff --git a/src/API/BusinessUnit/Review/Invitation/InvitationApi.php b/src/API/BusinessUnit/Review/Invitation/InvitationApi.php new file mode 100644 index 0000000..c029023 --- /dev/null +++ b/src/API/BusinessUnit/Review/Invitation/InvitationApi.php @@ -0,0 +1,216 @@ +businessUnitId = $businessUnitId ?? config('trustpilot.unit_id'); + $this->setPath('/private/business-units/' . $this->businessUnitId); + $this->setEndpoint(config('trustpilot.endpoints.invitation')); + } + + /** + * Create the product review invite. + * + * @param array $products An array of product skus, or products + * @param string $referenceId + * @param string $customerName + * @param string $customerEmail + * @param string $replyTo + * @param string $senderName + * @param string $senderEmail + * @param null|\DateTime $sendProductAt When to send the product review invite at. Note this must be after the send at. + * @param null|\DateTime $sendAt When to send the service review invite at. + * @param null|string $templateId + * @param null|string $redirectUrl + * @param array $tags + * @param string $locale + * @param null|string $locationId + * @return void + */ + public function products(array $products, string $referenceId, + string $customerName, string $customerEmail, string $replyTo, + string $senderName, + ?string $senderEmail = null, + ?DateTime $sendProductAt = null, + ?DateTime $sendAt = null, ?string $templateId = null, + ?string $redirectUrl = null, array $tags = [], string $locale = 'en-GB', + ?string $locationId = null + ): void { + // Default send product at + if ($sendProductAt == null) { + $sendProductAt = $sendAt == null ? Carbon::now()->subDay() : $sendAt->copy()->addDay(); + } + + // Setup product review invitation array + $productReviewInvitation = array_filter([ + 'preferredSendTime' => $sendProductAt->format('Y-m-d\TH:i:s'), + 'redirectUri' => $redirectUrl, + 'templateId' => $templateId, + ]); + + // Append products or product skus + $productKey = 'productSkus'; + if (count($products) > 0) { + if (Arr::isAssoc($products) || !is_string($products[0])) { + $productKey = 'products'; + $products = collect($products)->map(function ($product) { + if (is_array($product)) { + return $product; + } + return $product->toArray(); + })->toArray(); + } + } + $productReviewInvitation[$productKey] = $products; + + $this->service( + $referenceId, $customerName, $customerEmail, $replyTo, $senderName, + $senderEmail, $sendAt, $templateId, $redirectUrl, $tags, + $locale, $locationId, $productReviewInvitation + ); + } + + /** + * Create the service review invite. + * + * @param string $referenceId + * @param string $customerName + * @param string $customerEmail + * @param string $replyTo + * @param string $senderName + * @param string $senderEmail + * @param null|\DateTime $sendAt When to send the service review invite at. + * @param null|string $templateId + * @param null|string $redirectUrl + * @param array $tags + * @param string $locale + * @param null|string $locationId + * @param array $productReviewInvitation + * @return void + */ + public function service(string $referenceId, string $customerName, + string $customerEmail, string $replyTo, string $senderName, + ?string $senderEmail = null, + ?DateTime $sendAt = null, ?string $templateId = null, + ?string $redirectUrl = null, array $tags = [], string $locale = 'en-GB', + ?string $locationId = null, array $productReviewInvitation = [] + ): void { + // Default send at + if ($sendAt == null) { + $sendAt = Carbon::now()->subDays(2); + } + + // Default sender email + if ($senderEmail == null) { + $senderEmail = 'noreply.invitations@trustpilotmail.com'; + } + + $this->post('/email-invitations', [], array_filter([ + 'referenceNumber' => $referenceId, + 'consumerName' => $customerName, + 'consumerEmail' => $customerEmail, + 'senderName' => $senderName, + 'senderEmail' => $senderEmail, + 'replyTo' => $replyTo, + 'locale' => $locale, + 'locationId' => $locationId, + 'serviceReviewInvitation' => array_filter([ + 'preferredSendTime' => $sendAt->format('Y-m-d\TH:i:s'), + 'redirectUri' => $redirectUrl, + 'tags' => $tags, + 'templateId' => $templateId, + ]), + 'productReviewInvitation' => $productReviewInvitation, + ]), true); + } + + /** + * Generate a review invitation link. + * + * @param string $referenceId + * @param string $name + * @param string $email + * @param null|string $redirectUrl + * @param array $tags + * @param string $locale + * @param null|string $locationId + * @return string + */ + public function generateLink(string $referenceId, string $name, + string $email, ?string $redirectUrl = null, array $tags = [], + string $locale = 'en-GB', ?string $locationId = null + ): string { + return $this->post('/invitation-links', [], array_filter([ + 'referenceId' => $referenceId, + 'name' => $name, + 'email' => $email, + 'redirectUri' => $redirectUrl, + 'tags' => $tags, + 'locale' => $locale, + 'locationId' => $locationId, + ]), true)->url; + } + + /** + * Delete all information data related to the e-mails. + * + * @param array $emails + * @return void + */ + public function deleteByEmails(array $emails): void + { + $this->post('/invitation-data/delete', [], [ + 'customerEmails' => $emails, + ], true); + } + + /** + * Delete all information data before the given date. + * + * @param \DateTime $date + * @return void + */ + public function deleteBeforeDate(DateTime $date): void + { + $this->post('/invitation-data/delete', [], [ + 'deleteOlderThan' => $date->format('Y-m-d\TH:i:s'), + ], true); + } + + /** + * Get a list of invitation templates. + * + * @return \Illuminate\Support\Collection + */ + public function templates(): Collection + { + $response = $this->get('/templates', [], true); + return collect($response->templates)->map(function ($template) { + return (new Template())->data($template); + }); + } +} diff --git a/src/API/BusinessUnit/Review/Invitation/Template.php b/src/API/BusinessUnit/Review/Invitation/Template.php new file mode 100644 index 0000000..cce56b8 --- /dev/null +++ b/src/API/BusinessUnit/Review/Invitation/Template.php @@ -0,0 +1,9 @@ +businessUnitId = $businessUnitId ?? config('trustpilot.unit_id'); - $this->setPath('/product-reviews/business-units/' . $this->businessUnitId); + $this->setPath('/private/business-units/' . $this->businessUnitId); } /** @@ -36,7 +36,7 @@ public function __construct(?string $businessUnitId = null) */ public function perform(array $query, bool $search = false): Collection { - $response = $this->get('/imported-reviews', $query); + $response = $this->get('/email-invitations', $query); return collect($response->productReviews)->map(function ($productReview) { return (new ProductReview())->data($productReview); }); diff --git a/src/API/Resource.php b/src/API/Resource.php index 97aa5a8..e41c591 100644 --- a/src/API/Resource.php +++ b/src/API/Resource.php @@ -6,6 +6,16 @@ class Resource implements Arrayable { + /** + * Initalise the resource. + * + * @param array $data The resource data. + */ + public function __construct($data = []) + { + $this->data($data); + } + /** * Set the data. * diff --git a/src/Trustpilot.php b/src/Trustpilot.php index 3866341..523fd7a 100644 --- a/src/Trustpilot.php +++ b/src/Trustpilot.php @@ -12,11 +12,12 @@ class Trustpilot /** * Get the default business unit. * + * @param string|null $businessUnitId * @return \McCaulay\Trustpilot\API\BusinessUnit\BusinessUnit */ - public function businessUnit(): BusinessUnit + public function businessUnit(?string $businessUnitId = null): BusinessUnit { - return new BusinessUnit(); + return new BusinessUnit($businessUnitId); } /**