Skip to content

Commit 7a064ae

Browse files
author
Sunny Raj Rathod
committed
Merge pull request #113 from AuthorizeNet/future
Future
2 parents 3a82380 + 1e4c1f7 commit 7a064ae

File tree

11 files changed

+122
-47
lines changed

11 files changed

+122
-47
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "sample-code-php"]
22
path = sample-code-php
3-
url = https://github.com/Authorizenet/sample-code-php.git
3+
url = https://github.com/AuthorizeNet/sample-code-php.git

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ language: php
33
php:
44
- 5.6
55
- 7.0
6-
6+
77
sudo: false
88

99
before_script:
1010
- pecl install xmldiff
1111
- composer install --prefer-dist --ignore-platform-reqs
12-
- chmod +x ./test-sample-codes.sh
13-
- git submodule update --recursive
14-
12+
- git submodule update --remote --recursive
13+
1514
script:
1615
- vendor/phpunit/phpunit/phpunit --coverage-clover=coverage.clover
16+
- cd sample-code-php
17+
- composer install --prefer-dist --ignore-platform-reqs
18+
- vendor/phpunit/phpunit/phpunit test-runner.php .
1719

1820
after_script:
1921
- wget https://scrutinizer-ci.com/ocular.phar
2022
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
21-
- ./test-sample-codes.sh

lib/net/authorize/util/HttpClient.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ class HttpClient
1515
private $_Url = "";
1616

1717
public $VERIFY_PEER = true; // attempt trust validation of SSL certificates when establishing secure connections.
18-
private $_log_file = false;
1918
private $logger = NULL;
2019
/**
2120
* Constructor.
2221
*
2322
*/
2423
public function __construct()
2524
{
26-
$this->_log_file = (defined('AUTHORIZENET_LOG_FILE') ? AUTHORIZENET_LOG_FILE : false);
2725
$this->logger = LogFactory::getLog(get_class($this));
2826
date_default_timezone_set('UTC');
2927
}
@@ -54,7 +52,7 @@ public function _getPostUrl()
5452
*/
5553
public function setLogFile($filepath)
5654
{
57-
$this->_log_file = $filepath;
55+
$this->logger->setLogFile($filepath);
5856
}
5957

6058
/**
@@ -82,7 +80,7 @@ public function _sendRequest($xmlRequest)
8280
if ($this->VERIFY_PEER) {
8381
curl_setopt($curl_request, CURLOPT_CAINFO, dirname(dirname(__FILE__)) . '/../../ssl/cert.pem');
8482
} else {
85-
$this->logger("Invalid SSL option for the request");
83+
$this->logger->error("Invalid SSL option for the request");
8684
return false;
8785
}
8886

@@ -104,7 +102,7 @@ public function _sendRequest($xmlRequest)
104102
$this->now(), $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), $ex->getFile(), $ex->getLine() );
105103
$this->logger->error($errorMessage);
106104
}
107-
if ($this->_log_file) {
105+
if ($this->logger && $this->logger->getLogFile()) {
108106
if ($curl_error = curl_error($curl_request)) {
109107
$this->logger->error("CURL ERROR: $curl_error");
110108
}

lib/net/authorize/util/Log.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use net\authorize\util\ANetSensitiveFields;
55

66
define ("ANET_LOG_FILES_APPEND",true);
7-
define ("ANET_LOG_FILE","phplog");
87

98
define("ANET_LOG_DEBUG_PREFIX","DEBUG");
109
define("ANET_LOG_INFO_PREFIX","INFO");
@@ -30,6 +29,7 @@
3029
class Log
3130
{
3231
private $sensitiveXmlTags = NULL;
32+
private $logFile = '';
3333

3434
/**
3535
* Takes a regex pattern (string) as argument and adds the forward slash delimiter.
@@ -247,6 +247,7 @@ private function getMasked($raw)
247247
}
248248

249249
private function log($logLevelPrefix, $logMessage, $flags){
250+
if (!$this->logFile) return;
250251
//masking
251252
$logMessage = $this->getMasked($logMessage);
252253

@@ -264,7 +265,7 @@ private function log($logLevelPrefix, $logMessage, $flags){
264265
//Add timestamp, log level, method, file, line
265266
$logString = sprintf("\n %s %s : [%s] (%s : %s) - %s", \net\authorize\util\Helpers::now(), $logLevelPrefix,
266267
$methodName, $fileName, $lineNumber, $logMessage);
267-
file_put_contents(ANET_LOG_FILE, $logString, $flags);
268+
file_put_contents($this->logFile, $logString, $flags);
268269
}
269270

270271
public function debug($logMessage, $flags=FILE_APPEND)
@@ -298,9 +299,9 @@ private function logFormat($logLevelPrefix, $format, $objects, $flags){
298299
$objects[$i] = $this->getMasked($testObject);
299300
}
300301
$logMessage = vsprintf($format, $objects);
301-
log($logLevelPrefix, $logMessage, $flags);
302+
$this->log($logLevelPrefix, $logMessage, $flags);
302303
}
303-
catch(Exception $e){
304+
catch(\Exception $e){
304305
$this->debug("Incorrect log message format: " . $e->getMessage());
305306
}
306307
}
@@ -329,6 +330,20 @@ public function errorFormat($format, $args=array(), $flags=FILE_APPEND){
329330
$this->logFormat(ANET_LOG_ERROR_PREFIX, $format, $args , $flags);
330331
}
331332
}
333+
334+
/**
335+
* @param string $logFile
336+
*/
337+
public function setLogFile($logFile){
338+
$this->logFile = $logFile;
339+
}
340+
341+
/**
342+
* @return string
343+
*/
344+
public function getLogFile(){
345+
return $this->logFile;
346+
}
332347

333348
public function __construct(){
334349
$this->sensitiveXmlTags = ANetSensitiveFields::getSensitiveXmlTags();

lib/net/authorize/util/LogFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class LogFactory
77
public static function getLog($classType){
88
if(NULL == self::$logger){
99
self::$logger = new Log();
10+
if(defined('AUTHORIZENET_LOG_FILE')){
11+
self::$logger->setLogFile(AUTHORIZENET_LOG_FILE);
12+
}
1013
}
1114
return self::$logger;
1215
}

lib/shared/AuthorizeNetRequest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
use net\authorize\util\LogFactory;
3+
24
/**
35
* Sends requests to the Authorize.Net gateways.
46
*
@@ -13,7 +15,7 @@ abstract class AuthorizeNetRequest
1315
protected $_post_string;
1416
public $VERIFY_PEER = true; // attempt trust validation of SSL certificates when establishing secure connections.
1517
protected $_sandbox = true;
16-
protected $_log_file = false;
18+
protected $_logger = null;
1719

1820
/**
1921
* Set the _post_string
@@ -42,7 +44,7 @@ public function __construct($api_login_id = false, $transaction_key = false)
4244
$this->_api_login = ($api_login_id ? $api_login_id : (defined('AUTHORIZENET_API_LOGIN_ID') ? AUTHORIZENET_API_LOGIN_ID : ""));
4345
$this->_transaction_key = ($transaction_key ? $transaction_key : (defined('AUTHORIZENET_TRANSACTION_KEY') ? AUTHORIZENET_TRANSACTION_KEY : ""));
4446
$this->_sandbox = (defined('AUTHORIZENET_SANDBOX') ? AUTHORIZENET_SANDBOX : true);
45-
$this->_log_file = (defined('AUTHORIZENET_LOG_FILE') ? AUTHORIZENET_LOG_FILE : false);
47+
$this->_logger = LogFactory::getLog(get_class($this));
4648
}
4749

4850
/**
@@ -62,7 +64,7 @@ public function setSandbox($bool)
6264
*/
6365
public function setLogFile($filepath)
6466
{
65-
$this->_log_file = $filepath;
67+
$this->_logger->setLogFile($filepath);
6668
}
6769

6870
/**
@@ -94,9 +96,9 @@ protected function _sendRequest()
9496
if ($this->VERIFY_PEER) {
9597
curl_setopt($curl_request, CURLOPT_CAINFO, dirname(dirname(__FILE__)) . '/ssl/cert.pem');
9698
} else {
97-
if ($this->_log_file) {
98-
file_put_contents($this->_log_file, "----Request----\nInvalid SSL option\n", FILE_APPEND);
99-
}
99+
if ($this->_logger) {
100+
$this->_logger->error("----Request----\nInvalid SSL option\n");
101+
}
100102
return false;
101103
}
102104

@@ -106,15 +108,13 @@ protected function _sendRequest()
106108

107109
$response = curl_exec($curl_request);
108110

109-
if ($this->_log_file) {
110-
111+
if ($this->_logger) {
111112
if ($curl_error = curl_error($curl_request)) {
112-
file_put_contents($this->_log_file, "----CURL ERROR----\n$curl_error\n\n", FILE_APPEND);
113+
$this->_logger->error("----CURL ERROR----\n$curl_error\n\n");
113114
}
114115
// Do not log requests that could contain CC info.
115-
file_put_contents($this->_log_file, "----Request----\n{$this->_post_string}\n", FILE_APPEND);
116-
117-
file_put_contents($this->_log_file, "----Response----\n$response\n\n", FILE_APPEND);
116+
$this->_logger->info("----Request----\n{$this->_post_string}\n");
117+
$this->_logger->info("----Response----\n$response\n\n");
118118
}
119119
curl_close($curl_request);
120120

tests/AuthorizeNetAIM_Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public function testAimResponseFields()
268268
public function testVoid()
269269
{
270270
// First create transaction to void.
271-
$amount = rand(1, 1000);
271+
$amount = rand(1, 100000);
272272
$sale = new AuthorizeNetAIM;
273273
$sale->setFields(
274274
array(

tests/AuthorizeNetCIM_Test.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ public function testCreateCustomerProfile()
119119
$this->assertEquals($customerProfile->description, (string)$response->xml->profile->description);
120120
$this->assertEquals($customerProfile->merchantCustomerId, (string)$response->xml->profile->merchantCustomerId);
121121
$this->assertEquals($customerProfile->email, (string)$response->xml->profile->email);
122-
$this->assertEquals(substr($customerProfile->paymentProfiles[0]->payment->creditCard->cardNumber, -4, 4), substr((string)$response->xml->profile->paymentProfiles->payment->creditCard->cardNumber, -4, 4));
123-
124-
125-
122+
$testPayment = ($response->xml->profile->paymentProfiles[0]->payment->creditCard)?($response->xml->profile->paymentProfiles[0]->payment):
123+
($response->xml->profile->paymentProfiles[1]->payment);
124+
$this->assertEquals(substr($customerProfile->paymentProfiles[0]->payment->creditCard->cardNumber, -4, 4), substr((string)$testPayment->creditCard->cardNumber, -4, 4));
126125
$this->assertTrue($response->isOk());
127126

128127

@@ -155,7 +154,7 @@ public function testUpdateCustomerPaymentProfile()
155154
$request = new AuthorizeNetCIM;
156155
$customerProfile = new AuthorizeNetCustomer;
157156
$customerProfile->description = "Description of customer";
158-
$customerProfile->merchantCustomerId = time().rand(1,10);
157+
$customerProfile->merchantCustomerId = time().rand(1,100000);
159158
$customerProfile->email = "blahlah@domain.com";
160159
$response = $request->createCustomerProfile($customerProfile);
161160
$this->assertTrue($response->isOk());

tests/bootstrap.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414

1515
// Append to log file
1616
date_default_timezone_set('UTC'); //necessary for the following date to set timezone
17-
file_put_contents(AUTHORIZENET_LOG_FILE, sprintf("Logging Started: %s\n", date( DATE_RFC2822)), FILE_APPEND);
17+
$logMessage = sprintf("Logging Started: %s\n", date(DATE_RFC2822));
18+
if (AUTHORIZENET_LOG_FILE)
19+
{
20+
file_put_contents(AUTHORIZENET_LOG_FILE, $logMessage, FILE_APPEND);
21+
} else {
22+
echo $logMessage;
23+
}
1824

1925
// validate existence of available extensions
2026
if (!function_exists('simplexml_load_file'))

tests/net/authorize/api/controller/ApiCoreTestBase.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public function __construct()
4848
protected function setUp()
4949
{
5050
$logMessage = sprintf("\n%s: Test '%s' Starting.", \net\authorize\util\Helpers::now(), $this->getName());
51-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
51+
echo $logMessage;
52+
if (self::$log_file)
53+
{
54+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
55+
}
5256

5357
$this->refId = 'ref' . time();
5458

@@ -111,7 +115,11 @@ protected function setUp()
111115
protected function tearDown()
112116
{
113117
$logMessage = sprintf("\n%s: Test '%s' Completed.\n", \net\authorize\util\Helpers::now(), $this->getName());
114-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
118+
echo $logMessage;
119+
if (self::$log_file)
120+
{
121+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
122+
}
115123
}
116124

117125
protected function getRandomString( $title)
@@ -137,30 +145,50 @@ protected static function displayMessages( apiContract\ANetApiResponseType $resp
137145
if ( null != $response)
138146
{
139147
$logMessage = sprintf("\n%s: Controller Response is not null, iterating messages.", \net\authorize\util\Helpers::now());
140-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
148+
echo $logMessage;
149+
if (self::$log_file)
150+
{
151+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
152+
}
141153
$msgCount = 0;
142154
$allMessages = $response->getMessages();
143155
if ( null != $allMessages )
144156
{
145157
$logMessage = sprintf("\n%s: Controller ResultCode: '%s'.", \net\authorize\util\Helpers::now(), $allMessages->getResultCode());
146-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
158+
echo $logMessage;
159+
if (self::$log_file)
160+
{
161+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
162+
}
147163
if (null != $allMessages->getMessage())
148164
{
149165
foreach ($allMessages->getMessage() as $message)
150166
{
151167
$msgCount++;
152168
$logMessage = sprintf("\n%d - Message, Code: '%s', Text: '%s'", $msgCount, $message->getCode(), $message->getText());
153-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
169+
echo $logMessage;
170+
if (self::$log_file)
171+
{
172+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
173+
}
154174
}
155175
}
156176
}
157177
$logMessage = sprintf("\n%s: Controller Response contains '%d' messages.", \net\authorize\util\Helpers::now(), $msgCount);
158-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
178+
echo $logMessage;
179+
if (self::$log_file)
180+
{
181+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
182+
}
159183
}
160184
else
161185
{
162186
$logMessage = sprintf("\n%s: Response is null.", \net\authorize\util\Helpers::now());
163-
echo $logMessage; file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
187+
echo $logMessage;
188+
if (self::$log_file)
189+
{
190+
file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
191+
}
164192
}
165193
}
166194

0 commit comments

Comments
 (0)