00001 #ifndef OC_PAYFLOW_PRO_H
00002 #define OC_PAYFLOW_PRO_H
00003 #include <string>
00004 #include <vector>
00005 #include <cstdio>
00006 #include "pfpro.h"
00007
00008 #include "ocString.h"
00009
00010
00011
00012
00013
00014
00015 class ocPfParam
00016 {
00017 string key;
00018 string value;
00019 string strParam;
00020 public:
00021
00022 ocPfParam(const string & inKey, const string & inValue):key(inKey),value(inValue)
00023 {
00024 string::size_type ampIn = value.find("&");
00025 string::size_type eqIn = value.find("=");
00026
00027 if( ampIn != string::npos || eqIn != string::npos )
00028 {
00029 char temp[64];
00030 sprintf( temp,"%d", value.length() );
00031 key += "[";
00032 key += temp;
00033 key += "]";
00034 }
00035 }
00036 virtual ~ocPfParam()
00037 {;}
00038 ocPfParam(const ocPfParam & in):key(in.key),value(in.value)
00039 {;}
00040 const ocPfParam operator = ( const ocPfParam in )
00041 {
00042 key = in.key;
00043 value = in.value;
00044 return * this;
00045 }
00046 const char * toString( bool endTag )
00047 {
00048 strParam = key;
00049 strParam += "=";
00050 strParam += value;
00051 if( endTag ) strParam += "&";
00052 return strParam.c_str();
00053 }
00054 };
00055
00056
00057
00058 typedef class vector< ocPfParam > ocPfParams;
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 class ocPayflowPro
00069 {
00070 ocPfParams params;
00071 string hostAddress;
00072 int portNum;
00073 string response;
00074 string proxyAddress;
00075 int proxyPort;
00076 string proxyLogon;
00077 string proxyPassword;
00078 long timeout;
00079 int rc;
00080 int context;
00081 char * pTranResponse;
00082 bool isOk;
00083 string strError;
00084 string strResult;
00085 string strPnRef;
00086 string strResponseMessage;
00087 string strAuthCode;
00088 string strParmList;
00089
00090
00091 bool parseTranResponse( void )
00092 {
00093 bool bRet = false;
00094 strResult = ""; strPnRef = ""; strResponseMessage = "";
00095 ocString res = pTranResponse;
00096 for( string element=res.parse("&");
00097 !(element.length()==0 && res.endOfParse() );
00098 element=res.parse("&") )
00099 {
00100 ocString apair(element);
00101 string name = apair.parse("=");
00102 string value = apair.parse("=");
00103 transform(name.begin(),name.end(),name.begin(),(int(*)(int))toupper );
00104 if( name == "RESULT" )
00105 {
00106 strResult = value;
00107 }
00108 else if ( name == "PNREF" )
00109 {
00110 strPnRef = value;
00111 }
00112 else if( name == "RESPMSG" )
00113 {
00114 strResponseMessage = value;
00115 }
00116 else if( name == "AUTHCODE" )
00117 {
00118 strAuthCode = value;
00119 }
00120 }
00121 if( strResult == "0" )
00122 {
00123 bRet = true;
00124 }
00125 return bRet;
00126 }
00127
00128 public:
00129
00130
00131 ~ocPayflowPro( )
00132 {
00133 completeTransaction();
00134 if (context) pfproDestroyContext(context);
00135 }
00136
00137 ocPayflowPro ( const char * certPath, const char * host,
00138 int inPort, long inTimeout,
00139 const char * inProxyAddr = NULL,
00140 int inProxyPort = 0,
00141 const char * inProxyLogon = NULL,
00142 const char * inProxyPassword = NULL )
00143 :portNum(0),proxyPort(0),timeout(0),rc(0),context(0),pTranResponse(NULL),isOk(false)
00144 {
00145
00146 if( setenv( "PFPRO_CERT_PATH", certPath, 1 ) == 0 )
00147 {
00148
00149 hostAddress = host;
00150 portNum = inPort;
00151 timeout = inTimeout;
00152 if( inProxyAddr )
00153 {
00154 proxyAddress = inProxyAddr;
00155 }
00156 if( inProxyPort )
00157 {
00158 proxyPort = inProxyPort;
00159 }
00160 if( inProxyLogon )
00161 {
00162 proxyLogon = inProxyLogon;
00163 }
00164
00165 strError = "Failed to initialize";
00166 rc = pfproInit();
00167 if( rc == 0 )
00168 {
00169 strError = "Failed to create a context.";
00170 rc = pfproCreateContext( &context,
00171 const_cast<char *>(hostAddress.c_str()),
00172 portNum, timeout,
00173 const_cast<char *>(proxyAddress.c_str()),
00174 proxyPort,
00175 const_cast<char *>(proxyLogon.c_str()),
00176 const_cast<char *>(proxyPassword.c_str()));
00177 if( rc == 0 )
00178 {
00179 isOk = true;
00180 strError = "OK";
00181 }
00182 else
00183 {
00184 strError = "pfproCreateContext -- Failed to create context.";
00185 isOk=false;
00186 }
00187 }
00188 }
00189 else
00190 {
00191
00192 strError = "Failed to set certificate path env. variable.";
00193 isOk=false;
00194 }
00195 }
00196
00197
00198 void addParam ( string inKey, string inValue )
00199 {
00200 ocPfParam temp( inKey, inValue );
00201 params.push_back( temp );
00202 }
00203
00204
00205 bool submitTransaction( void )
00206 {
00207 bool bRet = false;
00208 strParmList = "";
00209 for( int i=0; i<params.size(); i++)
00210 {
00211 ocPfParam & parm = params[i];
00212 strParmList += parm.toString( (i+1) < params.size() );
00213 }
00214
00215 strError = "pfproSubmitTransaction call Failed.";
00216 rc = pfproSubmitTransaction( context,
00217 const_cast<char *>(strParmList.c_str()),
00218 strParmList.length(),
00219 &pTranResponse);
00220 if( rc == 0 )
00221 {
00222 bRet = parseTranResponse();
00223 if( bRet )
00224 {
00225 response = pTranResponse;
00226 strError = "Transaction Submitted Successfully.";
00227 }
00228 else
00229 {
00230 strError = "Transaction ";
00231 strError += getPnRef();
00232 strError += " Rejected. Code: ";
00233 strError += getResultCode();
00234 strError += " Reason: ";
00235 strError += getResponseMessage();
00236 }
00237 }
00238 else
00239 {
00240 strError += " rc=";
00241 ocAppend( strError, rc );
00242 }
00243 return bRet;
00244 }
00245
00246
00247 void completeTransaction( void )
00248 {
00249 if( pTranResponse )
00250 {
00251 pfproCompleteTransaction(pTranResponse);
00252 pTranResponse = NULL;
00253 }
00254 }
00255
00256 string getVersion( void )
00257 {
00258 return pfproVersion();
00259 }
00260
00261
00262
00263
00264
00265
00266 int getRc( void )
00267 {
00268 return rc;
00269 }
00270
00271 string & getError( void )
00272 {
00273 return strError;
00274 }
00275
00276 string & getUnparsedResponse( void )
00277 {
00278 return response;
00279 }
00280
00281 string & getResultCode( void )
00282 {
00283 return strResult;
00284 }
00285
00286 string & getPnRef( void )
00287 {
00288 return strPnRef;
00289 }
00290
00291 string & getResponseMessage( void)
00292 {
00293 return strResponseMessage;
00294 }
00295
00296 string & getAuthorizationCode(void)
00297 {
00298 return strAuthCode;
00299 }
00300 string & getParameters(void)
00301 {
00302 return strParmList;
00303 }
00304 };
00305
00306 #endif
00307
00308
00309