18 require_once dirname(__FILE__).
'/../../payment_gateway/payment_gateway.inc';
19 require_once dirname(__FILE__).
'/dps_processor.inc';
39 private $_pass_through_vars = NULL;
49 parent::__construct($assetid);
66 if (!parent::_createAdditional($link))
return FALSE;
68 return ($this->
_createBodycopy(
'display_format', translate(
'default_bodycopy_content')));
84 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bodycopy');
86 $copy_link = Array(
'asset' => &$this,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1,
'value' => $link_value);
88 $bodycopy->setAttrValue(
'name', ucwords(str_replace(
'_',
' ', $link_value)));
89 $args = Array(
'content' => $content);
90 if ($bodycopy->create($copy_link, $args)) {
91 $child_bodycopy = $GLOBALS[
'SQ_SYSTEM']->am->getLink($bodycopy->id, SQ_LINK_TYPE_2,
'bodycopy_div');
92 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($child_bodycopy[
'minorid'],
'attributes');
109 $page_links = parent::_getAllowedLinks();
110 $page_links[SQ_LINK_TYPE_2][
'bodycopy'] = Array(
'card' => 2,
'exclusive' => TRUE);
131 $keywords = parent::getAvailableKeywords();
133 $keywords[
'card_number'] =
'Card Number';
134 $keywords[
'card_name'] =
'Name on Card';
135 $keywords[
'card_start_month'] =
'Card Start Month';
136 $keywords[
'card_start_year'] =
'Card Start Year';
137 $keywords[
'card_expiry_month'] =
'Card Expiry Month';
138 $keywords[
'card_expiry_year'] =
'Card Expiry Year';
139 $keywords[
'card_issue_number'] =
'Card Issue Number';
140 $keywords[
'card_cv2'] =
'Card CV2';
142 $keywords[
'street_address1'] =
'Address Line 1';
143 $keywords[
'street_address2'] =
'Address Line 2';
144 $keywords[
'street_address3'] =
'Address Line 3';
145 $keywords[
'street_address4'] =
'Address Line 4';
146 $keywords[
'postcode'] =
'Postcode';
148 $keywords[
'transaction_amount'] =
'Transaction Amount';
149 $keywords[
'transaction_currency'] =
'Transaction Currency';
150 $keywords[
'processing_error'] =
'Processing Error (empty if no error)';
151 $keywords[
'submit_button'] =
'Submit Button';
152 $keywords[
'cancel_button'] =
'Cancel Button';
153 $keywords[
'reset_button'] =
'Reset Button';
174 $vars[
'keywords'] = array_merge($vars[
'keywords'], $keywords);
188 if (array_get_index($_GET,
'unknown_referer', FALSE)) {
189 trigger_error(
'Unknown caller reference');
194 if (array_get_index($_GET,
'error_message', FALSE)) {
195 $this->_setError($_GET[
'error_message']);
201 $amount = array_get_index($_SESSION,
'SQ_ECOM_AMOUNT', 0);
202 $card_info = array_get_index($_REQUEST, $this->
getPrefix().
'_card', FALSE);
203 $action = array_get_index($_REQUEST, $this->
getPrefix().
'_action', FALSE);
206 if ($action ==
'Cancel') {
209 }
else if (empty($amount)) {
210 unset($_SESSION[
'SQ_ECOM_AMOUNT']);
211 $this->_unsetCurrency();
213 if (isset($_SESSION[
'SQ_ECOM_PASS_THROUGH_VARS'])) {
214 unset($_SESSION[
'SQ_ECOM_PASS_THROUGH_VARS']);
221 }
else if ($card_info) {
228 if ($success === 1) {
229 unset($_SESSION[
'SQ_ECOM_AMOUNT']);
230 $this->_unsetCurrency();
232 if (isset($_SESSION[
'SQ_ECOM_PASS_THROUGH_VARS'])) {
233 unset($_SESSION[
'SQ_ECOM_PASS_THROUGH_VARS']);
236 if (isset($_SESSION[
'SQ_ECOM_PAYMENT_COMPLETED_BROADCASTER_ASSETID'])) {
237 $broadcaster = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($_SESSION[
'SQ_ECOM_PAYMENT_COMPLETED_BROADCASTER_ASSETID']);
238 if (!empty($broadcaster)) {
240 $response = array_get_index($_SESSION,
'SQ_ECOM_RESPONSE', FALSE);
242 $_POST[
'transaction_reference'] = $response[
'TRANSACTION'];
243 $_POST[
'transaction_time'] = $response[
'TIME'];
244 $_POST[
'transaction_status'] = $response[
'STATUS'];
245 $_POST[
'transaction_card_number'] = $response[
'CARDNO'];
246 $_POST[
'transaction_amount'] = $response[
'AMOUNT'];
247 $_POST[
'transaction_currency'] = $response[
'CURRENCY'];
249 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_ecommerce_payment_completed', $broadcaster);
277 if (is_null($bodycopy))
return;
279 $keywords = $bodycopy->getKeywords();
280 $replacements = Array();
281 foreach ($keywords as $word) {
284 $bodycopy->setKeywordReplacements($replacements);
287 if ($link_value ==
'display_format') {
289 $datapath = sq_web_path(
'data').
'/asset_types/payment_gateway_datacash/files';
292 if ($this->
attr(
'test_mode')) {
293 echo
'<h2 class="gateway-test-mode" style="color: white; background-color: #C00; padding: 5px">TEST MODE</h2>';
297 <script src=
"<?php echo $datapath; ?>/loader.js"></script>
299 var Loader =
new Loader(
'<?php echo $card; ?>',
'#FFFFFF',
'Processing Transaction...',
'<?php echo $datapath; ?>/loader.gif');
302 <form method=
"post" action=
"<?php echo $this->getURL(); ?>" onsubmit=
"Loader.show();">
304 $bodycopy->printBody();
308 $bodycopy->printBody();
322 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE, $link_value);
323 if (empty($link))
return NULL;
324 $bodycopy = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
340 if (!$card_info)
return FALSE;
343 foreach($card_info as $key => $value){
344 $card_info[$key] = htmlentities($value);
347 $amount = $_SESSION[
'SQ_ECOM_AMOUNT'];
352 if (empty($amount)) {
353 $this->_setError($this->
attr(
'amount_not_specified_error'));
355 $processor->setAmount($amount);
356 $processor->setCurrency($this->_getCurrency());
361 if (empty($card_info[
'number'])) {
362 $this->_setError($this->
attr(
'card_number_not_specified_error'));
364 $card_info[
'number'] = str_replace(Array(
' ',
"\t"),
'', $card_info[
'number']);
365 $processor->setCardNumber($card_info[
'number']);
369 if (!empty($card_info[
'name'])) {
370 $processor->setCardName($card_info[
'name']);
374 if ($this->_isValidDate(array_get_index($card_info,
'expiry_month',
''), array_get_index($card_info,
'expiry_year',
''))) {
375 $processor->setCardExpiryDate($card_info[
'expiry_month'], $card_info[
'expiry_year']);
377 $this->_setError($this->
attr(
'expiry_date_invalid_error'));
381 if (!empty($card_info[
'start_month']) || !empty($card_info[
'start_year'])) {
382 if ($this->_isValidDate(array_get_index($card_info,
'start_month',
''), array_get_index($card_info,
'start_year',
''))) {
383 $processor->setCardStartDate($card_info[
'start_month'], $card_info[
'start_year']);
385 $this->_setError($this->
attr(
'start_date_invalid_error'));
390 if (!empty($card_info[
'issue_number'])) {
391 if ($this->_isValidIssueNumber($card_info[
'issue_number'])) {
392 $processor->setCardIssueNumber($card_info[
'issue_number']);
394 $this->_setError($this->
attr(
'issue_number_invalid_error'));
399 if($this->
attr(
'cv2_standard_policy') != 0){
400 if (!empty($card_info[
'cv2']) && $this->_isValidCV2($card_info[
'cv2'])) {
401 $processor->setCardCV2($card_info[
'cv2']);
403 $this->_setError($this->
attr(
'card_cv2_invalid_error'));
409 for ($i = 1; $i < 5; $i++) {
410 if (!empty($card_info[
'street_address'.$i])) {
411 $addr .= $card_info[
'street_address'.$i].
"\n";
415 $processor->setStreetAddress($addr);
419 if (!empty($card_info[
'postcode'])) {
420 $addr .= $card_info[
'postcode'].
" ";
421 $processor->setPostcode($card_info[
'postcode']);
426 $policy = $this->
attr(
'avs_standard_policy');
427 $processor->setStandardPolicy($policy);
429 if (empty($card_info[
'street_address1']) && empty($card_info[
'street_address2']) && empty($card_info[
'street_address3']) && empty($card_info[
'street_address4'])) {
430 $this->_setError($this->
attr(
'street_address_not_specified_error'));
433 if (empty($card_info[
'postcode'])) {
434 $this->_setError($this->
attr(
'postcode_not_specified_error'));
443 $success = $processor->process();
446 $response = $processor->getResponse();
451 $_SESSION[
'SQ_ECOM_FORMATTED_CARD_NUMBER'] = $this->_getFormattedCardNumber($card_info[
'number']);
453 $_SESSION[
'SQ_ECOM_ORDER_BILLING_ADDR'] = $addr;
456 $this->_setTransactionResult($success, $response);
473 private function _setTransactionResult($success, $response)
475 if ($success === FALSE) {
476 $this->_setError($response[
'STATUS']);
478 if ($this->
attr(
'test_mode')) {
479 $response[
'STATUS'] =
'!!!ATTENTION!!! TEST MODE (transaction not performed) -- '.$response[
'STATUS'];
483 if (isset($_SESSION[
'SQ_ECOM_FORMATTED_CARD_NUMBER'])) {
484 $card_no = $_SESSION[
'SQ_ECOM_FORMATTED_CARD_NUMBER'];
485 unset($_SESSION[
'SQ_ECOM_FORMATTED_CARD_NUMBER']);
488 $response[
'CARDNO'] = $card_no;
489 $response[
'AMOUNT'] = $_SESSION[
'SQ_ECOM_AMOUNT'];
490 $response[
'CURRENCY'] = $this->_getCurrency();
493 if (isset($_SESSION[
'SQ_ECOM_ORDER_BILLING_ADDR'])) {
494 $billing_addr = $_SESSION[
'SQ_ECOM_ORDER_BILLING_ADDR'];
495 unset($_SESSION[
'SQ_ECOM_ORDER_BILLING_ADDR']);
497 $response[
'BILLING_ADDR'] = $billing_addr;
499 $_SESSION[
'SQ_ECOM_RESPONSE'] = $response;
513 private function _getFormattedCardNumber($card_number)
515 $card_no = $card_number;
516 $card_len = strlen($card_no);
517 if ($card_len == 16) {
518 $card_no =
'****-****-****-'.substr($card_no, -4);
520 $card_no = str_pad(substr($card_no, -4), $card_len,
'*', STR_PAD_LEFT);
540 if ($state ==
'SUCCESS') {
541 $back_url = array_get_index($_SESSION,
'SQ_ECOM_SUCCESS_URL');
542 }
else if ($state ==
'CANCEL') {
543 $back_url = array_get_index($_SESSION,
'SQ_ECOM_CANCEL_URL');
546 unset($_SESSION[
'SQ_ECOM_SUCCESS_URL']);
547 unset($_SESSION[
'SQ_ECOM_CANCEL_URL']);
549 if (is_null($back_url)) {
550 trigger_error(
'Unknown caller reference');
552 header(
'Location: '.$back_url);
567 private function _redirectParent($url)
570 <script
type=
"text/javascript">
571 if (parent !=
self) {
572 parent.location =
"{$url}";
592 $card = $prefix.
'_card';
595 if ($this->
attr(
'test_mode')) {
597 $card_number =
'<select name="'.$card.
'[number]">';
598 foreach ($test_card_numbers as $number => $label) {
599 $card_number .=
'<option value="'.$number.
'" >'.$label.
'</option>';
601 $card_number .=
'</select>';
603 $card_number =
'<input name="'.$card.
'[number]" autocomplete="off" />';
621 $string =
'<input name="'.$card.
'[name]" autocomplete="off" />';
637 $string =
'<input name="'.$card.
'[start_month]" size="2" autocomplete="off" />';
654 $string =
'<input name="'.$card.
'[start_year]" size="2" autocomplete="off" />';
671 $string =
'<input name="'.$card.
'[expiry_month]" size="2" autocomplete="off" />';
688 $string =
'<input name="'.$card.
'[expiry_year]" size="2" autocomplete="off" />';
705 $string =
'<input name="'.$card.
'[issue_number]" size="4" autocomplete="off" />';
722 $string =
'<input name="'.$card.
'[cv2]" size="4" autocomplete="off" />';
739 text_box($card.
'[street_address1]', $this->_getDefaultInputFromPassThroughAttribute(
'addr1_var_name'));
741 return ob_get_clean();
756 text_box($card.
'[street_address2]', $this->_getDefaultInputFromPassThroughAttribute(
'addr2_var_name'));
758 return ob_get_clean();
773 text_box($card.
'[street_address3]', $this->_getDefaultInputFromPassThroughAttribute(
'addr3_var_name'));
775 return ob_get_clean();
790 text_box($card.
'[street_address4]', $this->_getDefaultInputFromPassThroughAttribute(
'addr4_var_name'));
792 return ob_get_clean();
807 text_box($card.
'[postcode]', $this->_getDefaultInputFromPassThroughAttribute(
'postcode_var_name'));
809 return ob_get_clean();
822 $amount = array_get_index($_SESSION,
'SQ_ECOM_AMOUNT', 0);
837 return $this->_getCurrency();
851 return '<span class="payment-gateway-transaction-error">'.$this->getErrorMessage().
'</span>';
867 $button_text = $this->
attr(
'submit_text');
868 if (empty($button_text)) $button_text =
'Submit';
870 return '<input type="submit" value="'.$button_text.
'" />';
883 $button_text = $this->
attr(
'reset_text');
884 if (empty($button_text)) $button_text =
'Reset';
886 return '<input type="reset" value="'.$button_text.
'" />';
899 $button_text = $this->
attr(
'cancel_text');
900 if (empty($button_text)) $button_text =
'Cancel';
902 return '<input type="submit" name="'.$this->getPrefix().
'_action" value="'.$button_text.
'" />';
915 private function _setError($err_message)
917 $this->_tmp[
'is_error'] = TRUE;
918 $this->_tmp[
'error_message'][] = $err_message;
931 private function _isValidDate($month, $year)
935 $two_digits_pattern =
'/^\d{2}$/';
937 if (preg_match($two_digits_pattern, $month) && preg_match($two_digits_pattern, $year)) {
939 if ((0 < $month) && ($month < 13)) {
957 private function _isValidIssueNumber($issue_no)
961 $pattern =
'/^\d{1,4}$/';
963 if (preg_match($pattern, $issue_no)) {
980 private function _isValidCV2($cv2)
984 $pattern =
'/^\d{3,4}$/';
986 if (preg_match($pattern, $cv2)) {
995 private function _getPassThroughVariable($var_name, $default)
997 if (is_null($this->_pass_through_vars)) {
998 $this->_pass_through_vars = array_get_index($_SESSION,
'SQ_ECOM_PASS_THROUGH_VARS', Array());
1001 return array_get_index($this->_pass_through_vars, $var_name, $default);
1006 private function _getDefaultInputFromPassThroughAttribute($attr_name)
1009 $var_name = $this->
attr($attr_name);
1010 if (!empty($var_name)) {
1011 $result = $this->_getPassThroughVariable($var_name,
'');
1025 private function _getCurrency()
1027 return isset($_SESSION[
'SQ_ECOM_CURRENCY'])? $_SESSION[
'SQ_ECOM_CURRENCY'] : $this->
attr(
'currency');
1038 private function _unsetCurrency()
1040 if (isset($_SESSION[
'SQ_ECOM_CURRENCY'])) {
1041 unset($_SESSION[
'SQ_ECOM_CURRENCY']);
1055 return array_get_index($this->_tmp,
'is_error', FALSE);
1069 $errors = array_get_index($this->_tmp,
'error_message', Array());
1070 foreach ($errors as $error) {
1071 $err_message .=
"<li>$error </li>";
1074 return '<ul>'.$err_message.
'</ul>';