1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
|
|
|
|
6
|
|
|
* This file is part of the AuthnetJSON package. |
7
|
|
|
* |
8
|
|
|
* (c) John Conde <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Authnetjson; |
15
|
|
|
|
16
|
|
|
use \Curl\Curl; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Creates a request to the Authorize.Net JSON endpoints. |
20
|
|
|
* |
21
|
|
|
* @package AuthnetJSON |
22
|
|
|
* @author John Conde <[email protected]> |
23
|
|
|
* @copyright John Conde <[email protected]> |
24
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
25
|
|
|
* @link https://github.com/stymiee/authnetjson |
26
|
|
|
* @see https://developer.authorize.net/api/reference/ |
27
|
|
|
* |
28
|
|
|
* @method AuthnetJsonResponse createTransactionRequest(array $array) |
29
|
|
|
* @method AuthnetJsonResponse sendCustomerTransactionReceiptRequest(array $array) |
30
|
|
|
* @method AuthnetJsonResponse ARBCancelSubscriptionRequest(array $array) |
31
|
|
|
* @method AuthnetJsonResponse ARBCreateSubscriptionRequest(array $array) |
32
|
|
|
* @method AuthnetJsonResponse ARBGetSubscriptionStatusRequest(array $array) |
33
|
|
|
* @method AuthnetJsonResponse ARBUpdateSubscriptionRequest(array $array) |
34
|
|
|
* @method AuthnetJsonResponse getAUJobDetailsRequest(array $array) |
35
|
|
|
* @method AuthnetJsonResponse getAUJobSummaryRequest(array $array) |
36
|
|
|
* @method AuthnetJsonResponse createCustomerPaymentProfileRequest(array $array) |
37
|
|
|
* @method AuthnetJsonResponse createCustomerProfileRequest(array $array) |
38
|
|
|
* @method AuthnetJsonResponse createCustomerProfileTransactionRequest_authCapture(array $array) |
39
|
|
|
* @method AuthnetJsonResponse createCustomerProfileTransactionRequest_authOnly(array $array) |
40
|
|
|
* @method AuthnetJsonResponse createCustomerProfileTransactionRequest_captureOnly(array $array) |
41
|
|
|
* @method AuthnetJsonResponse createCustomerProfileTransactionRequest_priorAuthCapture(array $array) |
|
|
|
|
42
|
|
|
* @method AuthnetJsonResponse createCustomerProfileTransactionRequest_refund(array $array) |
43
|
|
|
* @method AuthnetJsonResponse createCustomerProfileTransactionRequest_void(array $array) |
44
|
|
|
* @method AuthnetJsonResponse createCustomerShippingAddressRequest(array $array) |
45
|
|
|
* @method AuthnetJsonResponse deleteCustomerPaymentProfileRequest(array $array) |
46
|
|
|
* @method AuthnetJsonResponse deleteCustomerProfileRequest(array $array) |
47
|
|
|
* @method AuthnetJsonResponse deleteCustomerShippingAddressRequest(array $array) |
48
|
|
|
* @method AuthnetJsonResponse getCustomerPaymentProfileRequest(array $array) |
49
|
|
|
* @method AuthnetJsonResponse getCustomerProfileIdsRequest(array $array) |
50
|
|
|
* @method AuthnetJsonResponse getCustomerProfileRequest(array $array) |
51
|
|
|
* @method AuthnetJsonResponse getCustomerShippingAddressRequest(array $array) |
52
|
|
|
* @method AuthnetJsonResponse getHostedPaymentPageRequest(array $array) |
53
|
|
|
* @method AuthnetJsonResponse getHostedProfilePageRequest(array $array) |
54
|
|
|
* @method AuthnetJsonResponse getMerchantDetailsRequest(array $array) |
55
|
|
|
* @method AuthnetJsonResponse getUnsettledTransactionListRequest(array $array) |
56
|
|
|
* @method AuthnetJsonResponse updateCustomerPaymentProfileRequest(array $array) |
57
|
|
|
* @method AuthnetJsonResponse updateCustomerProfileRequest(array $array) |
58
|
|
|
* @method AuthnetJsonResponse updateCustomerShippingAddressRequest(array $array) |
59
|
|
|
* @method AuthnetJsonResponse updateHeldTransactionRequest(array $array) |
60
|
|
|
* @method AuthnetJsonResponse updateSplitTenderGroupRequest(array $array) |
61
|
|
|
* @method AuthnetJsonResponse validateCustomerPaymentProfileRequest(array $array) |
62
|
|
|
* @method AuthnetJsonResponse getBatchStatisticsRequest(array $array) |
63
|
|
|
* @method AuthnetJsonResponse getSettledBatchListRequest(array $array) |
64
|
|
|
* @method AuthnetJsonResponse getTransactionDetailsRequest(array $array) |
65
|
|
|
* @method AuthnetJsonResponse getTransactionListRequest(array $array) |
66
|
|
|
*/ |
67
|
|
|
class AuthnetJsonRequest |
68
|
|
|
{ |
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var int Maximum number of retires making HTTP request before failure |
71
|
|
|
*/ |
72
|
|
|
private const MAX_RETRIES = 3; |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @var string Authorize.Net API login ID |
76
|
|
|
*/ |
77
|
|
|
private $login; |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @var string Authorize.Net API Transaction Key |
81
|
|
|
*/ |
82
|
|
|
private $transactionKey; |
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
|
|
|
|
85
|
|
|
* @var string URL endpoint for processing a transaction |
86
|
|
|
*/ |
87
|
|
|
private $url; |
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @var string JSON formatted API request |
91
|
|
|
*/ |
92
|
|
|
private $requestJson; |
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var object Wrapper object representing an endpoint |
96
|
|
|
*/ |
97
|
|
|
private $processor; |
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
|
|
|
|
100
|
|
|
* @var int Counts number of retries to make API call. Up to self::MAX_RETRIES |
101
|
|
|
*/ |
102
|
|
|
private $retries; |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used |
|
|
|
|
106
|
|
|
* for the API call. |
107
|
|
|
* |
108
|
|
|
* @param string $login Authorize.Net API login ID |
|
|
|
|
109
|
|
|
* @param string $transactionKey Authorize.Net API Transaction Key |
|
|
|
|
110
|
|
|
* @param string $api_url URL endpoint for processing a transaction |
|
|
|
|
111
|
|
|
*/ |
112
|
2 |
|
public function __construct(string $login, string $transactionKey, string $api_url) |
|
|
|
|
113
|
|
|
{ |
|
|
|
|
114
|
2 |
|
$this->login = $login; |
115
|
2 |
|
$this->transactionKey = $transactionKey; |
116
|
2 |
|
$this->url = $api_url; |
|
|
|
|
117
|
2 |
|
} |
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Outputs the account credentials, endpoint URL, and request JSON in a human readable format |
121
|
|
|
* |
122
|
|
|
* @return string HTML table containing debugging information |
123
|
|
|
*/ |
124
|
1 |
|
public function __toString() |
125
|
|
|
{ |
|
|
|
|
126
|
1 |
|
$output = '<table id="authnet-request">'."\n"; |
|
|
|
|
127
|
1 |
|
$output .= '<caption>Authorize.Net Request</caption>'."\n"; |
|
|
|
|
128
|
1 |
|
$output .= '<tr><th colspan="2"><b>Class Parameters</b></th></tr>'."\n"; |
|
|
|
|
129
|
1 |
|
$output .= '<tr><td><b>API Login ID</b></td><td>'.$this->login.'</td></tr>'."\n"; |
|
|
|
|
130
|
1 |
|
$output .= '<tr><td><b>Transaction Key</b></td><td>'.$this->transactionKey.'</td></tr>'."\n"; |
|
|
|
|
131
|
1 |
|
$output .= '<tr><td><b>Authnet Server URL</b></td><td>'.$this->url.'</td></tr>'."\n"; |
|
|
|
|
132
|
1 |
|
$output .= '<tr><th colspan="2"><b>Request JSON</b></th></tr>'."\n"; |
|
|
|
|
133
|
1 |
|
if (!empty($this->requestJson)) { |
|
|
|
|
134
|
1 |
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
|
|
|
135
|
1 |
|
$output .= $this->requestJson."\n"; |
|
|
|
|
136
|
1 |
|
$output .= '</pre></td></tr>'."\n"; |
|
|
|
|
137
|
|
|
} |
|
|
|
|
138
|
1 |
|
$output .= '</table>'; |
139
|
|
|
|
140
|
1 |
|
return $output; |
141
|
|
|
} |
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* The __set() method should never be used as all values to be made in the API call must be passed as an array |
|
|
|
|
145
|
|
|
* |
146
|
|
|
* @param string $name unused |
|
|
|
|
147
|
|
|
* @param mixed $value unused |
|
|
|
|
148
|
|
|
* @throws AuthnetCannotSetParamsException |
|
|
|
|
149
|
|
|
*/ |
|
|
|
|
150
|
2 |
|
public function __set($name, $value) |
|
|
|
|
151
|
|
|
{ |
|
|
|
|
152
|
2 |
|
throw new AuthnetCannotSetParamsException(sprintf('You cannot set parameters directly in %s.', __CLASS__)); |
|
|
|
|
153
|
|
|
} |
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Magic method that dynamically creates our API call based on the name of the method in the client code and |
|
|
|
|
157
|
|
|
* the array passed as its parameter. |
158
|
|
|
* |
159
|
|
|
* @param string $api_call name of the API call to be made |
|
|
|
|
160
|
|
|
* @param array $args the array to be passed to the API |
|
|
|
|
161
|
|
|
* @return AuthnetJsonResponse |
|
|
|
|
162
|
|
|
* @throws AuthnetCurlException |
|
|
|
|
163
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
164
|
|
|
*/ |
165
|
2 |
|
public function __call($api_call, array $args) |
|
|
|
|
166
|
|
|
{ |
|
|
|
|
167
|
|
|
$authentication = [ |
168
|
|
|
'merchantAuthentication' => [ |
|
|
|
|
169
|
2 |
|
'name' => $this->login, |
|
|
|
|
170
|
2 |
|
'transactionKey' => $this->transactionKey, |
|
|
|
|
171
|
|
|
] |
|
|
|
|
172
|
|
|
]; |
|
|
|
|
173
|
2 |
|
$call = []; |
|
|
|
|
174
|
2 |
|
if (count($args)) { |
175
|
2 |
|
$call = $args[0]; |
176
|
|
|
} |
|
|
|
|
177
|
|
|
$parameters = [ |
|
|
|
|
178
|
2 |
|
$api_call => $authentication + $call |
|
|
|
|
179
|
|
|
]; |
|
|
|
|
180
|
2 |
|
$this->requestJson = json_encode($parameters); |
181
|
|
|
|
182
|
2 |
|
$response = $this->process(); |
183
|
2 |
|
return new AuthnetJsonResponse($response); |
184
|
|
|
} |
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Makes POST request with retry logic. |
188
|
|
|
*/ |
|
|
|
|
189
|
3 |
|
private function makeRequest(): void |
|
|
|
|
190
|
|
|
{ |
|
|
|
|
191
|
3 |
|
$this->retries = 0; |
192
|
3 |
|
while ($this->retries < self::MAX_RETRIES) { |
193
|
3 |
|
$this->processor->post($this->url, $this->requestJson); |
194
|
3 |
|
if (!$this->processor->error) { |
|
|
|
|
195
|
2 |
|
break; |
196
|
|
|
} |
|
|
|
|
197
|
1 |
|
$this->retries++; |
198
|
|
|
} |
199
|
3 |
|
} |
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Tells the handler to make the API call to Authorize.Net. |
203
|
|
|
* |
204
|
|
|
* @return string JSON string containing API response |
205
|
|
|
* @throws AuthnetCurlException |
|
|
|
|
206
|
|
|
*/ |
207
|
73 |
|
private function process(): string |
|
|
|
|
208
|
|
|
{ |
|
|
|
|
209
|
73 |
|
$this->makeRequest(); |
210
|
73 |
|
if (!$this->processor->error && isset($this->processor->response)) { |
|
|
|
|
211
|
71 |
|
return $this->processor->response; |
212
|
|
|
} |
|
|
|
|
213
|
2 |
|
$error_message = null; |
|
|
|
|
214
|
2 |
|
$error_code = null; |
|
|
|
|
215
|
2 |
|
if ($this->processor->error_code || $this->processor->error_message) { |
216
|
1 |
|
$error_message = $this->processor->error_message; |
|
|
|
|
217
|
1 |
|
$error_code = $this->processor->error_code; |
|
|
|
|
218
|
|
|
} |
|
|
|
|
219
|
2 |
|
throw new AuthnetCurlException(sprintf('Connection error: %s (%s)', $error_message, $error_code)); |
|
|
|
|
220
|
|
|
} |
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Sets the handler to be used to handle our API call. Mainly used for unit testing as Curl is used by default. |
|
|
|
|
224
|
|
|
* |
225
|
|
|
* @param Curl $processor |
|
|
|
|
226
|
|
|
*/ |
|
|
|
|
227
|
2 |
|
public function setProcessHandler($processor): void |
|
|
|
|
228
|
|
|
{ |
|
|
|
|
229
|
2 |
|
$this->processor = $processor; |
230
|
2 |
|
} |
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Gets the request sent to Authorize.Net in JSON format for logging purposes. |
234
|
|
|
* |
235
|
|
|
* @return string transaction request sent to Authorize.Net in JSON format |
236
|
|
|
*/ |
237
|
1 |
|
public function getRawRequest(): string |
238
|
|
|
{ |
|
|
|
|
239
|
1 |
|
return $this->requestJson; |
240
|
|
|
} |
|
|
|
|
241
|
|
|
} |
|
|
|
|
242
|
|
|
|