Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
dps_processor.inc
1 <?php
18 define('DPS_LOG_FILE_PREFIX', 'dps_transactions_account_');
19 
20 
32 {
33 
34 
40  private static $_URL = 'https://sec.paymentexpress.com/pxpost.aspx';
41 
42 
48  private static $_test_account = Array(
49  'username' => 'SquizLabsDev',
50  'password' => 'd25wdfs6',
51  );
56  private $_client;
57 
62  private $_password;
63 
64 
69  private $_amount;
70 
75  private $_currency;
76 
77 
82  private $_card_number;
83 
88  private $_card_name;
89 
90 
95  private $_card_expiry_date;
96 
101  private $_card_start_date;
102 
107  private $_card_issue_number;
108 
109 
114  private $_card_cv2;
115 
120  private $_street_address;
121 
122 
127  private $_postcode;
128 
133  private $_standard_policy = 0;
134 
139  private $_test_mode = TRUE;
140 
145  private $_log_file_name;
146 
147 
152  private $_response;
153 
154 
159  private $_success = FALSE;
160 
161 
162 
167  private static $_TEST_CARD_NUMBERS = Array(
168  '4111111111111119' => 'VISA',
169  '5431111111111111' => 'MasterCard',
170  '371111111111114' => 'Amex',
171  '36000000000008' => 'Diners',
172  '4999999999999202' => 'VISA(Declined)',
173  );
174 
175 
187  function __construct($client, $password, $test_mode = TRUE)
188  {
189  $this->_client = $client;
190  $this->_password = $password;
191  $this->_test_mode = $test_mode;
192  $this->_log_file_name = DPS_LOG_FILE_PREFIX.$this->_client.($test_mode? '_test' : '_live');
193 
194  }//end constructor
195 
196 
197 
198 
199 
208  public function setCardNumber($card_no)
209  {
210  $this->_card_number = $card_no;
211 
212  }//end setCardNumber()
213 
214 
223  public function setCardName($name)
224  {
225  $this->_card_name = $name;
226 
227  }//end setCardNumber()
228 
229 
239  public function setCardExpiryDate($month, $year)
240  {
241  $this->_card_expiry_date = $month.$year;
242  }//end setCardExpiryDate()
243 
244 
254  public function setCardStartDate($month, $year)
255  {
256  $this->_card_start_date = $month.$year;
257 
258  }//end setCardStartDate()
259 
260 
269  public function setCardIssueNumber($issue_no)
270  {
271  $this->_card_issue_number = $issue_no;
272 
273  }//end setCardIssueNumber()
274 
275 
284  public function setCardCV2($cv2)
285  {
286  $this->_card_cv2 = $cv2;
287 
288  }//end setCardCV2()
289 
290 
300  public function setStreetAddress($address)
301  {
302  $this->_street_address = $address;
303 
304  }//end setStreetAddress()
305 
306 
315  public function setPostcode($postcode)
316  {
317  $this->_postcode = $postcode;
318 
319  }//end setPostcode()
320 
321 
322 
323 
324 
325 
334  public function setAmount($amount)
335  {
336  $this->_amount = sprintf('%01.2f',$amount);
337 
338  }//end setAmount()
339 
340 
348  public function setCurrency($currency)
349  {
350  $this->_currency = $currency;
351 
352  }//end setCurrency()
353 
354 
363  public function setStandardPolicy($policy)
364  {
365  $this->_standard_policy = $policy;
366 
367  }//end setStandardPolicy()
368 
369 
378  function getResponse()
379  {
380  return $this->_response;
381 
382  }//end getResponse()
383 
384 
385 
392  public function process()
393  {
394  $this->_sendRequest();
395  return $this->_success;
396 
397  }//end process()
398 
399 
400 
407  private function _sendRequest()
408  {
409  //log the request of the transaction
410  $this->_logRequest();
411 
412  $success = TRUE;
413  $client = $this->_test_mode ? self::$_test_account['username']: $this->_client;
414  $password = $this->_test_mode ? self::$_test_account['password']: $this->_password;
415  $enableAvs = ($this->_standard_policy != 0) ? 1 : 0;
416  $cmdDoTxnTransaction = '';
417  $cmdDoTxnTransaction .= "<Txn>";
418  $cmdDoTxnTransaction .= "<PostUsername>$client</PostUsername>";
419  $cmdDoTxnTransaction .= "<PostPassword>$password</PostPassword>"; #Insert your DPS Password here
420  $cmdDoTxnTransaction .= "<Amount>$this->_amount</Amount>";
421  $cmdDoTxnTransaction .= "<InputCurrency>$this->_currency</InputCurrency>";
422  $cmdDoTxnTransaction .= "<CardHolderName>$this->_card_name</CardHolderName>";
423  $cmdDoTxnTransaction .= "<CardNumber>$this->_card_number</CardNumber>";
424  $cmdDoTxnTransaction .= "<DateExpiry>$this->_card_expiry_date</DateExpiry>";
425  $cmdDoTxnTransaction .= "<TxnType>Purchase</TxnType>";
426  $cmdDoTxnTransaction .= "<MerchantReference></MerchantReference>";
427  $cmdDoTxnTransaction .= "<Cvc2>$this->_card_cv2</Cvc2>";
428  $cmdDoTxnTransaction .= "<EnableAvsData>".$enableAvs."</EnableAvsData>";
429  $cmdDoTxnTransaction .= "<AvsAction>$this->_standard_policy</AvsAction>";
430  $cmdDoTxnTransaction .= "</Txn>";
431 
432  $options = array(
433  'POST' => 1,
434  'POSTFIELDS' => $cmdDoTxnTransaction,
435  'RETURNTRANSFER' => 1,
436  'SSL_VERIFYPEER' => 1,//Needs to be included if no *.crt is available to verify SSL certificates
437  'SSL_VERIFYHOST' => 2,
438  'SSLVERSION' => 3,
439  );
440 
441  $details = fetch_url(self::$_URL, $options);
442 
443  $this->_parse_xml($details['response']);
444 
445  //log the transaction response
446  $this->_logResponse();
447 
448  }//end _sendRequest()
449 
450 
451  function _parse_xml($data)
452  {
453  $xml_parser = xml_parser_create();
454  xml_parse_into_struct($xml_parser, $data, $vals, $index);
455  xml_parser_free($xml_parser);
456 
457  $params = array();
458  $level = array();
459  foreach ($vals as $xml_elem) {
460  if ($xml_elem['type'] == 'open') {
461  if (array_key_exists('attributes',$xml_elem)) {
462  list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
463  }
464  else {
465  $level[$xml_elem['level']] = $xml_elem['tag'];
466  }
467  }
468  if ($xml_elem['type'] == 'complete') {
469  $start_level = 1;
470  $php_stmt = '$params';
471 
472  while($start_level < $xml_elem['level']) {
473  $php_stmt .= '[$level['.$start_level.']]';
474  $start_level++;
475  }
476  $php_stmt .= '[$xml_elem[\'tag\']] = isset($xml_elem[\'value\']) ? $xml_elem[\'value\'] : \'\';';
477  eval($php_stmt);
478  }
479  }
480 
481 
482  $success = $params['TXN']['SUCCESS'];
483  $responseTxt = $params['TXN']['RESPONSETEXT'];
484  $DPSTxnRef = $params['TXN']['DPSTXNREF'];
485  $this->_success = $success == 1 ? 1 : FALSE;
486 
487  $this->_response['TRANSACTION'] = $DPSTxnRef;
488  $this->_response['TIME'] = time();
489  $this->_response['STATUS'] = $responseTxt;
490 
491  }
492 
493 
494 
501  public static function getTestCardNumbers()
502  {
503  return self::$_TEST_CARD_NUMBERS;
504 
505  }//end getTestCardNumbers()
506 
507 
514  private function _logRequest()
515  {
516  $message = "\nRequest\n";
517  $message .= 'Card holder name: '.$this->_card_name."\n";
518  $message .= 'Card number: '.substr($this->_card_number, -4)."\n";
519  $message .= 'Amount: '.$this->_amount.' '.$this->_currency."\n";
520  $this->_log($message);
521 
522  }//end _logRequest()
523 
524 
531  private function _logResponse()
532  {
533  $message = "\nResponse\n";
534  $message .= 'Transaction: '.$this->_response['TRANSACTION']."\n";
535  $message .= 'Time: '.$this->_response['TIME']."\n";
536  $message .= 'Status: '.$this->_response['STATUS']."\n";
537  $this->_log($message);
538 
539  }//end _logResponse()
540 
541 
552  private function _log($message, $level = E_USER_NOTICE, $encode=FALSE)
553  {
554  log_write($message, $this->_log_file_name, $level, $encode);
555 
556  }//end log()
557 
558 
559 
560 
561 }//end class
562 
563 ?>