Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
payment_gateway_esec.inc
1 <?php
17 require_once dirname(__FILE__).'/../../payment_gateway/payment_gateway.inc';
18 require_once dirname(__FILE__).'/esec_processor_integration_interface.inc';
19 
33 {
34 
35 
41  var $esec = NULL;
42 
43 
44  function __construct($assetid=0)
45  {
46  $this->_esec = new Esec_Processor_Integration_Interface();
47  parent::__construct($assetid);
48 
49  }//end constructor
50 
51 
62  function _createAdditional(&$link)
63  {
64  if (!parent::_createAdditional($link)) return FALSE;
65 
66  return $this->_createBodycopy();
67 
68  }//end _createAdditional()
69 
70 
79  function _createBodycopy($default_content=NULL)
80  {
81  if (is_null($default_content)) {
82  $default_content = translate('default_bodycopy_content');
83  }
84 
85  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy');
86  $bodycopy = new Bodycopy();
87  $copy_link = Array('asset' => &$this, 'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
88 
89  $bodycopy->setAttrValue('name', 'Display Format');
90  $args = Array('content' => $default_content);
91  if ($bodycopy->create($copy_link, $args)) {
92  $child_bodycopy = $GLOBALS['SQ_SYSTEM']->am->getLink($bodycopy->id, SQ_LINK_TYPE_2, 'bodycopy_div');
93  $GLOBALS['SQ_SYSTEM']->am->releaseLock($child_bodycopy['minorid'], 'attributes');
94  return TRUE;
95  }
96  return FALSE;
97 
98  }//end _createBodycopy()
99 
100 
108  function _getAllowedLinks()
109  {
110  $page_links = parent::_getAllowedLinks();
111  $page_links[SQ_LINK_TYPE_2]['bodycopy'] = Array('card' => 2, 'exclusive' => TRUE);
112  return $page_links;
113 
114  }//end _getAllowedLinks()
115 
116 
131  {
132  $keywords = parent::getAvailableKeywords();
133 
134  $keywords['credit_card_number'] = 'Credit Card Number';
135  $keywords['credit_card_expiry_year'] = 'Credit Card Expiry Year';
136  $keywords['credit_card_expiry_month'] = 'Credit Card Expiry Month';
137  $keywords['credit_card_name'] = 'Name on card';
138  $keywords['credit_card_ccv'] = 'Credit Card CCV field';
139  $keywords['credit_card_type_selector'] = 'Credit Card Type Selector';
140  $keywords['submit_button'] = 'Submit Button';
141  $keywords['cancel_button'] = 'Cancel Button';
142  $keywords['reset_button'] = 'Reset Button';
143  $keywords['transaction_amount'] = 'Transaction Amount';
144  $keywords['processing_error'] = 'Processing Error (empty if no error)';
145 
146  return $keywords;
147 
148  }//end getAvailableKeywords()
149 
150 
161  function onRequestKeywords(&$broadcaster, $vars=Array())
162  {
163  $keywords = $this->getAvailableKeywords();
164  $vars['keywords'] = array_merge($vars['keywords'], $keywords);
165 
166  }//end onRequestKeywords()
167 
168 
175  function printBody()
176  {
177  $amount = array_get_index($_SESSION,'SQ_ECOM_AMOUNT', 0);
178 
179  $card_info = array_get_index($_REQUEST, $this->getPrefix().'_card', FALSE);
180  $action = array_get_index($_REQUEST, $this->getPrefix().'_action', FALSE);
181  if ($action == 'Cancel') {
182  $this->returnToCaller('CANCEL');
183 
184  } else if (empty($amount)) {
185  unset($_SESSION['SQ_ECOM_AMOUNT']);
186  $this->appendPendingOrderAuditLog(translate('ecom_payment_no_amount'));
187  $this->returnToCaller('SUCCESS');
188 
189  } else if ($card_info) {
190  $this->appendPendingOrderAuditLog(translate('ecom_payment_attempted_gateway_contacted'));
191 
192  if ($this->processPayment($card_info)) {
193  unset($_SESSION['SQ_ECOM_AMOUNT']);
194  $this->appendPendingOrderAuditLog(translate('ecom_payment_succeeded'));
195  $this->returnToCaller('SUCCESS');
196  } else {
197  $this->appendPendingOrderAuditLog(translate('ecom_payment_declined'));
198  }
199  }
200 
201  $this->printBodycopy();
202 
203 
204  }//end printBody()
205 
206 
213  function printBodycopy()
214  {
215  $bodycopy =& $this->getBodycopy();
216  if (is_null($bodycopy)) return;
217 
218  $keywords = $bodycopy->getKeywords();
219  $replacements = Array();
220  foreach ($keywords as $word) {
221  $replacements[$word] = $this->getKeywordReplacement($word);
222  }
223  $bodycopy->setKeywordReplacements($replacements);
224 
225  // OUTPUT HERE
226  $datapath = sq_web_path('data').'/asset_types/payment_gateway_esec/files';
227  $card = $this->getPrefix().'_card';
228 
229  if ($this->attr('test_mode')) {
230  echo '<h2 class="gateway-test-mode" style="color: white; background-color: #C00; padding: 5px">TEST MODE</h2>';
231  }
232 
233  ?>
234  <script src="<?php echo $datapath; ?>/loader.js"></script>
235  <script>
236  var Loader = new Loader('<?php echo $card; ?>','#FFFFFF','Processing Transaction...','<?php echo $datapath; ?>/loader.gif');
237  Loader.print();
238  </script>
239  <form method="post" action="<?php echo $this->getURL(); ?>" onsubmit="Loader.show();">
240  <?php
241  $bodycopy->printBody();
242  echo '</form>';
243 
244  }//end printBodycopy()
245 
246 
253  function &getBodycopy()
254  {
255  $null = NULL;
256  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, '', 'major');
257  if (empty($link)) return $null;
258  $bodycopy = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
259  return $bodycopy;
260 
261  }//end getBodycopy()
262 
263 
271  {
272  $amount = array_get_index($_SESSION,'SQ_ECOM_AMOUNT', 0);
273 
275  $card_types = $Esec->getCardTypes();
276 
277  // format the amount
278  $Esec->setAmount($amount);
279  $amount = $Esec->getAmount();
280 
281  $prefix = $this->getPrefix();
282  $card = $prefix.'_card';
283  $form_contents = '';
284 
285  // determine which card type is selected
286  $form_contents_select = '';
287  foreach ($card_types as $type) {
288  $form_contents_select .= '<option value="'.$type.'>'.$type.'</option>';
289  }
290 
291  // set variables for test mode
292  if ($this->attr('test_mode')) {
293 
294  $form_contents .= '<h2 style="color: white; background-color: grey; padding: 5px">TEST MODE</h2>';
295 
296  $test_card_numbers = $Esec->getTestCardNumbers();
297  $card_number = '<select name="'.$card.'[number]">';
298  foreach ($test_card_numbers as $number => $label) {
299  $card_number .= '<option value="'.$number.'>'.$number.'</option>';
300  }
301  $card_number .= '</select>';
302  } else {
303  $card_number = '<input name="'.$card.'[number]" autocomplete="off" />';
304  }
305 
306  // check for errors
307  if (array_get_index($this->_tmp, 'is_error', FALSE)) {
308  $form_contents .= '<h2 style="color: red; ">Error '.$this->_tmp['error_message'].'</h2>';
309  }
310 
311  $message = $this->attr('message');
312 
313  $datapath = sq_web_path('data').'/asset_types/payment_gateway_esec/files';
314 
315  $own_url = $this->getURL();
316 
317  ob_start();
318  ?>
319  <script src="<?php echo $datapath; ?>/loader.js"></script>
320  <script>
321  var Loader = new Loader('<?php echo $card; ?>','#FFFFFF','Processing Transaction...','<?php echo $datapath; ?>/loader.gif');
322  Loader.print();
323  </script>
324  <form method="post" action="<?php echo $own_url; ?>" onsubmit="Loader.show();">
325  <div><?php echo $message; ?></div>
326  <div style="background-color: #CCC; padding: 5px"><strong>$<?php echo $amount; ?></strong> Australian Dollars are about to be debited from your credit card</div>
327  <table cellpadding="5">
328  <tr>
329  <td align="right">Name on Card</td>
330  <td><input name="<?php echo $card; ?>[name]" autocomplete="off" /></td>
331  </tr>
332  <tr>
333  <td align="right">Number</td>
334  <td><?php echo $card_number; ?></td>
335  </tr>
336  <?php
337  if ($this->attr('display_ccv')) {
338  ?>
339  <tr>
340  <td align="right">CCV</td>
341  <td><input name="<?php echo $card; ?>[ccv]" size='4' autocomplete="off" /> - from the back of your credit card</td>
342  </tr>
343  <?php
344  }
345  ?>
346  <tr>
347  <td align="right">Expiry Date (MM/YY)</td>
348  <td><input name="<?php echo $card; ?>[month]" size="2" autocomplete="off" /> / <input name="<?php echo $card; ?>[year]" size="2" autocomplete="off" /></td>
349  </tr>
350  <tr>
351  <td align="right">Type</td>
352  <td>
353  <select name="<?php echo $card; ?>[type]">
354  <?php echo $form_contents_select; ?>
355  </select>
356  </td>
357  </tr>
358  <tr>
359  <td>&nbsp;</td>
360  <td><input type="submit" value=" Pay " /><input type="reset" value="Reset" /><input type="submit" name="<?php echo $prefix; ?>_action" value="Cancel" /></td>
361  </tr>
362  </table>
363  </form>
364  <?php
365  $form_contents .= ob_get_clean();
366 
367 
368  return $form_contents;
369 
370  }//end makeCreditCardForm()
371 
372 
381  function processPayment($card_info)
382  {
383  if (!$card_info) return FALSE;
384 
385  $ref_no = $_SESSION['SQ_ECOM_REF_NO'];
386  $amount = $_SESSION['SQ_ECOM_AMOUNT'];
387 
389 
390  $Esec->setAmount($amount);
391  $Esec->setReference($ref_no);
392  $Esec->setMerchant($this->attr('merchant_id'));
393  $Esec->setCardNumber($card_info['number']);
394  $Esec->setCCV(array_get_index($card_info, 'ccv', ''));
395  $Esec->setCardType($card_info['type']);
396  $Esec->setCardExpiryMonth($card_info['month']);
397  $Esec->setCardExpiryYear($card_info['year']);
398  $Esec->setCardName($card_info['name']);
399 
400  if ($this->attr('test_mode')) $Esec->setTest();
401 
402  $success = $Esec->process();
403 
404  // process response
405  $response = $Esec->getResponse();
406  if (!empty($response['REFERENCE'])) {
407  $_SESSION['SQ_ECOM_REF_NO'] = $response['REFERENCE'];
408  }
409 
410  if (!$success) {
411  $this->_tmp['is_error'] = TRUE;
412  $this->_tmp['error_message'] = $response['MESSAGE_CODE'].' '.$response['MESSAGE'];
413  } else {
414  $status = $Esec->explainStatus();
415  if ($this->attr('test_mode')) {
416  $status = '!!!ATTENTION!!! TEST MODE (transaction not performed) -- '.$status;
417  }
418 
419  $response['STATUS'] = $status;
420  // preparing card number; should contain only 4 last digits
421  $cardno = '';
422  $cardlength = strlen($card_info['number']);
423  if ($cardlength < 16) {
424  for ($i = 0; $i < $cardlength - 4; $i++) {
425  $cardno .= '*';
426  }
427  $cardno .= substr($card_info['number'], $i);
428  } else {
429  $cardno = '****-****-****-'.substr($card_info['number'],12);
430  }
431  $response['CARDNO'] = $cardno;
432  $response['TIME'] = date('r');
433  $response['AMOUNT'] = $amount;
434 
435  $_SESSION['SQ_ECOM_RESPONSE'] = $response;
436  }
437 
438  return $success;
439 
440  }//end processPayment()
441 
442 
451  function returnToCaller($state='SUCCESS')
452  {
453  $back_url = NULL;
454 
455  if ($state == 'SUCCESS') {
456  $back_url = array_get_index($_SESSION, 'SQ_ECOM_SUCCESS_URL');
457  unset($_SESSION['SQ_ECOM_SUCCESS_URL']);
458 
459  } else if ($state == 'CANCEL') {
460  $back_url = array_get_index($_SESSION, 'SQ_ECOM_CANCEL_URL');
461  unset($_SESSION['SQ_ECOM_CANCEL_URL']);
462  }
463 
464  if (is_null($back_url)) {
465  trigger_error('Unknown caller reference');
466  } else {
467  header('Location: '.$back_url);
468  exit;
469  }
470 
471  }//end returnToCaller()
472 
473 
481  {
482 
483  $prefix = $this->getPrefix();
484  $card = $prefix.'_card';
485 
486  // set variables for test mode
487  if ($this->attr('test_mode')) {
488  $test_card_numbers = $this->_esec->getTestCardNumbers();
489  $card_number = '<select name="'.$card.'[number]">';
490  foreach ($test_card_numbers as $number => $label) {
491  $card_number .= '<option value="'.$number.'" >'.$number.'</option>';
492  }
493  $card_number .= '</select>';
494  } else {
495  $card_number = '<input name="'.$card.'[number]" autocomplete="off" />';
496  }
497 
498  return $card_number;
499 
500  }//end getCreditCardNumberKeywordReplacement()
501 
502 
510  {
511 
512  $card = $this->getPrefix().'_card';
513  $string = '<input name="'.$card.'[name]" autocomplete="off" />';
514  return $string;
515 
516  }//end getCreditCardNameKeywordReplacement()
517 
518 
526  {
527 
528  $card = $this->getPrefix().'_card';
529  $string = '<input name="'.$card.'[ccv]" size="4" autocomplete="off" />';
530  return $string;
531 
532  }//end getCreditCardCcvKeywordReplacement()
533 
534 
542  {
543 
544  $card = $this->getPrefix().'_card';
545 
546  $card_types = $this->_esec->getCardTypes();
547 
548  ob_start();
549  echo '<select name="'.$card.'[type]">';
550  foreach ($card_types as $type) {
551  echo '<option value="'.$type.'">'.$type.'</option>';
552  }
553 
554  echo '</select>';
555 
556  return ob_get_clean();
557 
558  }//end getCreditCardTypeSelectorKeywordReplacement()
559 
560 
568  {
569  $amount = array_get_index($_SESSION,'SQ_ECOM_AMOUNT', 0);
570  $this->_esec->setAmount($amount);
571  $amount = $this->_esec->getAmount();
572 
573  return $amount;
574 
575  }//end getTransactionAmountKeywordReplacement()
576 
577 
585  {
586  if (array_get_index($this->_tmp, 'is_error', FALSE)) {
587  return '<span class="payment-gateway-transaction-error">'.$this->_tmp['error_message'].'</span>';
588  }
589 
590  return '';
591 
592  }//end getProcessingErrorKeywordReplacement()
593 
594 
602  {
603 
604  $card = $this->getPrefix().'_card';
605 
606  $string = '<input name="'.$card.'[year]" size="2" autocomplete="off" />';
607  return $string;
608 
609  }//end getCreditCardExpiryYearKeywordReplacement()
610 
611 
619  {
620 
621  $card = $this->getPrefix().'_card';
622 
623  $string = '<input name="'.$card.'[month]" size="2" autocomplete="off" />';
624  return $string;
625 
626  }//end getCreditCardExpiryMonthKeywordReplacement()
627 
628 
636  {
637  $button_text = $this->attr('submit_text');
638  if (empty($button_text)) $button_text = 'Submit';
639  return '<input type="submit" value="'.$button_text.'" />';
640 
641  }//end getSubmitButtonKeywordReplacement()
642 
643 
651  {
652  $button_text = $this->attr('reset_text');
653  if (empty($button_text)) $button_text = 'Reset';
654  return '<input type="reset" value="'.$button_text.'" />';
655 
656  }//end getResetButtonKeywordReplacement()
657 
658 
666  {
667  $button_text = $this->attr('cancel_text');
668  if (empty($button_text)) $button_text = 'Cancel';
669  return '<input type="submit" name="'.$this->getPrefix().'_action" value="'.$button_text.'" />';
670 
671  }//end getCancelButtonKeywordReplacement()
672 
673 
674 }//end class
675 
676 ?>