File size: 1,787 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace MauticPlugin\MauticEmailMarketingBundle\Api;

use Mautic\PluginBundle\Exception\ApiErrorException;

class ConstantContactApi extends EmailMarketingApi
{
    private string $version = 'v2';

    protected function request($endpoint, $parameters = [], $method = 'GET', $query = [])
    {
        $url = sprintf('https://api.constantcontact.com/%s/%s?api_key=%s', $this->version, $endpoint, $this->keys['client_id']);

        $response = $this->integration->makeRequest($url, $parameters, $method, [
            'encode_parameters' => 'json',
            'append_auth_token' => true,
            'query'             => $query,
        ]);

        if (is_array($response) && !empty($response[0]['error_message'])) {
            $errors = [];
            foreach ($response as $error) {
                $errors[] = $error['error_message'];
            }

            throw new ApiErrorException(implode(' ', $errors));
        } else {
            return $response;
        }
    }

    /**
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function getLists()
    {
        return $this->request('lists');
    }

    /**
     * @param array $fields
     * @param array $config
     *
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function subscribeLead($email, $listId, $fields = [], $config = [])
    {
        $parameters = array_merge($fields, [
            'lists' => [
                ['id' => "$listId"],
            ],
            'email_addresses' => [
                ['email_address' => $email],
            ],
        ]);

        $query = [
            'action_by' => $config['action_by'],
        ];

        return $this->request('contacts', $parameters, 'POST', $query);
    }
}