From 77fadd83d2907dc3416f1972936c1052be076765 Mon Sep 17 00:00:00 2001 From: Sean Delaney Date: Fri, 29 Nov 2024 16:58:24 +0000 Subject: [PATCH] feat(SFT-1624): added additional error catching for tags in mailchimp integrations (#1663) --- .../Mailchimp/BaseMailchimpIntegration.php | 94 +++++++++++++------ 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/packages/plugin/src/Integrations/EmailMarketing/Mailchimp/BaseMailchimpIntegration.php b/packages/plugin/src/Integrations/EmailMarketing/Mailchimp/BaseMailchimpIntegration.php index 6e892bba3..a1e4b8521 100644 --- a/packages/plugin/src/Integrations/EmailMarketing/Mailchimp/BaseMailchimpIntegration.php +++ b/packages/plugin/src/Integrations/EmailMarketing/Mailchimp/BaseMailchimpIntegration.php @@ -211,14 +211,20 @@ protected function manageTags(Client $client, string $listId, string $email, arr $emailHash = md5(strtolower($email)); - $response = $client->get( - $this->getEndpoint('/lists/'.$listId.'/members/'.$emailHash.'/tags'), - [ - 'query' => [ - 'count' => 999, + try { + $response = $client->get( + $this->getEndpoint('/lists/'.$listId.'/members/'.$emailHash.'/tags'), + [ + 'query' => [ + 'count' => 999, + ], ], - ], - ); + ); + + $this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response); + } catch (RequestException $exception) { + throw $exception; + } $json = json_decode((string) $response->getBody()); @@ -265,15 +271,21 @@ private function fetchTags(Client $client, string $listId): array return $this->existingTags; } - $response = $client->get( - $this->getEndpoint('/lists/'.$listId.'/segments'), - [ - 'query' => [ - 'fields' => 'segments.id,segments.name', - 'count' => 999, + try { + $response = $client->get( + $this->getEndpoint('/lists/'.$listId.'/segments'), + [ + 'query' => [ + 'fields' => 'segments.id,segments.name', + 'count' => 999, + ], ], - ], - ); + ); + + $this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response); + } catch (RequestException $exception) { + throw $exception; + } $json = json_decode((string) $response->getBody()); @@ -293,15 +305,21 @@ private function getOrCreateTag(Client $client, string $listId, string $tagName) return array_search($tagNameLowerCase, $existingTags, true); } - $response = $client->post( - $this->getEndpoint('/lists/'.$listId.'/segments'), - [ - 'json' => [ - 'name' => $tagName, - 'static_segment' => [], + try { + $response = $client->post( + $this->getEndpoint('/lists/'.$listId.'/segments'), + [ + 'json' => [ + 'name' => $tagName, + 'static_segment' => [], + ], ], - ], - ); + ); + + $this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response); + } catch (RequestException $exception) { + throw $exception; + } $json = json_decode((string) $response->getBody()); @@ -313,21 +331,35 @@ private function addTagsForMember(Client $client, string $listId, string $email, foreach ($tags as $tag) { $tagId = $this->getOrCreateTag($client, $listId, $tag); - $client->post( - $this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members'), - [ - 'json' => [ - 'email_address' => $email, + try { + $response = $client->post( + $this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members'), + [ + 'json' => [ + 'email_address' => $email, + ], ], - ], - ); + ); + + $this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response); + } catch (RequestException $exception) { + throw $exception; + } + + $json = json_decode((string) $response->getBody()); } } private function deleteTagsForMember(Client $client, string $listId, string $emailHash, array $tagsToDelete): void { foreach ($tagsToDelete as $tagId => $tagName) { - $client->delete($this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members/'.$emailHash)); + try { + $response = $client->delete($this->getEndpoint('/lists/'.$listId.'/segments/'.$tagId.'/members/'.$emailHash)); + + $this->triggerAfterResponseEvent(self::CATEGORY_TAG, $response); + } catch (RequestException $exception) { + throw $exception; + } } }