-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailchimp.cfc
More file actions
216 lines (166 loc) · 7.23 KB
/
mailchimp.cfc
File metadata and controls
216 lines (166 loc) · 7.23 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
component accessors="true" {
property name="APIKey" type="string";
property name="subdomain" type="string" setter="false";
property name="domain" type="string" setter="false";
property name="version" type="string" setter="false";
property name="outputFormat" type="string";
variables.defaultFormat = 'json';
variables.allowedFormats = 'json,xml,php';
public any function init( string APIKey = '', string outputFormat = variables.defaultFormat ) {
if ( len( arguments.APIKey ) ) {
setAPIKey( arguments.APIKey );
variables.subdomain = listLast( getAPIKey(), '-' );
}
variables.domain = '.api.mailchimp.com';
variables.version = '2.0';
setOutputFormat( arguments.outputFormat );
return this;
}
public void function setOutputFormat( string format = variables.defaultFormat ) {
arguments.format = lCase( arguments.format );
if ( !listFind( variables.allowedFormats, arguments.format ) ) {
variables.outputFormat = variables.defaultFormat;
} else {
variables.outputFormat = arguments.format;
}
}
private string function getURL( required string section, required string method ) {
var APIURL = 'https://';
APIURL &= getSubdomain() & getDomain();
APIURL &= '/' & getVersion();
APIURL &= '/' & arguments.section;
APIURL &= '/' & arguments.method & '.' & getOutputFormat();
return APIURL;
}
private any function postAPI( required string section, required string method, struct options = {} ) {
var httpService = new http();
var httpResult = '';
var result = {
'status' = 'error',
'code' = -99,
'name' = 'Unknown_Exception',
'error' = 'There was a connection error.'
};
httpService.setURL( getURL( arguments.section, arguments.method ) );
httpService.setMethod('post');
httpService.setTimeout( 1600 );
httpService.addParam( name="Content-Type", type="header", value="application/json" );
arguments.options[ "apikey" ] = getAPIKey();
httpService.addParam( type="body", value=serializeJSON( arguments.options ) );
httpResult = httpService.send().getPrefix().fileContent;
if ( getOutputFormat() == 'json' && isJSON( httpResult ) ) {
result = deserializeJSON( httpResult );
} else if ( getOutputFormat() == 'xml' && isXML( httpResult ) ) {
result = parseXML( httpResult );
} else {
result.error = httpResult;
}
return result;
}
// /lists
// ------------
// /lists/list
// ------------
// Retrieve all of the lists defined for your user account
// ------------
// list(string apikey, struct filters, int start, int limit, string sort_field, string sort_dir)
// ------------
public struct function listsList( struct filters = structNew(), numeric start = 0, numeric limit = 0, string sort_field = '', string sort_dir = '' ) {
var results = {
"total" = 0,
"data" = [],
"errors" = []
};
var options = {};
if ( structCount( arguments.filters ) ) {
options[ "filters" ] = arguments.filters;
}
if ( val( arguments.start ) ) {
options[ "start" ] = val( arguments.start );
}
if ( val( arguments.limit ) ) {
options[ "limit" ] = val( arguments.limit );
}
if ( len( arguments.sort_field ) ) {
options[ "sort_field" ] = arguments.sort_field;
}
if ( len( arguments.sort_dir ) ) {
options[ "sort_dir" ] = arguments.sort_dir;
}
structAppend( results, postAPI( 'lists', 'list', options ) );
return results;
}
// ----------------
// /lists/subscribe
// ----------------
// Subscribe the provided email to a list. By default this sends a confirmation email - you will not see new members until the link contained in it is clicked!
// ----------------
// subscribe(string apikey, string id, struct email, struct merge_vars, string email_type, bool double_optin, bool update_existing, bool replace_interests, bool send_welcome)
// ----------------
public struct function listsSubscribe( required string listid, required struct email, struct merge_vars = {}, string email_type = '', boolean double_optin = true, boolean update_existing = false, boolean replace_interests = true, boolean send_welcome = false ) {
var results = {
"email" = '',
"euid" = '',
"leid" = ''
};
var options = {};
options[ "id" ] = arguments.listid;
options[ "email" ] = arguments.email;
if ( structCount( arguments.merge_vars ) ) {
options[ "merge_vars" ] = arguments.merge_vars;
}
if ( len( arguments.email_type ) ) {
options[ "email_type" ] = arguments.email_type;
}
options[ "double_optin" ] = arguments.double_optin;
options[ "update_existing" ] = arguments.update_existing;
options[ "replace_interests" ] = arguments.replace_interests;
options[ "send_welcome" ] = arguments.send_welcome;
structAppend( results, postAPI( 'lists', 'subscribe', options ) );
return results;
}
// ------------------
// /lists/unsubscribe
// ------------------
// Unsubscribe the given email address from the list
// ------------------
// unsubscribe(string apikey, string id, struct email, boolean delete_member, boolean send_goodbye, boolean send_notify)
// ------------------
public struct function listsUnsubscribe( required string listid, required struct email, boolean delete_member = false, boolean send_goodbye = true, boolean send_notify = true ) {
var results = {
"complete" = false
};
var options = {};
options[ "id" ] = arguments.listid;
options[ "email" ] = arguments.email;
options[ "delete_member" ] = arguments.delete_member;
options[ "send_goodbye" ] = arguments.send_goodbye;
options[ "send_notify" ] = arguments.send_notify;
structAppend( results, postAPI( 'lists', 'unsubscribe', options ) );
return results;
}
// ------------------
// /lists/update-member
// ------------------
// Updates a given member from the list
// ------------------
// update-member(string apikey, string id, struct email, struct merge_vars, string email_type, boolean replace_interests)
// ------------------
public struct function listsUpdateMember( required string listid, required struct email, required struct merge_vars, string email_type = '', boolean replace_interests = true ) {
var results = {
"email" = '',
"euid" = '',
"leid" = ''
};
var options = {};
options[ "id" ] = arguments.listid;
options[ "email" ] = arguments.email;
options[ "merge_vars" ] = arguments.merge_vars;
if ( len( arguments.email_type ) ) {
options[ "email_type" ] = arguments.email_type;
}
options[ "replace_interests" ] = arguments.replace_interests;
structAppend( results, postAPI( 'lists', 'update-member', options ) );
return results;
}
}