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

Added support for PUT requests to the subscribe endpoint #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/controllers/MailchimpController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* A minimal Craft plugin to connect forms to Mailchimp
*
Expand All @@ -17,22 +18,28 @@
/*
* Class MailchimpController
*/

class MailchimpController extends Controller
{
// Protected Properties
// =========================================================================

protected int|bool|array $allowAnonymous = true;

protected const ALLOWED_METHODS = ['POST', 'PUT'];

// Public Methods
// =========================================================================

public function actionSubscribe(): Response
{
if ($_POST) {
return $this->asJson(SimpleMailchimp::getInstance()->smcService->subscribe($_POST));
if (in_array($this->request->method, self::ALLOWED_METHODS)) {
$params = $this->request->getBodyParams();

return $this->asJson(SimpleMailchimp::getInstance()->smcService->subscribe($params, $this->request->isPut));
}

return $this->asJson(['success' => false, 'msg' => 'Direct access not allowed']);
$this->response->setStatusCode(405);
return $this->asJson(['success' => false, 'msg' => 'Method not allowed']);
}
}
12 changes: 9 additions & 3 deletions src/services/MailchimpService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* A minimal Craft plugin to connect forms to Mailchimp
*
Expand All @@ -18,12 +19,13 @@
/*
* Class MailchimpService
*/

class MailchimpService extends Component
{
// Public Methods
// =========================================================================

public function subscribe($data): array
public function subscribe(array $data, bool $upsert = false): array
{
if (empty($data["email"])) {
return ['success' => false, 'msg' => 'Email can\'t be empty'];
Expand Down Expand Up @@ -52,14 +54,18 @@ public function subscribe($data): array

$MailChimp = new MC(App::parseEnv($settings['mcApiKey'] ?: ''));

$result = $MailChimp->post("lists/" . App::parseEnv($settings['mcListID'] ?: '') . "/members", $dataMC);
if ($upsert) {
$result = $MailChimp->put("lists/" . App::parseEnv($settings['mcListID'] ?: '') . "/members/" . md5($data["email"]), $dataMC);
} else {
$result = $MailChimp->post("lists/" . App::parseEnv($settings['mcListID'] ?: '') . "/members", $dataMC);
}


if ($result['status'] == 'subscribed') {
return ['success' => true, 'msg' => 'Email subscribed successfully', 'id' => $result['contact_id']];
}

return ['success' => false, 'msg' => 'Mailchimp error: ' . $result['title']];

} catch (\Exception $e) {
return ['success' => false, 'msg' => $e->getMessage()];
}
Expand Down