Not yet rated

Problem

I want to integrate SagePay VSP Direct payment system into a website using ColdFusion. SagePay no longer has examples of how to integrate ColdFusion sites with their VSP Direct Payment Gateway. SagePay are one of the UK's biggest gateway providers and with no kit available on their site please help.

Solution

SagePay VSP Direct is a method of processing a transaction through the Sage Pay gateway using server-to-server communication with the customer remaining on your website throughout the whole process.

Detailed explanation

This example already assumes you have created the payment details page with all the necessary form fields and have a understanding of SagePay. Please read their integration guides and you will need have a test or simulation account to test first.

Before calling the CFC example at the end of this article 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.  I sometimes do this by using something like...

TxCode = "#randrange(0,999999999)##randrange(0,999999999)#"

In your calling page first lets call the CFC

cfcSagepay = createobject("component", "sagepay");
gatewaysetup = cfcSagepay.SetGateway();

Change the path to where you have placed your CFC.

On the calling page we need to get the response  from the gateway and pass in all the required parameters   something like..

Response = cfcSagepay.gatewaySend()

You will also need to collect the responses from the 'response' strut above.

<cfif isDefined('PARes')>
<cfhttp url="#gatewaysetup.callbackURL#" method="post"
delimiter="," resolveurl="no" throwonerror="yes" timeout="120"
charset="windows-1252">
<cfhttpparam name="MD" value="#FORM.MD#" type="formfield">
<cfhttpparam name="PARes" value="#FORM.PARes#"
type="formfield">
</cfhttp>
<cfset Response = StructNew()>
<cfloop list="#CFHTTP.FileContent#" index="line"
delimiters="#chr(13)#">
<cfset line = Trim( line )>
<cfset StructInsert( Response, Trim( ListFirst( line, "=" ) ),
Trim(Mid(line,Find("=",line)+1,len(line)) ) )>
</cfloop>
</cfif>

 You could handle the response like this...
 

<cfif #Response.Status# IS "OK" OR
#Response.Status# IS "ATTEMPTONLY" >

all done everything is ok update your database and take them to
thank you page

<cfelseif #Response.Status# IS "3DAUTH">

<cfoutput>

<h1>3D Secure Verification Needed, Loading please
wait...</h1>

<InvalidTag LANGUAGE="Javascript">

function OnLoadEvent() { document.form.submit(); }

</SCRIPT>

<body OnLoad="OnLoadEvent();">

<cfoutput>

<form name="form" action="#Response.ACSURL#" method="POST"/>

<input type="hidden" name="PaReq" value="#Response.PaReq#"/>

<input type="hidden" name="TermUrl"
value="https://#CGI.HTTP_HOST#/backtothispage.cfm"/>

<input type="hidden" name="MD" value="#Response.MD#"/>

</cfoutput>

<NOSCRIPT>

<center>

<p>Please click button below to Authenticate your
card</p>

<input type="submit" value="Go"/>

</p>

</center>

</NOSCRIPT>

</form>

</body>

</cfoutput>

<!---The transaction returned failed!!!!!--->

<cfelse>



</cfif>

Sage Pay CFC

<cfcomponent>

<!---Config setting.

I use these setting to change the payment gateway from live or test
this is only a

guide however you could set these variables in your application.cfc
or integrate them

with your own live or production modes--->

<cfscript>

SimulatorSite ="0"; //use simulator URLS to emulate the systems
see:
www.sagepay.com/developers/integration_manual/integrating_simulator.asp

TestSite ="1"; // use testing URLS

LiveSite ="0"; // use live URLS

</cfscript>

<!--- gateway setup. URL's to be used depending on mode set
above--->

<cffunction name="SetGateway" access="public"
returntype="struct">

<!--- Set up local scope. --->

<cfset GatewaySettings = StructNew() />

<cfif #SimulatorSite# is "1" >

<cfscript>

StructInsert(GatewaySettings, "Verify", "false");

StructInsert(GatewaySettings, "PurchaseURL",
"https://test.sagepay.com/Simulator/VSPDirectGateway.asp");

StructInsert(GatewaySettings, "RefundURL",
"https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorRefundTx");

StructInsert(GatewaySettings, "ReleaseURL",
"https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorReleaseTx");

StructInsert(GatewaySettings, "RepeatURL",
"https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorRepeatTx");

StructInsert(GatewaySettings, "callbackURL",
"https://test.sagepay.com/Simulator/VSPDirectCallback.asp");

</cfscript>

</cfif>

<cfif #TestSite# is "1">

<cfscript>

StructInsert(GatewaySettings, "Verify", "false");

StructInsert(GatewaySettings, "PurchaseURL",
"https://test.sagepay.com/gateway/service/vspdirect-register.vsp");

StructInsert(GatewaySettings, "RefundURL",
"https://test.sagepay.com/gateway/service/refund.vsp");

StructInsert(GatewaySettings, "ReleaseURL",
"https://test.sagepay.com/gateway/service/release.vsp");

StructInsert(GatewaySettings, "RepeatURL",
"https://test.sagepay.com/gateway/service/repeat.vsp");

StructInsert(GatewaySettings, "callbackURL",
"https://test.sagepay.com/gateway/service/direct3dcallback.vsp");

</cfscript>

</cfif>

<cfif #LiveSite# is "1">

<cfscript>

StructInsert(GatewaySettings, "Verify", "false");

StructInsert(GatewaySettings, "PurchaseURL",
"https://live.sagepay.com/gateway/service/vspdirect-register.vsp");

StructInsert(GatewaySettings, "RefundURL",
"https://live.sagepay.com/gateway/service/refund.vsp");

StructInsert(GatewaySettings, "ReleaseURL",
"https://live.sagepay.com/gateway/service/release.vsp");

StructInsert(GatewaySettings, "RepeatURL",
"https://live.sagepay.com/gateway/service/repeat.vsp");

StructInsert(GatewaySettings, "callbackURL",
"https://live.sagepay.com/gateway/service/direct3dcallback.vsp");

</cfscript>

</cfif>

<cfreturn GatewaySettings>

</cffunction>



<!---

'****************************************************************************************

' Protx HTTP Call

'****************************************************************************************

--->

<cffunction name="gatewaySend" access="public" hint="checkes
form dedtails" returntype="struct">

<cfargument name="PurchaseURL" type="string" required="yes">

<cfargument name="VendorTxCode" type="string" required="yes">

<cfargument name="DefaultCurrency" type="string" default="GBP"
required="no">

<cfargument name="Amount" type="any" required="yes">

<cfargument name="CardHolder" type="string" required="yes">

<cfargument name="CardNumber" type="string" required="yes">

<cfargument name="DefaultApplyAVSCV2" type="string"
required="no" default="0">

<cfargument name="Basket" type="string" required="no"
default="">

<cfargument name="StartDate" type="string" required="no"
default="">

<cfargument name="ExpiryDate" type="string" required="yes">

<cfargument name="DeliveryAddress" type="string" required="yes"
default="">

<cfargument name="CardType" type="string" required="yes">

<cfargument name="BillingPostCode" type="string"
required="yes">

<cfargument name="DeliveryPostCode" type="string"
required="yes">

<cfargument name="CustomerName" type="string" required="yes">

<cfargument name="ContactNumber" type="string" required="no"
default="">

<cfargument name="ContactFax" type="string" required="no"
default="">

<cfargument name="CustomerEmail" type="string" required="no"
default="">

<cfargument name="ClientIPAddress" type="string" required="no"
default="#CGI.REMOTE_ADDR#">

<cfargument name="CAVV" type="string" required="no"
default="">

<cfargument name="XID" type="string" required="no"
default="">

<cfargument name="ECI" type="string" required="no"
default="">

<cfargument name="DSecureStatus" type="string" required="no"
default="">

<cfargument name="CV2" type="string" required="no">

<cfargument name="referrerID" type="string" required="no"
default="newebialimited">

<cfargument name="DefaultDescription" type="string"
required="no" default="Payment From #CustomerName#">

<cfargument name="billfirstName" type="string"
required="yes">

<cfargument name="billlastName" type="string" required="yes">

<cfargument name="BillingAddress1" type="string"
required="yes">

<cfargument name="billingcity" type="string" required="yes">

<cfargument name="BillingCountry" type="string" required="no"
default="GB">

<cfargument name="ISSUENUMBER" type="string" required="no"
default="">





<!---Get the contents of the post from the previous page and
split out the variables for sending--->

<cfset RequestData = GetHttpRequestData()>

<cfset Response = StructNew()>

<cfloop list="#RequestData.content#" index="line"
delimiters="&">

<cfset line = Trim( line )>

<cfset StructInsert( Response, Trim( ListFirst( line, "=" ) ),
URLDecode(Trim(Mid(line,Find("=",line)+1,len(line)) )) )>

</cfloop>



<!---Set the required outgoing properties for the initial HTTPS
post to the VPS--->

<!---******************HERE IS WHERE THE ORDER GETS SENT TO
VIA HTTPS*********************** --->

<cfhttp url="#PurchaseURL#" method="post" delimiter=","
throwonerror="no">

<!---to combat IIS's compression scheme incompatible with CFHTTP
this issue was fixed in MX7 but is back in CF8--->

<cfhttpparam type="Header" name="Accept-Encoding"
value="deflate;q=0">

<cfhttpparam type="Header" name="TE" value="deflate;q=0">

<!---end--->

<cfhttpparam name="TxType" value="Payment" type="formfield">

<cfhttpparam name="Vendor" value="#application.Vendor#"
type="formfield">

<cfhttpparam name="VendorTxCode"
value="#arguments.VendorTxCode#" type="formfield">

<cfhttpparam name="referrerID" value="#arguments.referrerID#"
type="formfield">

<cfhttpparam name="Currency" value="#arguments.DefaultCurrency#"
type="formfield">

<cfhttpparam name="Description"
value="#arguments.DefaultDescription#" type="formfield">

<cfhttpparam name="Amount" value="#arguments.Amount#"
type="formfield">

<cfhttpparam name="CardHolder" value="#arguments.CardHolder#"
type="formfield">

<cfhttpparam name="CardNumber" value="#arguments.CardNumber#"
type="formfield">

<cfhttpparam name="GiftAidPayment" value="0"
type="formfield">

<cfhttpparam name="ApplyAVSCV2"
value="#arguments.DefaultApplyAVSCV2#" type="formfield">

<cfhttpparam name="BillingSurname"
value="#arguments.billlastName#" type="formfield">

<cfhttpparam name="BillingFirstnames"
value="#arguments.billfirstName#" type="formfield">

<cfhttpparam name="BillingCity" value="#arguments.billingcity#"
type="formfield">

<cfhttpparam name="BillingCountry"
value="#arguments.BillingCountry#" type="formfield">

<cfhttpparam name="DeliverySurname"
value="#arguments.billlastName#" type="formfield">

<cfhttpparam name="DeliveryFirstnames"
value="#arguments.billfirstName#" type="formfield">

<cfhttpparam name="DeliveryAddress1"
value="#arguments.BillingAddress1#" type="formfield">

<cfhttpparam name="DeliveryCity"
value="#arguments.BillingAddress1#" type="formfield">

<cfhttpparam name="DeliveryCountry"
value="#arguments.BillingCountry#" type="formfield">

<cfhttpparam name="Basket" value="#arguments.Basket#"
type="formfield">

    <cfif #arguments.StartDate# is not "">

<cfhttpparam name="StartDate" value="#arguments.StartDate#"
type="formfield">

</cfif>

<cfif #arguments.ExpiryDate# is not "">

<cfhttpparam name="ExpiryDate" value="#arguments.ExpiryDate#"
type="formfield">

</cfif>

<cfif #arguments.DeliveryAddress# is not "">

<cfhttpparam name="DeliveryAddress"
value="#arguments.DeliveryAddress#" type="formfield">

</cfif>

<cfhttpparam name="BillingAddress1"
value="#arguments.BillingAddress1#" type="formfield">

<cfif #arguments.IssueNumber# is not "">

<cfhttpparam name="IssueNumber" value="#arguments.IssueNumber#"
type="formfield">

</cfif>

<cfhttpparam name="CV2" value="#arguments.CV2#"
type="formfield">

<cfhttpparam name="CardType" value="#arguments.CardType#"
type="formfield">

<cfhttpparam name="BillingPostCode"
value="#arguments.BillingPostCode#" type="formfield">

<cfif #arguments.DeliveryPostCode# is not "">

<cfhttpparam name="DeliveryPostCode"
value="#arguments.DeliveryPostCode#" type="formfield">

</cfif>

<cfhttpparam name="CustomerName"
value="#arguments.CustomerName#" type="formfield">

    <cfif #arguments.ContactNumber# is not "">

<cfhttpparam name="ContactNumber"
value="#arguments.ContactNumber#" type="formfield">

</cfif>

<cfif #arguments.ContactFax# is not "">

<cfhttpparam name="ContactFax" value="#arguments.ContactFax#"
type="formfield">

</cfif>

<cfhttpparam name="CustomerEmail"
value="#arguments.CustomerEmail#" type="formfield">

<cfif #arguments.ClientIPAddress# is not "">

<cfhttpparam name="ClientIPAddress"
value="#arguments.ClientIPAddress#" type="formfield">

</cfif>

<cfif #arguments.CAVV# is not "">

<cfhttpparam name="CAVV" value="#arguments.CAVV#"
type="formfield">

</cfif>

<cfif #arguments.XID# is not "">

<cfhttpparam name="XID" value="#arguments.XID#"
type="formfield">

</cfif>

<cfif #arguments.ECI# is not "">

<cfhttpparam name="ECI" value="#arguments.ECI#"
type="formfield">

</cfif>

<cfif #arguments.DSecureStatus# is not "">

<cfhttpparam name="3DSecureStatus"
value="#arguments.arguments.DSecureStatus#" type="formfield">

</cfif>

</cfhttp>

<!--- ********************************END OF HTTPS POST TO
PROTX******************************************--->

<cfset Response = StructNew()>

<!---if http post was ok--->

<cfif #cfhttp.statusCode# is "200 OK">

<cfloop list="#CFHTTP.FileContent#" index="line"
delimiters="#chr(13)#">

<cfset line = Trim( line )>

<cfset StructInsert( Response, Trim( ListFirst( line, "=" ) ),
Trim(Mid(line,Find("=",line)+1,len(line)) ) )>

</cfloop>

<!---if could not contact gateway--->

<cfelse>

<cfset StructInsert(Response, "Status", "timeout")>

<cfset StructInsert(Response, "StatusDetail", "Timeout Error:
could not contact payment gateway or header code was not 200,
please contact website owner.")>

</cfif>

<!---retrun responce--->

<cfreturn Response>

</cffunction>

</cfcomponent>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes