Completed
Pull Request — master (#114)
by Naman
30:46
created

AuthorizeNetTD::getTransactionList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Easily interact with the Authorize.Net Transaction Details XML API.
4
 *
5
 * @package    AuthorizeNet
6
 * @subpackage AuthorizeNetTD
7
 * @link       http://www.authorize.net/support/ReportingGuide_XML.pdf Transaction Details XML Guide
8
 */
9
10
11
/**
12
 * A class to send a request to the Transaction Details XML API.
13
 *
14
 * @package    AuthorizeNet
15
 * @subpackage AuthorizeNetTD
16
 */ 
17
class AuthorizeNetTD extends AuthorizeNetRequest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
20
    const LIVE_URL = "https://api2.authorize.net/xml/v1/request.api";
21
    const SANDBOX_URL = "https://apitest.authorize.net/xml/v1/request.api";
22
    
23
    private $_xml;
24
    
25
    /**
26
     * This function returns information about a settled batch: Batch ID, Settlement Time, & 
27
     * Settlement State. If you specify includeStatistics, you also receive batch statistics 
28
     * by payment type.
29
     *
30
     *
31
     * The detault date range is one day (the previous 24 hour period). The maximum date range is 31 
32
     * days. The merchant time zone is taken into consideration when calculating the batch date range, 
33
     * unless the Z is specified in the first and last settlement date
34
     *
35
     * @param bool   $includeStatistics
36
     * @param string $firstSettlementDate //  yyyy-mmddTHH:MM:SS
37
     * @param string $lastSettlementDate  //  yyyy-mmddTHH:MM:SS
38
     * @param bool   $utc                 //  Use UTC instead of merchant time zone setting
39
     *
40
     * @return AuthorizeNetTD_Response
41
     */
42 1
    public function getSettledBatchList($includeStatistics = false, $firstSettlementDate = false, $lastSettlementDate = false, $utc = true)
43
    {
44 1
        $utc = ($utc ? "Z" : "");
45 1
        $this->_constructXml("getSettledBatchListRequest");
46 1
        ($includeStatistics ?
47 1
        $this->_xml->addChild("includeStatistics", $includeStatistics) : null);
48 1
        ($firstSettlementDate ?
49 1
        $this->_xml->addChild("firstSettlementDate", $firstSettlementDate . $utc) : null);
50 1
        ($lastSettlementDate ?
51 1
        $this->_xml->addChild("lastSettlementDate", $lastSettlementDate . $utc) : null);
52 1
        return $this->_sendRequest();
53
    }
54
    
55
    /**
56
     * Return all settled batches for a certain month.
57
     *
58
     * @param int $month
59
     * @param int $year
60
     *
61
     * @return AuthorizeNetTD_Response
62
     */
63
    public function getSettledBatchListForMonth($month = false, $year = false)
64
    {
65
        $month = ($month ? $month : date('m'));
66
        $year = ($year ? $year : date('Y'));
67
        $firstSettlementDate = substr(date('c',mktime(0, 0, 0, $month, 1, $year)),0,-6);
68
        $lastSettlementDate  = substr(date('c',mktime(0, 0, 0, $month+1, 0, $year)),0,-6);
69
        return $this->getSettledBatchList(true, $firstSettlementDate, $lastSettlementDate);
70
    }
71
72
    /**
73
     * This function returns limited transaction details for a specified batch ID
74
     *
75
     * @param int $batchId
76
     *
77
     * @return AuthorizeNetTD_Response
78
     */
79
    public function getTransactionList($batchId)
80
    {
81
        $this->_constructXml("getTransactionListRequest");
82
        $this->_xml->addChild("batchId", $batchId);
83
        return $this->_sendRequest();
84
    }
85
    
86
    /**
87
     * Return all transactions for a certain day.
88
     *
89
     * @param int $month
90
     * @param int $day
91
     * @param int $year
92
     *
93
     * @return array Array of SimpleXMLElments
94
     */
95 1
    public function getTransactionsForDay($month = false, $day = false, $year = false)
96
    {
97 1
        $transactions = array();
98 1
        $month = ($month ? $month : date('m'));
99 1
        $day = ($day ? $day : date('d'));
100 1
        $year = ($year ? $year : date('Y'));
101 1
        $firstSettlementDate = substr(date('c',mktime(0, 0, 0, (int)$month, (int)$day, (int)$year)),0,-6);
102 1
        $lastSettlementDate  = substr(date('c',mktime(0, 0, 0, (int)$month, (int)$day, (int)$year)),0,-6);
103 1
        $response = $this->getSettledBatchList(true, $firstSettlementDate, $lastSettlementDate);
104 1
        $batches = $response->xpath("batchList/batch");
105 1
        foreach ($batches as $batch) {
106
            $batch_id = (string)$batch->batchId;
107
            $request = new AuthorizeNetTD;
108
            $tran_list = $request->getTransactionList($batch_id);
109
            $transactions = array_merge($transactions, $tran_list->xpath("transactions/transaction"));
110
        }
111 1
        return $transactions;
112
    }
113
114
    /**
115
     * This function returns full transaction details for a specified transaction ID.
116
     *
117
     * @param int $transId
118
     *
119
     * @return AuthorizeNetTD_Response
120
     */    
121
    public function getTransactionDetails($transId)
122
    {
123
        $this->_constructXml("getTransactionDetailsRequest");
124
        $this->_xml->addChild("transId", $transId);
125
        return $this->_sendRequest();
126
    }
127
    
128
    /**
129
     * This function returns statistics about the settled batch specified by $batchId.
130
     *
131
     * @param int $batchId
132
     *
133
     * @return AuthorizeNetTD_Response
134
     */
135
    public function getBatchStatistics($batchId)
136
    {
137
        $this->_constructXml("getBatchStatisticsRequest");
138
        $this->_xml->addChild("batchId", $batchId);
139
        return $this->_sendRequest();
140
    }
141
    
142
    /**
143
     * This function returns the last 1000 unsettled transactions.
144
     *
145
     *
146
     * @return AuthorizeNetTD_Response
147
     */
148
    public function getUnsettledTransactionList()
149
    {
150
        $this->_constructXml("getUnsettledTransactionListRequest");
151
        return $this->_sendRequest();
152
    }
153
    
154
    /**
155
     * @return string
156
     */
157 1
    protected function _getPostUrl()
158
    {
159 1
        return ($this->_sandbox ? self::SANDBOX_URL : self::LIVE_URL);
160
    }
161
    
162
    /**
163
     *
164
     *
165
     * @param string $response
166
     * 
167
     * @return AuthorizeNetTransactionDetails_Response
168
     */
169 1
    protected function _handleResponse($response)
170
    {
171 1
        return new AuthorizeNetTD_Response($response);
172
    }
173
    
174
    /**
175
     * Prepare the XML post string.
176
     */
177 1
    protected function _setPostString()
178
    {
179 1
        $this->_post_string = $this->_xml->asXML();
180
        
181 1
    }
182
    
183
    /**
184
     * Start the SimpleXMLElement that will be posted.
185
     *
186
     * @param string $request_type The action to be performed.
187
     */
188 1
    private function _constructXml($request_type)
189
    {
190 1
        $string = '<?xml version="1.0" encoding="utf-8"?><'.$request_type.' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"></'.$request_type.'>';
191 1
        $this->_xml = @new SimpleXMLElement($string);
192 1
        $merchant = $this->_xml->addChild('merchantAuthentication');
193 1
        $merchant->addChild('name',$this->_api_login);
194 1
        $merchant->addChild('transactionKey',$this->_transaction_key);
195 1
    }
196
    
197
}
198
199
/**
200
 * A class to parse a response from the Transaction Details XML API.
201
 *
202
 * @package    AuthorizeNet
203
 * @subpackage AuthorizeNetTD
204
 */
205
class AuthorizeNetTD_Response extends AuthorizeNetXMLResponse
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
206
{
207
    
208
209
}
210