/USPS-CFC

ColdFusion CFC used to connect to the USPS API.

Primary LanguageColdFusion

ColdFusion USPS CFC

This is a ColdFusion CFC used to connect to the USPS API. This has been tested with ColdFusion 9 and 10.

API's Supported:

  • CarrierPickupAvailability
  • CityStateLookup
  • ExpressMailCommitment
  • IntlRateV2
  • RateV4
  • TrackV2
  • Verify
  • ZipCodeLookup

In order for this CFC to work you need a USPS API account and you'll need to perform the tests required by the USPS to activate your account.

Initialization

This is a straight forward CFC. You can initialize it using your preferred method. Here are a few examples. The named arguments are listed for clarity but you can also pass the values in by themselves.

CreateObject()

variables.uspsUserID = 'YourIdHere';
variables.usps = CreateObject('component', 'usps').init(isProduction=false, isSecure=false, uspsUserID=variables.uspsUserID);

New Keyword

variables.uspsUserID = 'YourIdHere';
variables.usps = New usps(isProduction=false, isSecure=false, uspsUserID=variables.uspsUserID);

ColdSpring

This assumes you're setting the properties elsewhere, otherwise you can set them as static strings.

<bean id="usps" class="usps" singleton="true" lazy-init="true" autowire="no">
	<constructor-arg name="isProduction"><value>${isProduction}</value></constructor-arg>
	<constructor-arg name="isSecure"><value>${isSecure}</value></constructor-arg>
	<constructor-arg name="uspsUserID"><value>${uspsUserID}</value></constructor-arg>
</bean>

Sample Usage

Here's an example in cfscript:

<cfscript>
variables.uspsUserID = 'YourIdHere';
variables.usps = New usps(false, false, variables.uspsUserID);

// USPS Canned Test: This test cleanses an address and provides the ZIP+4 value.
variables.Verify1 = variables.usps.AddressValidate(
	Address2 = '6406 Ivy Lane',
	City = 'Greenbelt',
	State = 'MD'
);

// USPS Canned Test: This test will also cleanse the address and completes the ZIP Code.
variables.Verify2 = variables.usps.AddressValidate(
	Address2 = '6406 IVY LN',
	City = 'GREENBELT',
	State = 'MD',
	Zip5 = '20770',
	Zip4 = '1440'
);
</cfscript>

Files Included

usps.cfc

This is where the magic happens.

USPSCannedTests.cfm

The USPS Development Guide includes a number of canned tests. This file has them preconfigured and ready to go. Plug in your USPS user ID and run it.

ManualTester.html

This is a plain HTML file with a form to post directly to the USPS Test API. Useful for debugging the XML outside of ColdFusion.

Pull Requests

I don't have all the USPS API's implemented so if there's one you'd like to add, feel free to do so with a pull request. Just be sure to follow the general method pattern for a consistent implementation.

License

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.