1 | <?php |
||
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 | * Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used |
||
101 | * for the API call. |
||
102 | * |
||
103 | * @param string $login Authorize.Net API login ID |
||
104 | * @param string $transactionKey Authorize.Net API Transaction Key |
||
105 | * @param string $api_url URL endpoint for processing a transaction |
||
106 | */ |
||
107 | 1 | public function __construct(string $login, string $transactionKey, string $api_url) |
|
113 | |||
114 | /** |
||
115 | * Outputs the account credentials, endpoint URL, and request JSON in a human readable format |
||
116 | * |
||
117 | * @return string HTML table containing debugging information |
||
118 | */ |
||
119 | 1 | public function __toString() |
|
137 | |||
138 | /** |
||
139 | * The __set() method should never be used as all values to be made in the API call must be passed as an array |
||
140 | * |
||
141 | * @param string $name unused |
||
142 | * @param mixed $value unused |
||
143 | * @throws AuthnetCannotSetParamsException |
||
144 | */ |
||
145 | 2 | public function __set($name, $value) |
|
149 | |||
150 | /** |
||
151 | * Magic method that dynamically creates our API call based on the name of the method in the client code and |
||
152 | * the array passed as its parameter. |
||
153 | * |
||
154 | * @param string $api_call name of the API call to be made |
||
155 | * @param array $args the array to be passed to the API |
||
156 | * @return AuthnetJsonResponse |
||
157 | * @throws AuthnetCurlException |
||
158 | * @throws AuthnetInvalidJsonException |
||
159 | */ |
||
160 | 1 | public function __call($api_call, array $args) |
|
180 | |||
181 | /** |
||
182 | * Makes POST request with retry logic. |
||
183 | */ |
||
184 | private function makeRequest() : void |
||
185 | { |
||
186 | $retries = 0; |
||
187 | while ($retries < self::MAX_RETRIES) { |
||
188 | $this->processor->post($this->url, $this->requestJson); |
||
189 | if (!$this->processor->error) { |
||
190 | break; |
||
191 | } |
||
192 | $retries++; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Tells the handler to make the API call to Authorize.Net. |
||
198 | * |
||
199 | * @return string JSON string containing API response |
||
200 | * @throws AuthnetCurlException |
||
201 | */ |
||
202 | 72 | private function process() : string |
|
216 | |||
217 | /** |
||
218 | * Sets the handler to be used to handle our API call. Mainly used for unit testing as Curl is used by default. |
||
219 | * |
||
220 | * @param Curl $processor |
||
221 | */ |
||
222 | 1 | public function setProcessHandler($processor) : void |
|
226 | |||
227 | /** |
||
228 | * Gets the request sent to Authorize.Net in JSON format for logging purposes. |
||
229 | * |
||
230 | * @return string transaction request sent to Authorize.Net in JSON format |
||
231 | */ |
||
232 | 1 | public function getRawRequest() : string |
|
236 | } |
||
237 |