public
Description: An AS3 Flash API wrapper for Campaign Monitor
Homepage: http://www.campaignmonitor.com/api/kits/
Clone URL: git://github.com/campaignmonitor/flash-api-wrapper-as3.git
flash-api-wrapper-as3 / CMRequest.as
100644 151 lines (127 sloc) 6.049 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
/*
* This class allows you to send a subscriber request to campaign monitor. Example of use below:
*
* NOTE: The form may have up to 13 fields.
However, the names of the fields in flash must EXACTLY MATCH the name of the custom fields you make on campaign monitor!!!
Make sure you have a submit button (submitBtn) as well as a message textfield (msg),
in order to display to the user if the request was successful.
Below is an example of use:
import CMRequest;
var newsletterRequest:CMRequest;
submitBtn.addEventListener(MouseEvent.CLICK, submit);
function submit(e:MouseEvent):void {
if (email.text !== "" &&
firstName.text !== "" &&
lastName.text !== "" &&
address1.text !== "" &&
city.text !== "" &&
state.text !== "" &&
zip.text !== "" &&
phone.text !== "") {
msg.text = "Processing...";
// pass in the text field names to CMRequest()
newsletterRequest = new CMRequest("efiahdfy137856hsyeyrhkfs", "327987528klahjksdagsausdji73",
firstName, lastName, email,
address1, address2, city,
state, zip, phone);
newsletterRequest.addEventListener(CMRequest.ON_FINISHED, getResponse);
} else {
msg.text = "Please fill out all fields";
}
};
function getResponse(e:Event):void {
msg.text = newsletterRequest.result;
}
 
*
* Created By: Ben Vogelzang
* © Soldier Design 2008
*
* Feel free to use this code however you please.
* I only ask that you keep something that references me as a source of your code. Thanks!
*
* 2009-07-18 - Corrections made by Paul Chang (paul@paulchang.com). Filename renamed to CMRequest.as and several variables cast correctly.
*/
 
package com.campaignmonitor {
 
import flash.events.*;
import flash.net.*;
 
public class CMRequest extends EventDispatcher {
 
 
public static const ON_FINISHED:String = "OnFinished";
private static const INVALID_EMAIL:uint = 1; // error code for an invalid email
private static const SUCCESS:uint = 0; // code for successful request
private static const INVALID_API_KEY:uint = 100; // error code for a invalid api key
private static const INVALID_LIST_ID:uint = 101; // error code for a invalid list id
private var urlLoader:URLLoader; // the loader that gets the info from campaign monitor
private var _response:String; // the response message to show the user
 
/*
* @param apiID - the campaign monitor api id number
* @param listID - the campaign monitor list id number
* @param firstName - the first name text field of the participant
* @param lastName - the last name text field of the participant
* @param optionalFields - the rest of the fields that you need for campaign monitor.
* NOTE: These must have the same name variable name as optional fields on campaign monitor.
*
*/
public function addWithCustomFields(apiID:String, listID:String, firstName:String, lastName:String, email:String, ... optionalFields):void {
 
var myApiId:String = apiID;
var myListId:String = listID;
 
// build the soap call with appropriate content
var theSoapCall:String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<SOAP-ENV:Body><Subscriber.AddWithCustomFields xmlns=\"http://api.createsend.com/api/\">";
theSoapCall += "<ApiKey>" + myApiId + "</ApiKey>";
theSoapCall += "<ListID>" + myListId + "</ListID>";
theSoapCall += "<Email>" + email + "</Email>";
theSoapCall += "<Name>" + firstName + " " + lastName + "</Name>";
 
// add all the custom fields to the soap call
theSoapCall += "<CustomFields>";
for (var i:uint=0; i < optionalFields.length; i++) {
theSoapCall += "<SubscriberCustomField><Key>[" + optionalFields[i].name + "]</Key><Value>" + optionalFields[i].text + "</Value></SubscriberCustomField>"
}
theSoapCall += "</CustomFields>";
theSoapCall += "</Subscriber.AddWithCustomFields></SOAP-ENV:Body></SOAP-ENV:Envelope>";
 
 
// send off the soap call and get back the response from campaign monitor
var contactSendXML:XML = new XML (theSoapCall);
var urlRequest:URLRequest = new URLRequest("http://api.createsend.com/api/api.asmx");
urlRequest.method = URLRequestMethod.POST;
urlRequest.requestHeaders.push(new URLRequestHeader("Content-Type","text/xml; charset=utf-8"));
urlRequest.requestHeaders.push(new URLRequestHeader("SOAPAction","http://api.createsend.com/api/Subscriber.AddWithCustomFields"));
urlRequest.data = contactSendXML;
 
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, onLoaded);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ifFailed);
urlLoader.load(urlRequest);
}
 
// called when campaign monitor has responded
private function onLoaded(e:Event):void {
 
// set the response to whatever campaign monitor returned
var recieveXML:XML = new XML(urlLoader.data);
     if (recieveXML..*::Code == INVALID_EMAIL) {
trace(recieveXML..*::Message);
_response = "Invalid Email Address!";
}
else if (recieveXML..*::Code == SUCCESS) {
trace(recieveXML..*::Message);
_response = "Thank You!";
}
else if (recieveXML..*::Code == INVALID_API_KEY) {
trace(recieveXML..*::Message);
_response = "Invalid API Key";
}
else if (recieveXML..*::Code == INVALID_LIST_ID) {
trace(recieveXML..*::Message);
_response = "Invalid ListID";
}
 
dispatchEvent(new Event(ON_FINISHED));
}
 
// called if the request failed to send or campaign monitor did not respond
private function ifFailed(e:IOErrorEvent):void {
trace("Soap call could not be sent because " + e);
}
 
// accessor to get the response
public function get result():String {
return _response;
}
}
}