Sage Pay CFC

Below is a quick update on the old 'protx.cfc' now 'sagepay.cfc'. It's a little messy and there are a few things I would have done different if I had more time, but I didn't. I wanted to share will you my new CFC because as you may know Sage Pay don't seem to have a ColdFusion VSP Direct integration Kit anymore! Anyhow below will help jump start anyone wanting to integration the gatway using the VSP Direct service they offer.

First make sure you have read their integration guides @ sagepay.com and you will need have a test or simulation account to test first. If you don't have one you can sign up for a free simulation account here. You can get a live account by clicking here.

Download the sagepay.cfc (.rar file 3KB)

Using the sagepay.cfc

The sagepay.cfc is only a starting place, there is more I need to do, one example, I could give is using init() to setup the URL's in the CFC and most likely I will in my next update.

Before calling above CFC you will need to create your unique transaction number. This is used to identify the order you pass to sagepay but you can can also used it as your unique identifier. You will also need to setup all your form variables to post off to the to the CFC yourself as I have not got around yet to putting up a example that's not commercial.

view plain print about
1<cfscript>
2 cfcSagepay = createobject("component", "components.sagepay");
3 gatewaysetup = cfcSagepay.SetGateway(); // not needed please use a init() fo set this up
4

5          Response = cfcSagepay.gatewaySend(PurchaseURL=#gatewaysetup.PurchaseURL#,
6                                    VendorTxCode =', //transaction code
7
amount='
,//transaction account
8
CardHolder=',//customer name and name on card
9
CardNumber='
,//card number
10
StartDate=',//card start date
11
ExpiryDate='
,//card expiry date
12
DeliveryAddress=',//Delivery Address
13
CardType='
,//type of card being used
14
BillingPostCode=',//card post code
15
DeliveryPostCode='
,//Delivery Address
16
CustomerName=',//card holders name
17
CV2='
,//security code
18
billfirstName=',//billing first name
19
billlastName='
,//billing last name
20
BillingAddress1=',//billing address
21
billingcity='
,//billing city
22
DefaultCurrency='GBP',
23
24//options
25
                            
26referrerID=',
27ContactNumber='

28                                        );
29
</cfscript>
30
31//Other optional fields that can be passed to above function
32//Basket
33//ContactNumber
34//ContactFax
35//CustomerEmail
36//ClientIPAddress
37//CAVV
38//XID
39//ECI
40//DSecureStatus
41//referrerID
42//DefaultDescription

Again you will also need to collect the responses from the 'response' strut above. I am working on putting this within the CFC for anyone struggling you will need to slip the response example below

view plain print about
1<!---
2 '****************************************************************************************
3 ' 3D Secure - New responce to sort out with 3D Secure info in it. sort ready to determine next action
4 '****************************************************************************************
5 --->

6<cfif isDefined('PARes')>
7 <cfhttp url="#gatewaysetup.callbackURL#" method="post" delimiter="," resolveurl="no" throwonerror="yes" timeout="120" charset="windows-1252">
8 <cfhttpparam name="MD" value="#FORM.MD#" type="formfield">
9 <cfhttpparam name="PARes" value="#FORM.PARes#" type="formfield">
10 </cfhttp>
11 <cfset Response = StructNew()>
12 <cfloop list="#CFHTTP.FileContent#" index="line" delimiters="#chr(13)#">
13 <cfset line = Trim( line )>
14 <cfset StructInsert( Response, Trim( ListFirst( line, "=" ) ), Trim(Mid(line,Find("=",line)+1,len(line)) ) )>
15 </cfloop>
16</cfif>
17
18
19<cfif #Response.Status# IS "OK" OR #Response.Status# IS "ATTEMPTONLY" >
20all done everything is ok update your database and take them to thank you page
21
22<cfelseif #Response.Status# IS "3DAUTH">
23<cfoutput>
24<h1>3D Secure Verification Needed, Loading please wait...</h1>
25 <InvalidTag LANGUAGE="Javascript">
26 function OnLoadEvent() { document.form.submit(); }
27 </SCRIPT>
28 <body OnLoad="OnLoadEvent();">
29 <cfoutput>
30 <form name="form" action="#Response.ACSURL#" method="POST"/>
31 <input type="hidden" name="PaReq" value="#Response.PaReq#"/>
32 <input type="hidden" name="TermUrl" value="https://#CGI.HTTP_HOST#/backtothispage.cfm"/>
33 <input type="hidden" name="MD" value="#Response.MD#"/>
34 </cfoutput>
35 <NOSCRIPT>
36 <center>
37 <p>Please click button below to Authenticate your card</p>
38 <input type="submit" value="Go"/>
39 </p>
40 </center>
41 </NOSCRIPT>
42 </form>
43</body>
44</cfoutput>
45 <!---The transaction returned failed!!!!!--->
46 <cfelse>
47its all over show why it failed
48</cfif>

Sagepay.cfc

view plain print about
1<!----------------------------------------------------------------
2Title: SagePay Gateway CFC
3Aurthor: Glyn Jackson Newebia Ltd www.newebia.co.uk/blog
4Usage: Free to anyone all I ask is if you make changes share them
5Notes: To be used with VSP Direct from Sagepay only!
6Version: v0.1
7Updated: 27/05/09 by no one!
8----------------------------------------------------------------->

9<cfcomponent>
10<!---Config setting.
11I use these setting to change the payment gateway from live or test this is only a
12guide however you could set these variables in your application.cfc or integrate them
13with your own live or production modes--->

14<cfscript>
15SimulatorSite ="0"; //use simulator URLS to emulate the systems see: www.sagepay.com/developers/integration_manual/integrating_simulator.asp
16
TestSite ="1"; // use testing URLS
17
LiveSite ="0"; // use live URLS
18
</cfscript>
19<!--- gateway setup. URL's to be used depending on mode set above--->
20<cffunction name="SetGateway" access="public" returntype="struct">
21 <!--- Set up local scope. --->
22 <cfset GatewaySettings = StructNew() />
23 <cfif #SimulatorSite# is "1" >
24 <cfscript>
25 StructInsert(GatewaySettings, "Verify", "false");
26 StructInsert(GatewaySettings, "PurchaseURL", "https://test.sagepay.com/Simulator/VSPDirectGateway.asp");
27
StructInsert(GatewaySettings, "
RefundURL", "https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorRefundTx");
28
StructInsert(GatewaySettings, "ReleaseURL", "https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorReleaseTx");
29
StructInsert(GatewaySettings, "
RepeatURL", "https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorRepeatTx");
30
StructInsert(GatewaySettings, "callbackURL", "https://test.sagepay.com/Simulator/VSPDirectCallback.asp");
31
</cfscript>
32 </cfif>
33 <cfif #TestSite# is "
1">
34 <cfscript>

35 StructInsert(GatewaySettings, "
Verify", "false");
36 StructInsert(GatewaySettings, "
PurchaseURL", "https://test.sagepay.com/gateway/service/vspdirect-register.vsp");
37
StructInsert(GatewaySettings, "RefundURL", "https://test.sagepay.com/gateway/service/refund.vsp");
38
StructInsert(GatewaySettings, "
ReleaseURL", "https://test.sagepay.com/gateway/service/release.vsp");
39
StructInsert(GatewaySettings, "RepeatURL", "https://test.sagepay.com/gateway/service/repeat.vsp");
40
StructInsert(GatewaySettings, "
callbackURL", "https://test.sagepay.com/gateway/service/direct3dcallback.vsp");
41
</cfscript>
42 </cfif>
43 <cfif #LiveSite# is "1">
44 <cfscript>
45 StructInsert(GatewaySettings, "Verify", "false");
46 StructInsert(GatewaySettings, "PurchaseURL", "https://live.sagepay.com/gateway/service/vspdirect-register.vsp");
47
StructInsert(GatewaySettings, "
RefundURL", "https://live.sagepay.com/gateway/service/refund.vsp");
48
StructInsert(GatewaySettings, "ReleaseURL", "https://live.sagepay.com/gateway/service/release.vsp");
49
StructInsert(GatewaySettings, "
RepeatURL", "https://live.sagepay.com/gateway/service/repeat.vsp");
50
StructInsert(GatewaySettings, "callbackURL", "https://live.sagepay.com/gateway/service/direct3dcallback.vsp");
51
</cfscript>
52 </cfif>
53 <cfreturn GatewaySettings>
54</cffunction>
55
56<!---
57'****************************************************************************************
58' Protx HTTP Call
59'****************************************************************************************
60--->

61<cffunction name="
gatewaySend" access="public" hint="checkes form dedtails" returntype="struct">
62<cfargument name="
PurchaseURL" type="string" required="yes">
63<cfargument name="
VendorTxCode" type="string" required="yes">
64<cfargument name="
DefaultCurrency" type="string" default="GBP" required="no">
65<cfargument name="
Amount" type="any" required="yes">
66<cfargument name="
CardHolder" type="string" required="yes">
67<cfargument name="
CardNumber" type="string" required="yes">
68<cfargument name="
DefaultApplyAVSCV2" type="string" required="no" default="0">
69<cfargument name="
Basket" type="string" required="no" default="">
70<cfargument name="
StartDate" type="string" required="no" default="">
71<cfargument name="
ExpiryDate" type="string" required="yes">
72<cfargument name="
DeliveryAddress" type="string" required="yes" default="">
73<cfargument name="
CardType" type="string" required="yes">
74<cfargument name="
BillingPostCode" type="string" required="yes">
75<cfargument name="
DeliveryPostCode" type="string" required="yes">
76<cfargument name="
CustomerName" type="string" required="yes">
77<cfargument name="
ContactNumber" type="string" required="no" default="">
78<cfargument name="
ContactFax" type="string" required="no" default="">
79<cfargument name="
CustomerEmail" type="string" required="no" default="">
80<cfargument name="
ClientIPAddress" type="string" required="no" default="#CGI.REMOTE_ADDR#">
81<cfargument name="
CAVV" type="string" required="no" default="">
82<cfargument name="
XID" type="string" required="no" default="">
83<cfargument name="
ECI" type="string" required="no" default="">
84<cfargument name="
DSecureStatus" type="string" required="no" default="">
85<cfargument name="
CV2" type="string" required="no">
86<cfargument name="
referrerID" type="string" required="no" default="newebialimited">
87<cfargument name="
DefaultDescription" type="string" required="no" default="Payment From #CustomerName#">
88<cfargument name="
billfirstName" type="string" required="yes">
89<cfargument name="
billlastName" type="string" required="yes">
90<cfargument name="
BillingAddress1" type="string" required="yes">
91<cfargument name="
billingcity" type="string" required="yes">
92<cfargument name="
BillingCountry" type="string" required="no" default="GB">
93<cfargument name="
ISSUENUMBER" type="string" required="no" default="">
94
95
96 <!---Get the contents of the post from the previous page and split out the variables for sending--->
97 <cfset RequestData = GetHttpRequestData()>

98 <cfset Response = StructNew()>

99 <cfloop list="
#RequestData.content#" index="line" delimiters="&">
100 <cfset line = Trim( line )>

101 <cfset StructInsert( Response, Trim( ListFirst( line, "=" ) ), URLDecode(Trim(Mid(line,Find("=",line)+1,len(line)) )) )>
102 </cfloop>
103
104<!---Set the required outgoing properties for the initial HTTPS post to the VPS--->
105 <!---******************HERE IS WHERE THE ORDER GETS SENT TO PROTX VIA HTTPS*********************** --->
106 <cfhttp url="#PurchaseURL#" method="post" delimiter="," throwonerror="no">
107 <!---to combat IIS's compression scheme incompatible with CFHTTP this issue was fixed in MX7 but is back in CF8--->
108 <cfhttpparam type="
Header" name="Accept-Encoding" value="deflate;q=0">
109 <cfhttpparam type="
Header" name="TE" value="deflate;q=0">
110 <!---end--->
111 <cfhttpparam name="
TxType" value="Payment" type="formfield">
112 <cfhttpparam name="
Vendor" value="#application.Vendor#" type="formfield">
113 <cfhttpparam name="
VendorTxCode" value="#arguments.VendorTxCode#" type="formfield">
114 <cfhttpparam name="
referrerID" value="#arguments.referrerID#" type="formfield">
115 <cfhttpparam name="
Currency" value="#arguments.DefaultCurrency#" type="formfield">
116 <cfhttpparam name="
Description" value="#arguments.DefaultDescription#" type="formfield">
117 <cfhttpparam name="
Amount" value="#arguments.Amount#" type="formfield">
118 <cfhttpparam name="
CardHolder" value="#arguments.CardHolder#" type="formfield">
119 <cfhttpparam name="
CardNumber" value="#arguments.CardNumber#" type="formfield">
120 <cfhttpparam name="
GiftAidPayment" value="0" type="formfield">
121 <cfhttpparam name="
ApplyAVSCV2" value="#arguments.DefaultApplyAVSCV2#" type="formfield">
122 <cfhttpparam name="
BillingSurname" value="#arguments.billlastName#" type="formfield">
123 <cfhttpparam name="
BillingFirstnames" value="#arguments.billfirstName#" type="formfield">
124 <cfhttpparam name="
BillingCity" value="#arguments.billingcity#" type="formfield">
125 <cfhttpparam name="
BillingCountry" value="#arguments.BillingCountry#" type="formfield">
126 <cfhttpparam name="
DeliverySurname" value="#arguments.billlastName#" type="formfield">
127 <cfhttpparam name="
DeliveryFirstnames" value="#arguments.billfirstName#" type="formfield">
128 <cfhttpparam name="
DeliveryAddress1" value="#arguments.BillingAddress1#" type="formfield">
129 <cfhttpparam name="
DeliveryCity" value="#arguments.BillingAddress1#" type="formfield">
130 <cfhttpparam name="
DeliveryCountry" value="#arguments.BillingCountry#" type="formfield">
131 <cfhttpparam name="
Basket" value="#arguments.Basket#" type="formfield">
132    <cfif #arguments.StartDate# is not "
">
133 <cfhttpparam name="
StartDate" value="#arguments.StartDate#" type="formfield">
134 </cfif>
135 <cfif #arguments.ExpiryDate# is not "
">
136 <cfhttpparam name="
ExpiryDate" value="#arguments.ExpiryDate#" type="formfield">
137 </cfif>
138 <cfif #arguments.DeliveryAddress# is not "
">
139 <cfhttpparam name="
DeliveryAddress" value="#arguments.DeliveryAddress#" type="formfield">
140 </cfif>
141 <cfhttpparam name="
BillingAddress1" value="#arguments.BillingAddress1#" type="formfield">
142 <cfif #arguments.IssueNumber# is not "
">
143 <cfhttpparam name="
IssueNumber" value="#arguments.IssueNumber#" type="formfield">
144 </cfif>
145 <cfhttpparam name="
CV2" value="#arguments.CV2#" type="formfield">
146 <cfhttpparam name="
CardType" value="#arguments.CardType#" type="formfield">
147 <cfhttpparam name="
BillingPostCode" value="#arguments.BillingPostCode#" type="formfield">
148 <cfif #arguments.DeliveryPostCode# is not "
">
149 <cfhttpparam name="
DeliveryPostCode" value="#arguments.DeliveryPostCode#" type="formfield">
150 </cfif>
151 <cfhttpparam name="
CustomerName" value="#arguments.CustomerName#" type="formfield">
152    <cfif #arguments.ContactNumber# is not "
">
153 <cfhttpparam name="
ContactNumber" value="#arguments.ContactNumber#" type="formfield">
154 </cfif>
155 <cfif #arguments.ContactFax# is not "
">
156 <cfhttpparam name="
ContactFax" value="#arguments.ContactFax#" type="formfield">
157 </cfif>
158 <cfhttpparam name="
CustomerEmail" value="#arguments.CustomerEmail#" type="formfield">
159 <cfif #arguments.ClientIPAddress# is not "
">
160 <cfhttpparam name="
ClientIPAddress" value="#arguments.ClientIPAddress#" type="formfield">
161 </cfif>
162 <cfif #arguments.CAVV# is not "
">
163 <cfhttpparam name="
CAVV" value="#arguments.CAVV#" type="formfield">
164 </cfif>
165 <cfif #arguments.XID# is not "
">
166 <cfhttpparam name="
XID" value="#arguments.XID#" type="formfield">
167 </cfif>
168 <cfif #arguments.ECI# is not "
">
169 <cfhttpparam name="
ECI" value="#arguments.ECI#" type="formfield">
170 </cfif>
171 <cfif #arguments.DSecureStatus# is not "
">
172 <cfhttpparam name="
3DSecureStatus" value="#arguments.arguments.DSecureStatus#" type="formfield">
173 </cfif>
174 </cfhttp>
175 <!--- ********************************END OF HTTPS POST TO PROTX******************************************--->
176 <cfset Response = StructNew()>

177 <!---if http post was ok--->
178 <cfif #cfhttp.statusCode# is "200 OK">
179 <cfloop list="
#CFHTTP.FileContent#" index="line" delimiters="#chr(13)#">
180 <cfset line = Trim( line )>

181 <cfset StructInsert( Response, Trim( ListFirst( line, "=" ) ), Trim(Mid(line,Find("=",line)+1,len(line)) ) )>
182 </cfloop>
183 <!---if could not contact gateway--->
184 <cfelse>
185 <cfset StructInsert(Response, "Status", "timeout")>
186 <cfset StructInsert(Response, "StatusDetail", "Timeout Error: could not contact payment gateway or header code was not 200, please contact website owner.")>
187 </cfif>
188 <!---retrun responce--->
189 <cfreturn Response>
190</cffunction>
191</cfcomponent>
TweetBacks
Comments
 

About Me

Glyn Jackson, 28 years old, MD and senior developer of a development firm based in Staffordshire called Newebia Ltd. Academic background in BSc Information System & Internet Commerce. Online marketing expert (EE Ranked) and .NET developer. Has been developing with ColdFusion for 5 years and loves it. "I am not a veteran in ColdFusion but I do work on challenging projects which help me learn more about ColdFusion and if I can contribute to the community in anyway then, it's all good!"

Recommends

  • ColdFusion