Skip to content

Commit 87847f3

Browse files
committed
Clean up of controllers
1 parent a57b448 commit 87847f3

12 files changed

+198
-79
lines changed

classmap.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
return array(
1717

18-
'Doctrine\Common\Annotations\Annotation' => $vendorDir . '/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation',
1918
'Doctrine\Common\Annotations\Annotation' => $vendorDir . '/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation.php',
2019
'Doctrine\Common\Annotations\AnnotationException' => $vendorDir . '/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php',
2120
'Doctrine\Common\Annotations\AnnotationReader' => $vendorDir . '/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php',
@@ -161,8 +160,6 @@
161160
'Metadata\Driver\FileLocatorInterface' => $vendorDir . '/jms/metadata/src/Metadata/Driver/FileLocatorInterface.php',
162161
'Metadata\Driver\LazyLoadingDriver' => $vendorDir . '/jms/metadata/src/Metadata/Driver/LazyLoadingDriver.php',
163162
'Metadata\AdvancedMetadataFactoryInterface' => $vendorDir . '/jms/metadata/src/Metadata/AdvancedMetadataFactoryInterface.php',
164-
'Metadata\ClassHierarchyMetadata' => $vendorDir . '/jms/metadata/src/Metadata/ClassHierarchyMetadata.php',
165-
'Metadata\ClassMetadata' => $vendorDir . '/jms/metadata/src/Metadata/ClassMetadata.php',
166163
'Metadata\MergeableClassMetadata' => $vendorDir . '/jms/metadata/src/Metadata/MergeableClassMetadata.php',
167164
'Metadata\MergeableInterface' => $vendorDir . '/jms/metadata/src/Metadata/MergeableInterface.php',
168165
'Metadata\MetadataFactory' => $vendorDir . '/jms/metadata/src/Metadata/MetadataFactory.php',
@@ -217,12 +214,19 @@
217214
'AuthorizeNetSubscriptionListSorting' => $sharedDir . 'AuthorizeNetTypes.php',
218215
'AuthorizeNetSubscriptionListPaging' => $sharedDir . 'AuthorizeNetTypes.php',
219216

217+
// Following section contains the new controller model classes needed
218+
//Utils
220219
//'net\authorize\util\ObjectToXml' => $libDir . 'net/authorize/util/ObjectToXml.php',
221220
'net\authorize\util\HttpClient' => $libDir . 'net/authorize/util/HttpClient.php',
222221

222+
//constants
223+
'net\authorize\api\constants\ANetEnvironment' => $libDir . 'net/authorize/api/constants/ANetEnvironment.php',
224+
225+
//base classes
226+
'net\authorize\api\controller\base\IApiOperation' => $libDir . 'net/authorize/api/controller/base/IApiOperation.php',
223227
'net\authorize\api\controller\base\ApiOperationBase' => $libDir . 'net/authorize/api/controller/base/ApiOperationBase.php',
224228

225-
// 'net\authorize\api\contract\v1\AccountTypeEnum' => $libDir . 'net/authorize/api/contract/v1/AccountTypeEnum.php',
229+
//following are generated class mappings
226230
'net\authorize\api\contract\v1\ANetApiRequestType' => $libDir . 'net/authorize/api/contract/v1/ANetApiRequestType.php',
227231
'net\authorize\api\contract\v1\ANetApiResponseType' => $libDir . 'net/authorize/api/contract/v1/ANetApiResponseType.php',
228232
'net\authorize\api\contract\v1\ARBCancelSubscriptionRequest' => $libDir . 'net/authorize/api/contract/v1/ARBCancelSubscriptionRequest.php',
@@ -393,4 +397,7 @@
393397
'net\authorize\api\contract\v1\TransactionResponseType\MessagesAType\MessageAType' => $libDir . 'net/authorize/api/contract/v1/TransactionResponseType/MessagesAType/MessageAType.php',
394398
'net\authorize\api\contract\v1\TransactionResponseType\SplitTenderPaymentsAType\SplitTenderPaymentAType' => $libDir . 'net/authorize/api/contract/v1/TransactionResponseType/SplitTenderPaymentsAType/SplitTenderPaymentAType.php',
395399

400+
//Controllers
401+
'net\authorize\api\controller\LogoutController' => $libDir . 'net/authorize/api/controller/LogoutController.php',
402+
396403
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace net\authorize\api\constants;
3+
4+
class ANetEnvironment
5+
{
6+
const CUSTOM = "http://ww730vsmbu114.visa.com";
7+
const SANDBOX = "https://apitest.authorize.net";
8+
const PRODUCTION = "https://api.authorize.net";
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1-
Controller.php for: Logout Something to test
1+
<?php
2+
namespace net\authorize\api\controller;
23

4+
use net\authorize\api\contract\v1\AnetApiRequestType;
5+
use net\authorize\api\controller\base\ApiOperationBase;
6+
7+
class LogoutController extends ApiOperationBase
8+
{
9+
public function __construct(AnetApiRequestType $request)
10+
{
11+
$responseType = 'net\authorize\api\contract\v1\LogoutResponse';
12+
parent::__construct($request, $responseType);
13+
}
14+
15+
protected function validateRequest()
16+
{
17+
//validate required fields of $this->apiRequest->
18+
19+
//validate non-required fields of $this->apiRequest->
20+
}
21+
}

lib/net/authorize/api/controller/base/ApiOperationBase.php

+38-11
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22
namespace net\authorize\api\controller\base;
33

44
use InvalidArgumentException;
5-
use HttpException;
6-
//use net\authorize\util\ObjectToXml;
7-
//use stdClass;
85
use JMS\Serializer\SerializerBuilder;
96
use JMS\Serializer\handler\HandlerRegistryInterface;
107
use Goetas\Xsd\XsdToPhp\Jms\Handler\BaseTypesHandler;
118
use Goetas\Xsd\XsdToPhp\Jms\Handler\XmlSchemaDateHandler;
129

1310
use \net\authorize\util\HttpClient;
1411

15-
//use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16-
17-
class ApiOperationBase
12+
abstract class ApiOperationBase implements IApiOperation
1813
{
1914
/**
2015
* @var \net\authorize\api\contract\v1\AnetApiRequestType
@@ -67,6 +62,9 @@ public function __construct(\net\authorize\api\contract\v1\AnetApiRequestType $r
6762
}
6863

6964
$this->apiRequest = $request;
65+
66+
self::validate();
67+
7068
$this->apiResponseType = $responseType;
7169
$this->httpClient = new HttpClient;
7270

@@ -84,28 +82,57 @@ function (HandlerRegistryInterface $h)
8482
);
8583
$this->serializer = $serializerBuilder->build();
8684
}
87-
85+
86+
/**
87+
* Retrieves response
88+
* @return \net\authorize\api\contract\v1\AnetApiResponseType
89+
*/
90+
public function getApiResponse()
91+
{
92+
return $this->apiResponse;
93+
}
94+
8895
/**
8996
* Sends request and retrieves response
9097
* @return \net\authorize\api\contract\v1\AnetApiResponseType
9198
*/
92-
public function executeWithApiResponse()
99+
public function executeWithApiResponse($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
93100
{
94-
$this->execute();
101+
$this->execute($endPoint);
95102
return $this->apiResponse;
96103
}
97104

98-
public function execute()
105+
public function execute($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
99106
{
107+
$this->beforeExecute();
108+
100109
$xmlRequest = $this->serializer->serialize($this->apiRequest, 'xml');
101110
/*
102111
//$xmlRequest = '<?xml version="1.0" encoding="UTF-8"?> <ARBGetSubscriptionListRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"> <merchantAuthentication> <name>4YJmeW7V77us</name> <transactionKey>4qHK9u63F753be4Z</transactionKey> </merchantAuthentication> <refId><![CDATA[ref1416999093]]></refId> <searchType><![CDATA[subscriptionActive]]></searchType> <sorting> <orderBy><![CDATA[firstName]]></orderBy> <orderDescending>false</orderDescending> </sorting> <paging> <limit>10</limit> <offset>1</offset> </paging> </ARBGetSubscriptionListRequest> ';
103112
*/
113+
$this->httpClient->setPostUrl( $endPoint);
104114
$xmlResponse = $this->httpClient->_sendRequest($xmlRequest);
105115
if ( null == $xmlResponse)
106116
{
107-
throw new HttpException( "response from api is null");
117+
throw new \Exception( "Error getting valid response from api. Check log file for error details");
108118
}
109119
$this->apiResponse = $this->serializer->deserialize( $xmlResponse, $this->apiResponseType , 'xml');
120+
121+
$this->afterExecute();
122+
}
123+
124+
private function validate()
125+
{
126+
$merchantAuthentication = $this->apiRequest->getMerchantAuthentication();
127+
if ( null == $merchantAuthentication)
128+
{
129+
throw new \InvalidArgumentException( "MerchantAuthentication cannot be null");
130+
}
131+
132+
$this->validateRequest();
110133
}
134+
135+
protected function beforeExecute() {}
136+
protected function afterExecute() {}
137+
protected function validateRequest() {} //need to make this abstract
111138
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace net\authorize\api\controller\base;
3+
4+
interface IApiOperation
5+
{
6+
public function getApiResponse();
7+
public function executeWithApiResponse( $endPoint = null);
8+
public function execute( $endPoint = null);
9+
/*
10+
//TS GetApiResponse();
11+
AuthorizeNet.Api.Contracts.V1.ANetApiResponse GetErrorResponse();
12+
//TS ExecuteWithApiResponse(AuthorizeNet.Environment environment = null);
13+
//void Execute(AuthorizeNet.Environment environment = null);
14+
AuthorizeNet.Api.Contracts.V1.messageTypeEnum GetResultCode();
15+
List<string> GetResults();
16+
*/
17+
}

lib/net/authorize/util/HttpClient.php

+42-37
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<?php
22
namespace net\authorize\util;
33

4-
/**
5-
* Easily interact with the Authorize.Net XML API.
6-
*
7-
* @package AuthorizeNet
8-
* @subpackage AuthorizeNetARB
9-
* @link http://www.authorize.net/support/ARB_guide.pdf ARB Guide
10-
*/
4+
//use \net\authorize\api\constants\ANetEnvironment;
115

126
/**
137
* A class to send a request to the XML API.
@@ -17,42 +11,39 @@
1711
*/
1812
class HttpClient
1913
{
20-
const URL = "https://apitest.authorize.net/xml/v1/request.api";
21-
14+
//const URL = "https://apitest.authorize.net/xml/v1/request.api";
15+
private $_Url = "";
2216

2317
public $VERIFY_PEER = true; // attempt trust validation of SSL certificates when establishing secure connections.
2418
private $_log_file = false;
2519

2620
/**
2721
* Constructor.
2822
*
29-
* @param string $api_login_id The Merchant's API Login ID.
30-
* @param string $transaction_key The Merchant's Transaction Key.
3123
*/
32-
public function __construct()//$api_login_id = false, $transaction_key = false)
24+
public function __construct()
3325
{
34-
//$this->_api_login = ($api_login_id ? $api_login_id : (defined('AUTHORIZENET_API_LOGIN_ID') ? AUTHORIZENET_API_LOGIN_ID : ""));
35-
//$this->_transaction_key = ($transaction_key ? $transaction_key : (defined('AUTHORIZENET_TRANSACTION_KEY') ? AUTHORIZENET_TRANSACTION_KEY : ""));
36-
//$this->_sandbox = (defined('AUTHORIZENET_SANDBOX') ? AUTHORIZENET_SANDBOX : true);
37-
//$this->_log_file = (defined('AUTHORIZENET_LOG_FILE') ? AUTHORIZENET_LOG_FILE : false);
3826
$this->_log_file = AUTHORIZENET_LOG_FILE;
27+
date_default_timezone_set('UTC');
3928
}
4029

41-
/*
42-
public function cancelSubscription($subscriptionId)
43-
{
44-
$this->_request_type = "CancelSubscriptionRequest";
45-
$this->_request_payload .= "<subscriptionId>$subscriptionId</subscriptionId>";
46-
return $this->_sendRequest();
47-
}
48-
*/
30+
/**
31+
* Set a log file.
32+
*
33+
* @param string $endPoint end point to hit from \net\authorize\api\constants\ANetEnvironment
34+
*/
35+
public function setPostUrl( $endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
36+
{
37+
$this->_Url = sprintf( "%s/xml/v1/request.api", $endPoint);
38+
}
4939

5040
/**
5141
* @return string
5242
*/
5343
public function _getPostUrl()
5444
{
55-
return (self::URL);
45+
//return (self::URL);
46+
return ($this->_Url);
5647
}
5748

5849
/**
@@ -66,12 +57,15 @@ public function setLogFile($filepath)
6657
}
6758

6859
/**
69-
* Posts the request to AuthorizeNet & returns response.
60+
* Posts the request to AuthorizeNet endpoint using Curl & returns response.
7061
*
71-
* @return $xmlResponse The response.
62+
* @param $xmlRequest
63+
* @return bool|mixed $xmlResponse The response.
7264
*/
7365
public function _sendRequest($xmlRequest)
7466
{
67+
$xmlResponse = "";
68+
7569
$post_url = $this->_getPostUrl();
7670
$curl_request = curl_init($post_url);
7771
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $xmlRequest);
@@ -80,37 +74,48 @@ public function _sendRequest($xmlRequest)
8074
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
8175
curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 2);
8276

83-
file_put_contents($this->_log_file, sprintf("Url: %s", $post_url), FILE_APPEND);
77+
file_put_contents($this->_log_file, sprintf("\n%s: Url: %s", $this->now(), $post_url), FILE_APPEND);
8478
// Do not log requests that could contain CC info.
85-
file_put_contents($this->_log_file, sprintf("Request: %s", $xmlRequest), FILE_APPEND);
79+
file_put_contents($this->_log_file, sprintf("\n%s:Request to AnetApi: \n%s", $this->now(), $xmlRequest), FILE_APPEND);
8680

8781
if ($this->VERIFY_PEER) {
8882
curl_setopt($curl_request, CURLOPT_CAINFO, dirname(dirname(__FILE__)) . '../../../ssl/cert.pem');//..\..\..\ssl\cert.pem
8983
} else {
9084
if ($this->_log_file) {
91-
file_put_contents($this->_log_file, "----Request----\nInvalid SSL option\n", FILE_APPEND);
85+
file_put_contents($this->_log_file, "\nInvalid SSL option for the request", FILE_APPEND);
9286
}
9387
return false;
9488
}
9589

9690
if (preg_match('/xml/',$post_url)) {
9791
curl_setopt($curl_request, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
98-
file_put_contents($this->_log_file, "Sending XML Request type", FILE_APPEND);
92+
file_put_contents($this->_log_file, "\nSending 'XML' Request type", FILE_APPEND);
9993
}
10094

101-
$xmlResponse = curl_exec($curl_request);
102-
file_put_contents($this->_log_file, sprintf("Response: %s", $xmlResponse), FILE_APPEND);
103-
95+
try
96+
{
97+
file_put_contents($this->_log_file, sprintf("\n%s:Sending http request via Curl", $this->now()), FILE_APPEND);
98+
$xmlResponse = curl_exec($curl_request);
99+
file_put_contents($this->_log_file, sprintf("\n%s:Response from AnetApi: \n%s\n", $this->now(), $xmlResponse), FILE_APPEND);
100+
} catch (\Exception $ex)
101+
{
102+
$errorMessage = sprintf("\n%s:Error making http request via curl: Code:'%s', Message:'%s', Trace:'%s', File:'%s':'%s'",
103+
$this->now(), $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), $ex->getFile(), $ex->getLine() );
104+
file_put_contents($this->_log_file, $errorMessage, FILE_APPEND);
105+
}
104106
if ($this->_log_file) {
105-
106107
if ($curl_error = curl_error($curl_request)) {
107-
file_put_contents($this->_log_file, "----CURL ERROR----\n$curl_error\n\n", FILE_APPEND);
108+
file_put_contents($this->_log_file, sprintf("\n%s:CURL ERROR: %s", $this->now(), $curl_error), FILE_APPEND);
108109
}
109110

110111
}
111112
curl_close($curl_request);
112113

113-
//return $this->_handleResponse($response);
114114
return $xmlResponse;
115115
}
116+
117+
private function now()
118+
{
119+
return date( DATE_RFC2822);
120+
}
116121
}

phpunit_config.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
*/
5+
6+
define( "PHPUNIT_TESTSUITE", "true");
7+
define( "AUTHORIZENET_API_LOGIN_ID", "");
8+
define( "AUTHORIZENET_TRANSACTION_KEY", "");
9+
define( "AUTHORIZENET_MD5_SETTING", "");
10+
define( "MERCHANT_LIVE_API_LOGIN_ID", "");
11+
define( "MERCHANT_LIVE_TRANSACTION_KEY", "");
12+
define( "CP_API_LOGIN_ID", "");
13+
define( "CP_TRANSACTION_KEY", "");
14+
define( "AUTHORIZENET_LOG_FILE", "./authorize-net.log");
15+
16+
?>

resources/ControllerTemplate.phpt

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1-
Controller.php for: APICONTROLLERNAME Something to test
1+
<?php
2+
namespace net\authorize\api\controller;
23

4+
use net\authorize\api\contract\v1\AnetApiRequestType;
5+
use net\authorize\api\controller\base\ApiOperationBase;
6+
7+
class APICONTROLLERNAMEController extends ApiOperationBase
8+
{
9+
public function __construct(AnetApiRequestType $request)
10+
{
11+
$responseType = 'net\authorize\api\contract\v1\APICONTROLLERNAMEResponse';
12+
parent::__construct($request, $responseType);
13+
}
14+
15+
protected function validateRequest()
16+
{
17+
//validate required fields of $this->apiRequest->
18+
19+
//validate non-required fields of $this->apiRequest->
20+
}
21+
}

0 commit comments

Comments
 (0)