SecurePayTech.com logo


<!---
*****************************************************************
**     SECUREPAYLINE CLASS
*****************************************************************

Copyright (c) 2005 Cabbage Tree Creative Ltd.
http://www.cabbagetree.co.nz/
This software is freely re-distributable, as long as the above copyright notice remains intact

This CFC provides an interface with the SecurePayTech secure payment system using SOAP.
Currently it is able to perform purchases and refunds.

Summary of methods:
     Payment methods
          purchase( merchantID, merchantKey, orderReference, amount, currency, cardType, cardNumber, cardExpiry, cardHolderName )
          refund( merchantID, merchantKey, amount, originalTxNumber )
     Data methods
          getCardTypes()
          getStatusCodes()
     Private methods
          SOAPResultToStruct()
--->

<cfcomponent
     displayname="SecurePayTech"
     hint="SecurePayTech SOAP Interface Wrapper CFC"
>
     <cffunction
          name="purchase"
          access="public"
          hint="Makes a purchase using the SecurePayTech system. Returns a structure with the keys resultCode, resultMessage, failReason, merchTxnRef, receiptNo, transactionNo, authorizationID, batchNo. Throws InvalidDateFormat, InvalidCardType."
          output="No"
          returntype="struct"
     >
          <cfargument name="merchantID" required="Yes" type="string" displayname="Unique merchant identifier">
          <cfargument name="merchantKey" required="Yes" type="string" displayname="40 character merchant key">
          <cfargument name="orderReference" required="Yes" type="string" displayname="Order reference identifier">
          <cfargument name="amount" required="Yes" type="numeric" displayname="Purchase amount">
          <cfargument name="currency" required="No" type="string" default="NZD" displayname="Purchase amount currency">
          <cfargument name="cardType" required="Yes" type="numeric" displayname="Credit card type">
          <cfargument name="cardNumber" required="Yes" type="string" displayname="Credit card number">
          <cfargument name="cardExpiry" required="Yes" type="string" displayname="Credit card expiry date, in MMYY format">
          <cfargument name="cardHolderName" required="Yes" type="string" displayname="Credit card holder name">
          <cfscript>
               var SOAPResult=0;
          </cfscript>
          <!--- Check cardExpiry is in MMYY format --->
          <cfif not (reFind("^[0-9]{4}$", arguments.cardExpiry) and (int(mid(arguments.cardExpiry, 1, 2)) gte 1 and int(mid(arguments.cardExpiry, 1, 2)) lte 12))>
               <cfthrow type="InvalidDateFormat" message="Invalid expiry date" detail="The expiry date provided is not a date in the format MMYY.">
          </cfif>
          <!--- Check card type is valid --->
          <cfif not ArrayLen(structFindValue(getCardTypes(), arguments.cardType))>
               <cfthrow type="InvalidCardType" message="Invalid card type" detail="The card type provided is not in the supported list of card types.">
          </cfif>
          <!--- Call webservice --->
          <cfinvoke
               webservice="https://tx.securepaytech.com:8443/web/SoapPurchase?wsdl"
               method="purchase"
               argumentCollection="#arguments#"
               returnvariable="SOAPResult"
          />
          <cfreturn SOAPResultToStruct(SOAPResult)>
     </cffunction>
     
     <cffunction
          name="refund"
          access="public"
          hint="Processes a refund for a purchase made using the SecurePayTech system. Returns a structure with the keys resultCode, resultMessage, failReason, merchTxnRef, receiptNo, transactionNo, authorizationID, batchNo."
          output="No"
          returntype="struct"
     >
          <cfargument name="merchantID" required="Yes" type="string" displayname="Unique merchant identifier">
          <cfargument name="merchantKey" required="Yes" type="string" displayname="40 character merchant key">
          <cfargument name="amount" required="Yes" type="numeric" displayname="Refund amount">
          <cfargument name="originalTxNumber" required="Yes" type="string" displayname="Original transaction number">
          <cfscript>
               var SOAPResult=0;
          </cfscript>
          <!--- Call webservice --->
          <cfinvoke
               webservice="https://tx.securepaytech.com:8443/web/SoapRefund?wsdl"
               method="refund"
               argumentCollection="#arguments#"
               returnvariable="SOAPResult"
          />
          <cfreturn SOAPResultToStruct(SOAPResult)>
     </cffunction>
     
     <cffunction
          name="getCardTypes"
          access="public"
          hint="Returns a structure containing the available card types."
          output="No"
          returntype="struct"
     >
          <cfscript>
               var cards=StructNew();
               cards["Visa"]=1;
               cards["Mastercard"]=2;
               cards["American Express"]=3;
               cards["Diners Club"]=4;
               return cards;
          </cfscript>
     </cffunction>
     
     <cffunction
          name="getStatusCodes"
          access="public"
          hint="Returns an array containing the meanings of the various status codes."
          output="No"
          returntype="array"
     >
          <cfscript>
               var codes=ListToArray("Transaction OK,Insufficient funds,Card Expired,Card Declined,Server Error Occurred,Communications Error,Unsupported Transaction Type,Bad or Malformed Request");
               return codes;
          </cfscript>
     </cffunction>
     
     <cffunction
          name="SOAPResultToStruct"
          access="private"
          hint="Converts a com.securepaytech.tx.TransactionResult SOAP result object into a Coldfusion structure."
          output="No"
          returntype="struct"
     >
          <cfargument name="SOAPResult" required="Yes" type="any" displayname="SOAP result object of type com.securepaytech.tx.TransactionResult">
          <cfscript>
               var fields=listToArray("resultCode,failReason,merchTxnRef,receiptNo,transactionNo,authorizationID,batchNo");
               var i=1;
               var fieldName="";
               var value="";
               var result=StructNew();
               var statusCodes=getStatusCodes();
               for(i=1; i lte arraylen(fields); i=i+1){
                    fieldName=fields[i];
                    if(isDefined("arguments.SOAPResult."&fieldName) and arguments.SOAPResult[fieldName] neq "null"){
                         value=arguments.SOAPResult[fieldName];
                         result[fieldName]=value;
                         // freindly display of status messages
                         if(fieldName eq "resultCode"){
                              if(isNumeric(value) and val(value) gte 1 and val(value) lte arraylen(statusCodes)){
                                   result["resultMessage"]=statusCodes[val(value)];
                              }else{
                                   result["resultMessage"]="Unknown";
                              }
                         }
                    }else{
                         result[fieldName]="";
                    }
               }
               return result;
          </cfscript>
     </cffunction>
</cfcomponent>