18 define(
'UTRANSACT_LOG_FILE',
'utransact_transactions');
19 define(
'MAX_TRANSACTION_PROCESS_ATTEMPT', 5);
21 require_once dirname(__FILE__).
'/transaction_dataset.inc';
49 private $_transactions_url;
57 private $_services_url;
65 private $_bgas_client;
73 private $_transactions_client;
81 private $_services_client;
100 private $_auth_bgas_header_ns =
"http://www.paymentgateway.co.nz/pgservice/Bgas.asmx";
108 private $_auth_transactions_header_ns =
"http://www.paymentgateway.co.nz/pgservice/Transactions.asmx";
116 private $_auth_services_header_ns =
"http://www.paymentgateway.co.nz/pgservice/Services.asmx";
124 private $_card_types;
132 private $_test_card_numbers = Array (
133 "4111111111111111" =>
"Visa Card Test",
134 "5555555555554444" =>
"MasterCard Card Test",
135 "492" =>
"Invalid Card Test",
157 private $_reference_no;
164 private $_credit_card;
171 private $_credit_card_cv2;
178 private $_expiry_month;
185 private $_expiry_year;
192 private $_card_holder_name;
199 private $_merchant_id;
206 private $_payment_model;
213 private $_first_name;
233 private $_address_line_1;
240 private $_address_line_2;
268 private $_cart_items = Array();
275 private $_response = Array();
289 function __construct($username, $password, $bgas_url, $transactions_url, $services_url)
291 $this->_username = $username;
292 $this->_password = $password;
294 $this->_bgas_url = $bgas_url;
295 $this->_transactions_url = $transactions_url;
296 $this->_services_url = $services_url;
298 $this->_bgas_client =
new SoapClient($this->_bgas_url, array(
'trace' => 1));
299 $this->_transactions_client =
new SoapClient($this->_transactions_url, array(
'trace' => 1));
300 $this->_services_client =
new SoapClient( $this->_services_url, array(
'trace' => 1));
302 $this->_log_file_name = UTRANSACT_LOG_FILE;
317 $transaction_handle = isset($_SESSION[
'UTRANSACT_TRANSACTION_HANDLE']) ? $_SESSION[
'UTRANSACT_TRANSACTION_HANDLE'] : FALSE;
319 if (!$transaction_handle) {
321 $this->_response[
'STATUS'] =
"FAIL";
322 $this->_response[
'MESSAGE'] =
"Couldn't find transaction handle in session";
324 $this->_log(
"Couldn't find transaction handle in the session for the transaction ref ".$this->_reference_no);
329 if (empty($this->_cart_items)) {
331 $this->_response[
'STATUS'] =
"FAIL";
332 $this->_response[
'MESSAGE'] =
"Cart items not found in session";
334 $this->_log(
"Cart items not found in session for transaction ref ".$this->_reference_no);
340 $this->_response[
'STATUS'] =
"FAIL";
341 $this->_response[
'MESSAGE'] = translate(
'utransact_problem_while_processing', $this->_reference_no);
343 $this->_log(
"Cannot acquire Utransact bga authentication token");
352 $transaction_table->addTransactionRow(
353 $this->_reference_no,
354 empty($this->_credit_card_cv2) ? $this->_credit_card : $this->_credit_card.
','.$this->_credit_card_cv2,
355 $this->_expiry_year.
'/'.$this->_expiry_month,
356 $this->_card_holder_name,
358 $this->_payment_model
362 $transaction_table->addPurchaserRow(
366 $this->_address_line_1,
367 $this->_address_line_2,
374 foreach($this->_cart_items as $item) {
375 $transaction_table->addLineItemRow(
376 $item[
"ApplicationUsername"],
378 $item[
"Description"],
381 $item[
"RefundExpiryDate"],
383 $item[
"ProductCode"],
384 $item[
"FinancialCode"],
385 $item[
"FinancialValues"],
386 $item[
"AdditionalInfo"]
394 $new_transaction_dataset = $transaction_table->getTransactionDatasetXML();
397 $transaction_auth_header = $this->
_getAuthSoapHeader($this->_auth_transactions_header_ns);
399 $process_attempt_count = 1;
400 $transaction_response = null;
402 while (empty($transaction_response)) {
407 $transaction_exists_response = $this->_transactions_client->__soapCall(
410 'parameters' => Array(
411 'bgaName' => $this->_username,
412 'referenceId' => $this->_reference_no,
416 $transaction_auth_header
420 if (isset($transaction_exists_response->TransactionExistsResult) && $transaction_exists_response->TransactionExistsResult) {
421 $transaction_result = $transaction_exists_response->TransactionExistsResult;
426 $transaction_response = $this->_transactions_client->__soapCall(
427 'ProcessTransaction',
429 'parameters' => Array(
430 'transactionHandle' => $transaction_handle,
431 'transaction' => Array(
432 'any' => $new_transaction_dataset,
437 $transaction_auth_header
439 $transaction_result = isset($transaction_response->ProcessTransactionResult->any) ? $transaction_response->ProcessTransactionResult->any :
'';
442 }
catch (SoapFault $fault) {
443 $this->_response[
'STATUS'] =
"FAIL";
444 $this->_response[
'MESSAGE'] = $fault->getMessage().
"<br>".translate(
'utransact_problem_while_processing', $this->_reference_no);
446 $this->_log($fault->getMessage());
449 }
catch (BusinessRuleException $e) {
450 $this->_response[
'STATUS'] =
"FAIL";
451 $this->_response[
'MESSAGE'] = translate(
'utransact_problem_while_processing', $this->_reference_no);
453 $this->_log(
"ERROR: Transaction expection occured for transaction ref ".$this->_reference_no.
": ".$e->getMessage());
456 }
catch (TransactionException $e) {
457 $this->_response[
'STATUS'] =
"FAIL";
458 $this->_response[
'MESSAGE'] = translate(
'utransact_problem_while_processing', $this->_reference_no);
460 $this->_log(
"ERROR: Transaction expection occured for transaction ref ".$this->_reference_no.
": ".$e->getMessage());
463 }
catch (CoreUnavailableException $e) {
466 $this->_log(
"ERROR: Attempt #$process_attempt_count, Core not available while processing transaction ".$this->_reference_no.
" :".$e->getMessage());
469 if ($process_attempt_count > MAX_TRANSACTION_PROCESS_ATTEMPT) {
470 $this->_response[
'STATUS'] =
"FAIL";
471 $this->_response[
'MESSAGE'] = translate(
'utransact_core_not_available_for_processing', $this->_reference_no);
477 $process_attempt_count++;
486 $transaction_successful = FALSE;
489 if (!empty($transaction_errors)) {
492 foreach($transaction_errors as $error_id => $error_msg) {
493 $error_str .= $error_msg.
"<br>";
497 $message = $error_str;
499 $this->_log(
"ERROR: Error occured while processing transaction ref ".$this->_reference_no.
" : ".$message);
505 $transaction_details_response = $this->_transactions_client->__soapCall(
508 'parameters' => Array(
509 'bgaName' => $this->_username,
510 'referenceId' => $this->_reference_no,
514 $transaction_auth_header
516 }
catch (SoapFault $fault) {
518 $this->_log(
"ERROR: Transaction ref ".$this->_reference_no.
" completed, but couldn't obtain details: ".$e->getMessage());
521 if (isset($transaction_details_response->GetDetailsResult->any)) {
525 $status = $transaction_info[
'Status'];
526 $message =
"Transaction successfull";
528 $this->_response[
'AMOUNT'] = $transaction_info[
'Amount'];
529 $this->_response[
'GST'] = $transaction_info[
'GST'];
530 $this->_response[
'INVOICE_NUMBER'] = $transaction_info[
'InvoiceNumber'];
531 $this->_response[
'UTRANSACT_REFERENCE'] = $transaction_info[
'uTransactRef'];
532 $this->_response[
'MERCHANT_ID'] = $transaction_info[
'MerchantId'];
533 $this->_response[
'DATE'] = $transaction_info[
'Date'];
534 $this->_response[
'PAYMENT_GATEWAY_XML'] = $transaction_details_response->GetDetailsResult->any;
539 $message = translate(
'utransact_cannot_obtain_transaction_details', $this->_reference_no);
541 $this->_log(
"ERROR: Couldn't get transaction ref ".$this->_reference_no.
" details");
544 $transaction_successful = TRUE;
547 $this->_response[
'REFERENCE'] = $this->_reference_no;
548 $this->_response[
'STATUS'] = $status;
549 $this->_response[
'MESSAGE'] = $message;
552 $this->_logRequest();
553 $this->_logResponse();
555 return $transaction_successful;
574 $this->_response[
'STATUS'] =
"FAIL";
575 $this->_response[
'MESSAGE'] =
"Cannot obtain payment gateway authentication soap header";
577 $this->_log(
"Cannot obtain authentication soap header");
583 $transaction_handle = $this->_transactions_client->__soapCall(
584 'RequestTransactionHandle',
589 }
catch(SoapFault $fault){
590 $this->_log($fault->getMessage());
592 if (isset($_SESSION[
'UTRANSACT_AUTH_TOKEN'])) unset($_SESSION[
'UTRANSACT_AUTH_TOKEN']);
597 return isset($transaction_handle->RequestTransactionHandleResult) ? $transaction_handle->RequestTransactionHandleResult : FALSE;
613 if (empty($this->_auth_token)) {
614 $this->_auth_token = isset($_SESSION[
'UTRANSACT_AUTH_TOKEN']) ? $_SESSION[
'UTRANSACT_AUTH_TOKEN'] :
'';
618 if (!empty($this->_auth_token)) {
621 $soap_response = $this->_services_client->__soapCall(
623 Array(
'parameters' =>
625 'authCookie' => $this->_auth_token,
629 }
catch(SoapFault $fault){
630 $this->_log($fault->getMessage());
635 if (isset($soap_response->IsValidLoginResult) && $soap_response->IsValidLoginResult) {
642 $auth_token = $this->_bgas_client->__soapCall(
644 Array(
'parameters' => Array(
645 'username' => $this->_username,
646 'password' => $this->_password,
650 }
catch(SoapFault $fault){
651 $this->_log($fault->getMessage());
656 $response_xml = $this->_bgas_client->__getLastResponse();
657 preg_match(
"|<BgaAuthToken>(.*?)</BgaAuthToken>|i", $response_xml, $bga_token_match);
659 $auth_token = isset($bga_token_match[1]) ? $bga_token_match[1] :
'';
662 $this->_log(
"BgaAuthToken not found in LoginBga() response");
667 $this->_auth_token = $auth_token;
668 $_SESSION[
'UTRANSACT_AUTH_TOKEN'] = $this->_auth_token;
687 $this->_response[
'STATUS'] =
"FAIL";
688 $this->_response[
'MESSAGE'] =
"Cannot acquire Utransact bga authentication token";
693 $soap_header_data =
"
694 <PgWebServiceAuthToken".(empty($namespace) ?
">" :
" xmlns='".$namespace.
"'>").
"
695 <BgaAuthToken>".$this->_auth_token.
"</BgaAuthToken>
696 <UserAuthToken>".$this->_auth_token.
"</UserAuthToken>
697 </PgWebServiceAuthToken>";
700 $soap_header_var = @
new SoapVar($soap_header_data, XSD_ANYXML);
701 $soap_header = @
new SoapHeader($namespace,
"PgWebServiceAuthToken", $soap_header_var);
719 $this->_response[
'STATUS'] =
"FAIL";
720 $this->_response[
'MESSAGE'] =
"Cannot obtain list of credit card supported by payement gateway";
722 $this->_card_types = Array();
728 $cards_dataset = $this->_bgas_client->__soapCall(
729 'GetSupportedCreditCards',
734 }
catch(SoapFault $fault){
735 $this->_log($fault->getMessage());
739 preg_match_all(
'|<Id>(.*?)</Id>.*?<Name>(.*?)</Name>|msi', $cards_dataset->GetSupportedCreditCardsResult->any, $card_matches);
741 $card_list = Array();
742 foreach((isset($card_matches[1]) ? $card_matches[1] : Array()) as $key => $card_id) {
743 $card_list[$card_id] = isset($card_matches[2][$key]) ? $card_matches[2][$key] : $card_id;
746 $this->_card_types = $card_list;
764 $this->_log(
"Cannot obtain authetication header while attempting to get GST rate from payment gateway");
769 $gst_rate_response = $this->_transactions_client->__soapCall(
775 }
catch(SoapFault $fault){
776 $this->_log($fault->getMessage());
780 return isset($gst_rate_response->GetGstRateResult) ? $gst_rate_response->GetGstRateResult : FALSE;
795 if (empty($result)) {
796 return Array(translate(
'utransact_empty_response_while_processing', $this->_reference_no));
799 preg_match_all(
'|<ErrorCode>(.*?)</ErrorCode>.*?<DisplayMessage>(.*?)</DisplayMessage>|i',$result, $error_match);
802 if (!isset($error_match[1][0]))
return Array();
805 foreach($error_match[1] as $index => $error_code) {
806 $errors[$error_code] = isset($error_match[2][$index]) ? $error_match[2][$index] :
'';
824 if (empty($result)) {
825 return translate(
'utransact_empty_response_while_processing', $this->_reference_no);
828 preg_match(
'|<InvoiceNumber>(.*?)</InvoiceNumber>|',$result, $match);
830 return isset($match[1]) ? $match[1] :
'' ;
845 if (empty($result)) {
846 return Array(translate(
'utransact_empty_response_while_processing', $this->_reference_no));
852 foreach(Array(
'ReferenceId',
'InvoiceNumber',
'Date',
'Status',
'Amount' ,
'GST',
'uTransactRef',
'MerchantId') as $field) {
855 preg_match(
"|<Transaction xmlns.*?<$field>(.*?)</$field>.*?</Transaction>|msi", $result, $match);
856 $details[$field] = isset($match[1]) ? $match[1] :
'' ;
874 return $this->_response;
887 return $this->_webservice_transactions_url;
901 return $this->_webservice_services_url;
917 $this->_credit_card = $credit_card_no;
932 $this->_credit_card_cv2 = $cv2;
948 $this->_reference_no = $reference_no;
963 $this->_expiry_month = strtoupper($month);
978 $this->_expiry_year = $year;
993 $this->_card_holder_name = $card_holder_name;
1008 $this->_merchant_id = $merchant_id;
1023 $this->_payment_model = $model;
1036 return $this->_reference_no;
1049 return $this->_credit_card;
1062 return $this->_credit_card_cv2;
1075 return $this->_expiry_date;
1088 return $this->_card_holder_name;
1101 return $this->_merchant_id;
1114 return $this->_payment_model;
1129 $this->_first_name = $first_name;
1144 $this->_surname = $surname;
1159 $this->_email = $email;
1174 $this->_address_line_1 = $address_1;
1189 $this->_address_line_2 = $address_2;
1204 $this->_suburb = $suburb;
1219 $this->_city= $suburb;
1234 $this->_postcode = $postcode;
1247 return $this->_first_name;
1260 return $this->_surname;
1273 return $this->_email;
1286 return $this->_address_line_1;
1299 return $this->_address_line_2;
1312 return $this->_suburb;
1325 return $this->_city;
1338 return $this->_postcode;
1353 $this->_reference_id = $ref_id;
1368 $this->_application_username = $username;
1383 $this->_name = $name;
1398 $this->_description = $description;
1413 $this->_quantity = $quantity;
1428 $this->_refund_type = $refund_type;
1443 $this->_refund_expiry_date = $refund_expiry_date;
1458 $this->_product_code = $product_code;
1473 $this->_financial_code = $financial_code;
1488 $this->_financial_values = $financial_values;
1503 $this->_additional_info = $additional_info;
1526 function addCartItem($application_username, $name, $description, $quantity, $refund_type, $refund_expiry_date, $amount, $product_code, $financial_code, $financial_values, $additional_info)
1529 "ApplicationUsername" => $application_username,
1531 "Description" => $description,
1532 "Quantity" => $quantity,
1533 "RefundType" => $refund_type,
1534 "RefundExpiryDate" => $refund_expiry_date,
1535 "Amount" => $amount,
1536 "ProductCode" => $product_code,
1537 "FinancialCode" => $financial_code,
1538 "FinancialValues" => $financial_values,
1539 "AdditionalInfo" => $additional_info,
1543 $this->_cart_items[] = $cart_item;
1558 $this->_cart_items = Array();
1570 return $this->_cart_items;
1582 return $this->_expiry_month;
1595 return $this->_expiry_year;
1608 return $this->_card_types;
1621 return $this->_test_card_numbers;
1632 private function _logRequest()
1634 $message =
"\nRequest:\n";
1635 $message .=
'Time: '.date(
'r').
"\n";
1636 $message .=
'Card holder name: '.$this->_card_holder_name.
"\n";
1637 $message .=
'Card number: ****-****-****-'.substr($this->_credit_card, -4).
"\n";
1638 $message .=
'Amount: '.array_get_index($this->_response,
'AMOUNT',
'').
"\n";
1639 $this->_log($message);
1650 private function _logResponse()
1652 $message =
"\nResponse:\n";
1653 $message .=
'Transaction Ref: '.$this->_reference_no.
"\n";
1654 $message .=
'Invoice No.: '.array_get_index($this->_response,
'INVOICE_NUMBER',
'').
"\n";
1655 $message .=
'UTransct Ref: '.array_get_index($this->_response,
'UTRANSACT_REFERENCE',
'').
"\n";
1656 $message .=
'Merchant Id: '.array_get_index($this->_response,
'MERCHANT_ID',
'').
"\n";
1657 $message .=
'Time: '.array_get_index($this->_response,
'DATE', date(
'r')).
"\n";
1658 $message .=
'Status: '.array_get_index($this->_response,
'STATUS',
'').
"\n";
1659 $message .=
'Message: '.array_get_index($this->_response,
'MESSAGE',
'').
"\n";
1660 $this->_log($message);
1675 private function _log($message, $level = E_USER_NOTICE, $encode=FALSE)
1677 log_write($message, $this->_log_file_name, $level, $encode);