/
Transfirst.php
404 lines (357 loc) · 11.5 KB
/
Transfirst.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* Transfirst processor
*
* PHP version 5
*
* LICENSE:
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Payment
* @package Payment_Process2
* @author Ian Eure <ieure@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Payment_Process2
*/
require_once 'Payment/Process2/Common.php';
require_once 'Payment/Process2/Driver.php';
require_once 'Payment/Process2/Exception.php';
require_once 'Payment/Process2/Result/Transfirst.php';
require_once 'HTTP/Request2.php';
require_once 'Validate.php';
/**
* Payment_Process2_Transfirst
*
* This is a processor for TransFirst's merchant payment gateway, formerly known
* as DPILink. (http://www.transfirst.com/)
*
* *** WARNING ***
* This is BETA code. While I have tested it and it appears to work for me, I
* strongly recommend that you do additional testing before using it in
* production systems.
*
* @package Payment_Process2
* @author Ian Eure <ieure@php.net>
* @version @version@
*/
class Payment_Process2_Transfirst extends Payment_Process2_Common implements Payment_Process2_Driver
{
// Transfirst transaction types
// Request authorization only - no funds are transferred.
const ACTION_TRANSFIRST_AUTH = 30;
// Transfer funds from a previous authorization.
const ACTION_TRANSFIRST_SETTLE = 40;
// Authorize & transfer funds
const ACTION_TRANSFIRST_AUTHSETTLE = 32;
// Debit the indicated amount to a previously-charged card.
const ACTION_TRANSFIRST_CREDIT = 20;
// Cancel authorization
const ACTION_TRANSFIRST_VOID = 61;
const RESULT_TRANSFIRST_APPROVAL = 00;
const RESULT_TRANSFIRST_DECLINE = 05;
const RESULT_TRANSFIRST_INVALIDAMOUNT = 13;
const RESULT_TRANSFIRST_INVALIDCARDNO = 14;
const RESULT_TRANSFIRST_REENTER = 19;
var $_typeFieldMap = array(
'CreditCard' => array(),
);
/**
* Front-end -> back-end field map.
*
* This array contains the mapping from front-end fields (defined in
* the Payment_Process class) to the field names Transfirst requires.
*
* @see _prepare()
* @access private
*/
var $_fieldMap = array(
// Required
'login' => "DPIAccountNum",
'password' => "password",
'invoiceNumber' => "orderNum",
'customerId' => "customerNum",
'amount' => "transactionAmount",
'transactionSource' => "ECommerce",
// Credit Card Type
'cardNumber' => "cardAccountNum",
'expDate' => "expirationDate",
'zip' => "cardHolderZip",
// Common Type
'name' => "cardHolderName",
'address' => "cardHolderAddress",
'city' => "cardHolderCity",
'state' => "cardHolderState",
'phone' => "cardHolderPhone",
'email' => "cardHolderEmail"
);
var $_action = "transactionCode";
/** @todo Work out if this actually lives in credit card */
var $expDate = null;
/**
* Default options for this processor.
*
* @see Payment_Process::setOptions()
*/
protected $defaultOptions = array(
'authorizeUri' => "https://epaysecure.transfirst.com/eLink/authpd.asp"
);
/**
* Has the transaction been processed?
*
* @type boolean
* @access private
*/
var $_processed = false;
/**
* Constructor.
*
* @param array $options Class options to set.
* @see Payment_Process::setOptions()
* @return void
*/
function __construct($options = array(), HTTP_Request2 $request = null)
{
parent::__construct($options, $request);
$this->_driver = 'Transfirst';
$this->_makeRequired('login', 'password', 'action', 'invoiceNumber', 'customerId', 'amount', 'cardNumber', 'expDate');
}
/**
* Prepare the data.
*
* This function handles the 'testTransaction' option, which is specific to
* this processor.
*/
function _prepare()
{
if (!empty($this->_options['testTransaction'])) {
$this->_data['testTransaction'] = $this->_options['testTransaction'];
}
return parent::_prepare();
}
/**
* Process the transaction.
*
* @return mixed Payment_Process2_Result on success, PEAR_Error on failure
*/
function process()
{
// Sanity check
$this->validate();
// Prepare the data
$this->_prepare();
$req = clone $this->_request;
$req->setURL($this->_options['authorizeUri']);
$req->setMethod('POST');
$req->addPostParameter($this->prepareRequestData());
$res = $req->send();
$this->_processed = true;
$response = trim($res->getBody());
$result = Payment_Process2_Result::factory('Transfirst', $response, $this);
$result->_request = $this;
$this->_result = $result;
return $result;
}
/**
* Get (completed) transaction status.
*
* @return string Two-digit status returned from gateway.
*/
function getStatus()
{
if (!$this->_processed) {
throw new Payment_Process2_Exception('The transaction has not been processed yet.', Payment_Process2::ERROR_INCOMPLETE);
}
return $this->_result->code;
}
/**
* Get transaction sequence.
*
* 'Sequence' is what Transfirst calls their transaction ID/approval code. This
* function returns that code from a processed transaction.
*
* @return mixed Sequence ID, or PEAR_Error if the transaction hasn't been
* processed.
*/
function getSequence()
{
if (!$this->_processed) {
throw new Payment_Process2_Exception('The transaction has not been processed yet.', Payment_Process2::ERROR_INCOMPLETE);
}
return $this->_result->_sequenceNumber;
}
function prepareRequestData()
{
$data = array();
foreach ($this->_data as $var => $value) {
if (!empty($value)) {
$data[$var] = $value;
}
}
return $data;
}
/**
* Handle transaction source.
*
* @access private
*/
function _handleTransactionSource()
{
if ($this->transactionSource == Payment_Process2::SOURCE_ONLINE) {
$this->_data["ECommerce"] = 'Y';
} else {
$this->_data["ECommerce"] = 'N';
}
}
/**
* Handle card expiration date.
*
* The gateway wants the date in the format MMYY, with no other chars.
*
* @access private
*/
function _handleExpDate()
{
$specific = "expirationDate";
if (isset($this->_data[$specific])) {
$this->_data[$specific] = str_replace('/', '', $this->_data[$specific]);
} else {
$this->_data[$specific] = str_replace('/', '', $this->expDate);
}
}
/**
* Validate the merchant account login.
*
* The Transfirst docs specify that the login is exactly eight digits.
*
* @access private
* @return bool
* @throws Payment_Process2_Exception
*/
function _validateLogin()
{
$options = array(
'format' => VALIDATE_NUM,
'max_length' => 8,
'min_length' => 8
);
if (!Validate::string($this->login, $options)) {
throw new Payment_Process2_Exception("Invalid login");
}
return true;
}
/**
* Validate the merchant account password.
*
* The Transfirst docs specify that the password is a string between 6 and 10
* characters in length.
*
* @access private
* @return bool
* @throws Payment_Process2_Exception
*/
function _validatePassword()
{
$options = array(
'format' => VALIDATE_ALPHA . VALIDATE_NUM,
'min_length' => 6,
'max_length' => 10
);
if (!Validate::string($this->password, $options)) {
throw new Payment_Process2_Exception("Invalid password");
}
return true;
}
/**
* Validate the invoice number.
*
* Invoice number must be a 5-character long alphanumeric string.
*
* @return bool
* @throws Payment_Process2_Exception
*/
function _validateInvoiceNumber()
{
$options = array(
'format' => VALIDATE_NUM . VALIDATE_ALPHA,
'min_length' => 5,
'max_length' => 5
);
if (!Validate::string($this->invoiceNumber, $options)) {
throw new Payment_Process2_Exception("Invalid invoiceNumber");
}
return true;
}
/**
* Validate the customer id
*
* Customer id must be a 15-character long alphanumeric string.
*
* @return bool
* @throws Payment_Process2_Exception
*/
function _validateCustomerId()
{
$options = array(
'format' => VALIDATE_NUM . VALIDATE_ALPHA,
'min_length' => 15,
'max_length' => 15
);
if (!Validate::string($this->customerId, $options)) {
throw new Payment_Process2_Exception("Invalid customerId");
}
return true;
}
/**
* Validate the zip code.
*
* Zip is only required if AVS is enabled.
*
* @return bool
* @throws Payment_Process2_Exception
*/
function _validateZip()
{
if (strlen($this->zip) || $this->performAvs) {
return parent::_validateZip();
}
return true;
}
public function translateAction($action) {
switch ($action) {
case Payment_Process2::ACTION_NORMAL:
return Payment_Process2_Transfirst::ACTION_TRANSFIRST_AUTHSETTLE;
case Payment_Process2::ACTION_AUTHONLY:
return Payment_Process2_Transfirst::ACTION_TRANSFIRST_AUTH;
case Payment_Process2::ACTION_POSTAUTH:
return Payment_Process2_Transfirst::ACTION_TRANSFIRST_SETTLE;
}
return false;
}
}
?>