File size: 2,701 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace MauticPlugin\MauticEmailMarketingBundle\Api;

use Mautic\PluginBundle\Exception\ApiErrorException;

class MailchimpApi extends EmailMarketingApi
{
    private string $version = '3.0';

    /**
     * @param array  $parameters
     * @param string $method
     *
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    protected function request($endpoint, $parameters = [], $method = 'GET')
    {
        if (isset($this->keys['password'])) {
            // Extract the dc from the key
            $parts = explode('-', $this->keys['password']);
            if (2 !== count($parts)) {
                throw new ApiErrorException('Invalid key');
            }

            $dc                   = $parts[1];
            $apiUrl               = 'https://'.$dc.'.api.mailchimp.com';
            $parameters['apikey'] = $this->keys['password'];
        } else {
            $apiUrl               = $this->keys['api_endpoint'];
            $parameters['apikey'] = $this->keys['access_token'];
        }
        $url = sprintf('%s/%s/%s', $apiUrl, $this->version, $endpoint);

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

        if (is_array($response) && !empty($response['status']) && 'error' == $response['status']) {
            throw new ApiErrorException($response['error']);
        } elseif (is_array($response) && !empty($response['errors'])) {
            $errors = [];
            foreach ($response['errors'] as $error) {
                $errors[] = $error['message'];
            }
            throw new ApiErrorException(implode(' ', $errors));
        } else {
            return $response;
        }
    }

    public function getLists()
    {
        return $this->request('lists', ['limit' => 100]);
    }

    /**
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function getCustomFields($listId)
    {
        return $this->request('lists/'.$listId.'/merge-fields');
    }

    /**
     * @param array $fields
     * @param array $config
     *
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function subscribeLead($email, $listId, $fields = [], $config = [])
    {
        $emailStruct        = new \stdClass();
        $emailStruct->email = $email;

        $parameters = array_merge($config, [
            'id' => $listId,
        ]);
        if (!empty($fields)) {
            $parameters = array_merge($parameters, ['merge_fields' => $fields]);
        }
        $parameters['email_address'] = $email;

        return $this->request('lists/'.$listId.'/members', $parameters, 'POST');
    }
}