This repository has been archived by the owner on Apr 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
usps.cfc
308 lines (287 loc) · 15.4 KB
/
usps.cfc
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!---
File: usps.cfc
For full details see README.md at GitHub:
https://github.com/matthewriley/USPS-CFC
Copyright © Matthew Riley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--->
<cfcomponent name="USPS" displayname="USPS CFC" accessors="true" output="false" hint="This is a ColdFusion CFC used to connect to the USPS API.">
<cfproperty name="isProduction" type="boolean">
<cfproperty name="isSecure" type="boolean">
<cfproperty name="uspsUserID" type="string">
<cfproperty name="url" type="string">
<cfproperty name="URLTEST" type="string">
<cfproperty name="URLTESTSECURE" type="string">
<cfproperty name="URLPROD" type="string">
<cfproperty name="URLPRODSECURE" type="string">
<cffunction name="init" access="public" returntype="USPS" output="false" hint="Initializes the USPS CFC">
<cfargument name="isProduction" type="boolean" required="false" default="#getIsProduction()#">
<cfargument name="isSecure" type="boolean" required="false" default="#getIsSecure()#">
<cfargument name="uspsUserID" type="string" required="false" default="#getUspsUserID()#">
<cfscript>
// set constants (CF9 fix)
setURLTEST("http://testing.shippingapis.com/ShippingAPITest.dll");
setURLTESTSECURE("https://secure.shippingapis.com/ShippingAPITest.dll");
setURLPROD("http://production.shippingapis.com/ShippingAPI.dll");
setURLPRODSECURE("https://secure.shippingapis.com/ShippingAPI.dll");
// set production and secure flags
setIsProduction(arguments.isProduction);
setIsSecure(arguments.isSecure);
setUspsUserID(arguments.uspsUserID);
// configure url based on flags
if(getIsProduction()){
if(getIsSecure()){
setUrl(getURLPRODSECURE());
}
else{
setUrl(getURLPROD());
}
}
else{
if(getIsSecure()){
setUrl(getURLTESTSECURE());
}
else{
setUrl(getURLTEST());
}
}
return this;
</cfscript>
</cffunction>
<cffunction name="processRequest" access="private" returntype="xml" output="false" hint="Makes the HTTP request to the USPS API.">
<cfargument name="api" type="String" required="true">
<cfargument name="xml" type="String" required="true">
<cftry>
<cfhttp url="#getUrl()#" method="post" timeout="3" throwonerror="true">
<cfhttpparam encoded="no" type="formfield" name="api" value="#arguments.api#">
<cfhttpparam encoded="no" type="formfield" name="xml" value="#Trim(arguments.xml)#">
</cfhttp>
<cfset local.responseXML = cfhttp.fileContent />
<cfcatch type="any">
<cfset local.responseXML = '<?xml version="1.0"?>
<Error>
<Type>#cfcatch.Type#</Type>
<Message>#cfcatch.Message#</Message>
</Error>' />
</cfcatch>
</cftry>
<cfreturn XmlParse(local.responseXML)>
</cffunction>
<cffunction name="RateV4" access="public" returntype="xml" output="false" hint="Returns domestic rates from the USPS RateV4 API">
<cfargument name="Service" type="String" required="true">
<cfargument name="FirstClassMailType" type="String" required="false" default="">
<cfargument name="ZipOrigination" type="String" required="true">
<cfargument name="ZipDestination" type="String" required="true">
<cfargument name="Pounds" type="String" required="true">
<cfargument name="Ounces" type="String" required="false" default="0">
<cfargument name="Container" type="String" required="false" default="">
<cfargument name="Size" type="String" required="true">
<cfargument name="Width" type="String" required="false" default="">
<cfargument name="Length" type="String" required="false" default="">
<cfargument name="Height" type="String" required="false" default="">
<cfargument name="Girth" type="String" required="false" default="">
<cfargument name="Machinable" type="String" required="false" default="true">
<cfargument name="ReturnLocations" type="String" required="false" default="">
<cfargument name="ShipDate" type="String" required="false" default="">
<cfset local.api = "RateV4">
<cfsavecontent variable="local.xml"><cfoutput>
<RateV4Request USERID="#getUspsUserID()#">
<Package ID="1">
<Service>#arguments.Service#</Service><cfif Len(Trim(arguments.FirstClassMailType))>
<FirstClassMailType>#arguments.FirstClassMailType#</FirstClassMailType></cfif>
<ZipOrigination>#arguments.ZipOrigination#</ZipOrigination>
<ZipDestination>#arguments.ZipDestination#</ZipDestination>
<Pounds>#Val(Trim(arguments.Pounds))#</Pounds>
<Ounces>#Val(Trim(arguments.Ounces))#</Ounces>
<Container>#arguments.Container#</Container>
<Size>#arguments.Size#</Size><cfif Len(Trim(arguments.Width))>
<Width>#arguments.Width#</Width></cfif><cfif Len(Trim(arguments.Length))>
<Length>#arguments.Length#</Length></cfif><cfif Len(Trim(arguments.Height))>
<Height>#arguments.Height#</Height></cfif><cfif Len(Trim(arguments.Girth))>
<Girth>#arguments.Girth#</Girth></cfif><cfif Len(Trim(arguments.Machinable))>
<Machinable>#arguments.Machinable#</Machinable></cfif><cfif Len(Trim(arguments.ReturnLocations))>
<ReturnLocations>#arguments.ReturnLocations#</ReturnLocations></cfif><cfif IsDate(arguments.ShipDate)>
<ShipDate>#DateFormat(arguments.ShipDate,'DD-mmm-yyyy')#</ShipDate></cfif>
</Package>
</RateV4Request>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="IntlRateV2" access="public" returntype="xml" output="false" hint="Returns international rates from the USPS IntlRateV2 API">
<cfargument name="Pounds" type="String" required="true">
<cfargument name="Ounces" type="String" required="false" default="0">
<cfargument name="Container" type="String" required="false" default="">
<cfargument name="Size" type="String" required="false">
<cfargument name="Width" type="String" required="false" default="0">
<cfargument name="Length" type="String" required="false" default="0">
<cfargument name="Height" type="String" required="false" default="0">
<cfargument name="Girth" type="String" required="false" default="0">
<cfargument name="Machinable" type="String" required="false" default="true">
<cfargument name="MailType" type="String" required="false" default="Package">
<cfargument name="ValueOfContents" type="String" required="false" default="0">
<cfargument name="ReturnLocations" type="String" required="false" default="">
<cfargument name="Country" type="String" required="false" default="Canada">
<cfargument name="ShipDate" type="String" required="false" default="">
<cfargument name="CommercialFlag" type="String" required="false" default="">
<CFSET local.api = "IntlRateV2">
<cfsavecontent VARIABLE="local.xml"><cfoutput>
<IntlRateV2Request USERID="#getUspsUserID()#">
<Package ID="1">
<Pounds>#val(Trim(arguments.Pounds))#</Pounds>
<Ounces>#val(Trim(arguments.Ounces))#</Ounces><cfif Len(Trim(arguments.Machinable))>
<Machinable>#arguments.Machinable#</Machinable></cfif>
<MailType>#arguments.MailType#</MailType>
<ValueOfContents>#arguments.ValueOfContents#</ValueOfContents>
<Country>#arguments.Country#</Country><cfif Len(Trim(arguments.Container))>
<Container>#arguments.Container#</Container></cfif><cfif Len(Trim(arguments.Size))>
<Size>#arguments.Size#</Size></cfif>
<Width>#arguments.Width#</Width>
<Length>#arguments.Length#</Length>
<Height>#arguments.Height#</Height>
<Girth>#arguments.Girth#</Girth><cfif Len(Trim(arguments.ReturnLocations))>
<ReturnLocations>#arguments.ReturnLocations#</ReturnLocations></cfif><cfif IsDate(arguments.ShipDate)>
<ShipDate>#DateFormat(arguments.ShipDate,'DD-mmm-yyyy')#</ShipDate></cfif><cfif Len(arguments.CommercialFlag)>
<CommercialFlag>#arguments.CommercialFlag#</CommercialFlag></cfif>
</Package>
</IntlRateV2Request>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="ZipCodeLookup" access="public" returntype="xml" output="false" hint="Returns ZIP Code and ZIP Code + 4 corresponding to the given address, city, and state from the USPS ZipCodeLookup API">
<cfargument name="FirmName" type="String" required="false" default="">
<cfargument name="Address1" type="String" required="false" default="">
<cfargument name="Address2" type="String" required="true">
<cfargument name="City" type="String" required="true">
<cfargument name="State" type="String" required="true">
<cfset local.api = 'ZipCodeLookup'>
<cfsavecontent variable="local.xml"><cfoutput>
<ZipCodeLookupRequest USERID="#getUspsUserID()#">
<Address ID="1">
<FirmName>#arguments.FirmName#</FirmName>
<Address1>#arguments.Address1#</Address1>
<Address2>#arguments.Address2#</Address2>
<City>#arguments.City#</City>
<State>#arguments.State#</State>
</Address>
</ZipCodeLookupRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="CityStateLookup" access="public" returntype="xml" output="false" hint="Returns city and state corresponding to the given ZIP Code from the USPS CityStateLookup API">
<cfargument name="Zip5" type="String" required="false" default="">
<cfset local.api = 'CityStateLookup'>
<cfsavecontent variable="local.xml"><cfoutput>
<CityStateLookupRequest USERID="#getUspsUserID()#">
<ZipCode ID="1">
<Zip5>#arguments.Zip5#</Zip5>
</ZipCode>
</CityStateLookupRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="AddressValidate" access="public" returntype="xml" output="false" hint="Corrects errors in street addresses, including abbreviations and missing information, and supplies ZIP Codes and ZIP Codes + 4.">
<cfargument name="FirmName" type="String" required="false" default="">
<cfargument name="Address1" type="String" required="false" default="">
<cfargument name="Address2" type="String" required="true">
<cfargument name="City" type="String" required="false" default="">
<cfargument name="State" type="String" required="false" default="">
<cfargument name="Urbanization" type="String" required="false" default="">
<cfargument name="Zip5" type="String" required="false" default="">
<cfargument name="Zip4" type="String" required="false" default="">
<cfset local.api = 'Verify'>
<cfsavecontent variable="local.xml"><cfoutput>
<AddressValidateRequest USERID="#getUspsUserID()#">
<Address ID="1">
<FirmName>#arguments.FirmName#</FirmName>
<Address1>#arguments.Address1#</Address1>
<Address2>#arguments.Address2#</Address2>
<City>#arguments.City#</City>
<State>#arguments.State#</State><cfif Len(Trim(arguments.Urbanization))>
<Urbanization>#arguments.Urbanization#</Urbanization></cfif>
<Zip5>#arguments.Zip5#</Zip5>
<Zip4>#arguments.Zip4#</Zip4>
</Address>
</AddressValidateRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="Track" access="public" returntype="xml" output="false" hint="Lets customers determine the delivery status of their Priority Mail, Express Mail, and Package Services packages with Delivery Confirmation.">
<cfargument name="TrackID" type="String" required="true">
<cfset local.api = 'TrackV2'>
<cfsavecontent variable="local.xml"><cfoutput>
<TrackRequest USERID="#getUspsUserID()#">
<TrackID ID="#arguments.TrackID#"></TrackID>
</TrackRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="TrackField" access="public" returntype="xml" output="false" hint="Identical to the Track request except for the request name and the return information. Data returned still contains the detail and summary information, but this information is broken down into fields instead of having onlone line of text.">
<cfargument name="TrackID" type="String" required="true">
<cfset local.api = 'TrackV2'>
<cfsavecontent variable="local.xml"><cfoutput>
<TrackFieldRequest USERID="#getUspsUserID()#">
<TrackID ID="#arguments.TrackID#"></TrackID>
</TrackFieldRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="ExpressMailCommitment" access="public" returntype="xml" output="false" hint="Provides delivery commitments for Express Mail packages.">
<cfargument name="OriginZIP" type="String" required="true">
<cfargument name="DestinationZIP" type="String" required="true">
<cfargument name="Date" type="String" required="false" default="">
<cfargument name="ReturnDates" type="String" required="false" default="">
<cfset local.api = 'ExpressMailCommitment'>
<cfsavecontent variable="local.xml"><cfoutput>
<ExpressMailCommitmentRequest USERID="#getUspsUserID()#">
<OriginZIP>#arguments.OriginZIP#</OriginZIP>
<DestinationZIP>#arguments.DestinationZIP#</DestinationZIP>
<Date>#arguments.Date#</Date><cfif Len(Trim(arguments.ReturnDates))>
<ReturnDates>#arguments.ReturnDates#</ReturnDates></cfif>
</ExpressMailCommitmentRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
<cffunction name="CarrierPickupAvailability" access="public" returntype="xml" output="false" hint="Checks the availability for Carrier Pickup at a specific address and informs the user of the first available date for pickup.">
<cfargument name="FirmName" type="String" required="false" default="">
<cfargument name="SuiteOrApt" type="String" required="false" default="">
<cfargument name="Address2" type="String" required="true">
<cfargument name="Urbanization" type="String" required="false" default="">
<cfargument name="City" type="String" required="false" default="">
<cfargument name="State" type="String" required="false" default="">
<cfargument name="ZIP5" type="String" required="true">
<cfargument name="ZIP4" type="String" required="false" default="">
<cfargument name="DATE" type="String" required="false" default="">
<cfset local.api = 'CarrierPickupAvailability'>
<cfsavecontent variable="local.xml"><cfoutput>
<CarrierPickupAvailabilityRequest USERID="#getUspsUserID()#">
<FirmName>#arguments.FirmName#</FirmName>
<SuiteOrApt>#arguments.SuiteOrApt#</SuiteOrApt>
<Address2>#arguments.Address2#</Address2>
<Urbanization>#arguments.Urbanization#</Urbanization>
<City>#arguments.City#</City>
<State>#arguments.State#</State>
<ZIP5>#arguments.ZIP5#</ZIP5>
<ZIP4>#arguments.ZIP4#</ZIP4>
<DATE>#arguments.DATE#</DATE>
</CarrierPickupAvailabilityRequest>
</cfoutput></cfsavecontent>
<cfset local.requestResponse = processRequest(local.api, local.xml)>
<cfreturn local.requestResponse>
</cffunction>
</cfcomponent>