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 Authnetjson\Exception\AuthnetInvalidCredentialsException; |
17
|
|
|
use Authnetjson\Exception\AuthnetInvalidServerException; |
18
|
|
|
use Curl\Curl; |
19
|
|
|
use Exception; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Factory to instantiate an instance of an AuthnetJson object with the proper endpoint |
23
|
|
|
* URL and Processor Class. |
24
|
|
|
* |
25
|
|
|
* @author John Conde <[email protected]> |
26
|
|
|
* @copyright 2015 - 2023 John Conde <[email protected]> |
27
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
28
|
|
|
* |
29
|
|
|
* @link https://github.com/stymiee/authnetjson |
30
|
|
|
*/ |
31
|
|
|
class AuthnetApiFactory |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @const Indicates use of Authorize.Net's production server |
35
|
|
|
*/ |
36
|
|
|
public const USE_PRODUCTION_SERVER = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @const Indicates use of the development server |
40
|
|
|
*/ |
41
|
|
|
public const USE_DEVELOPMENT_SERVER = 1; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @const Indicates use of the CDN endpoint |
45
|
|
|
*/ |
46
|
|
|
public const USE_CDN_SERVER = 2; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Validates the Authorize.Net credentials and returns a Request object to be used to make an API call. |
50
|
|
|
* |
51
|
|
|
* @param string $login Authorize.Net API Login ID |
52
|
|
|
* @param string $transaction_key Authorize.Net API Transaction Key |
53
|
|
|
* @param int|null $endpoint ID of which endpoint to use (optional) |
54
|
|
|
* @return AuthnetJsonRequest |
55
|
|
|
* @throws AuthnetInvalidCredentialsException |
56
|
|
|
* @throws AuthnetInvalidServerException |
57
|
|
|
*/ |
58
|
6 |
|
public static function getJsonApiHandler(string $login, string $transaction_key, ?int $endpoint = null): object |
59
|
|
|
{ |
60
|
6 |
|
$login = trim($login); |
61
|
6 |
|
$transaction_key = trim($transaction_key); |
62
|
6 |
|
$endpoint = $endpoint ?? self::USE_CDN_SERVER; |
63
|
6 |
|
$api_url = static::getWebServiceURL($endpoint); |
64
|
|
|
|
65
|
5 |
|
if (empty($login) || empty($transaction_key)) { |
66
|
2 |
|
throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
$curl = new Curl(); |
70
|
3 |
|
$curl->setOpt(CURLOPT_RETURNTRANSFER, true); |
71
|
3 |
|
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); |
72
|
3 |
|
$curl->setOpt(CURLOPT_HEADER, false); |
73
|
3 |
|
$curl->setOpt(CURLOPT_TIMEOUT, 10); |
74
|
3 |
|
$curl->setOpt(CURLOPT_CONNECTTIMEOUT, 10); |
75
|
3 |
|
$curl->setHeader('Content-Type', 'text/json'); |
76
|
|
|
|
77
|
3 |
|
$object = new AuthnetJsonRequest($login, $transaction_key, $api_url); |
78
|
3 |
|
$object->setProcessHandler($curl); |
79
|
|
|
|
80
|
3 |
|
return $object; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the API endpoint to be used for a JSON API call. |
85
|
|
|
* |
86
|
|
|
* @param int $server ID of which server to use |
87
|
|
|
* @return string The URL endpoint the request is to be sent to |
88
|
|
|
* @throws AuthnetInvalidServerException |
89
|
|
|
*/ |
90
|
5 |
|
protected static function getWebServiceURL(int $server): string |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$urls = [ |
93
|
5 |
|
static::USE_PRODUCTION_SERVER => 'https://api.authorize.net/xml/v1/request.api', |
94
|
5 |
|
static::USE_DEVELOPMENT_SERVER => 'https://apitest.authorize.net/xml/v1/request.api', |
95
|
5 |
|
static::USE_CDN_SERVER => 'https://api2.authorize.net/xml/v1/request.api', |
96
|
|
|
]; |
97
|
5 |
|
if (array_key_exists($server, $urls)) { |
98
|
4 |
|
return $urls[$server]; |
99
|
|
|
} |
100
|
1 |
|
throw new AuthnetInvalidServerException('You did not provide a valid server.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Validates the Authorize.Net credentials and returns a SIM object to be used to make a SIM API call. |
105
|
|
|
* |
106
|
|
|
* @param string $login Authorize.Net API Login ID |
107
|
|
|
* @param string $transaction_key Authorize.Net API Transaction Key |
108
|
|
|
* @param int|null $server ID of which server to use (optional) |
109
|
|
|
* @return AuthnetSim |
110
|
|
|
* @throws AuthnetInvalidCredentialsException |
111
|
|
|
* @throws AuthnetInvalidServerException |
112
|
|
|
* @throws Exception |
113
|
|
|
*/ |
114
|
2 |
|
public static function getSimHandler(string $login, string $transaction_key, ?int $server = null): object |
115
|
|
|
{ |
116
|
2 |
|
$login = trim($login); |
117
|
2 |
|
$transaction_key = trim($transaction_key); |
118
|
2 |
|
$server = $server ?? self::USE_PRODUCTION_SERVER; |
119
|
2 |
|
$api_url = static::getSimURL($server); |
120
|
|
|
|
121
|
2 |
|
if (empty($login) || empty($transaction_key)) { |
122
|
1 |
|
throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return new AuthnetSim($login, $transaction_key, $api_url); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Gets the API endpoint to be used for a SIM API call. |
130
|
|
|
* |
131
|
|
|
* @param int $server ID of which server to use |
132
|
|
|
* @return string The URL endpoint the request is to be sent to |
133
|
|
|
* @throws AuthnetInvalidServerException |
134
|
|
|
*/ |
135
|
3 |
|
protected static function getSimURL(int $server): string |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$urls = [ |
138
|
3 |
|
static::USE_PRODUCTION_SERVER => 'https://secure2.authorize.net/gateway/transact.dll', |
139
|
3 |
|
static::USE_DEVELOPMENT_SERVER => 'https://test.authorize.net/gateway/transact.dll', |
140
|
|
|
]; |
141
|
3 |
|
if (array_key_exists($server, $urls)) { |
142
|
2 |
|
return $urls[$server]; |
143
|
|
|
} |
144
|
1 |
|
throw new AuthnetInvalidServerException('You did not provide a valid server.'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Validates the Authorize.Net credentials and returns a Webhooks Request object to be used to make a Webhook call. |
149
|
|
|
* |
150
|
|
|
* @param string $login Authorize.Net API Login ID |
151
|
|
|
* @param string $transaction_key Authorize.Net API Transaction Key |
152
|
|
|
* @param int|null $server ID of which server to use (optional) |
153
|
|
|
* @return AuthnetWebhooksRequest |
154
|
|
|
* @throws AuthnetInvalidCredentialsException |
155
|
|
|
* @throws AuthnetInvalidServerException |
156
|
|
|
*/ |
157
|
5 |
|
public static function getWebhooksHandler(string $login, string $transaction_key, ?int $server = null): object |
158
|
|
|
{ |
159
|
5 |
|
$login = trim($login); |
160
|
5 |
|
$transaction_key = trim($transaction_key); |
161
|
5 |
|
$server = $server ?? self::USE_PRODUCTION_SERVER; |
162
|
5 |
|
$api_url = static::getWebhooksURL($server); |
163
|
|
|
|
164
|
4 |
|
if (empty($login) || empty($transaction_key)) { |
165
|
2 |
|
throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.'); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
$base64credentials = base64_encode(sprintf('%s:%s', $login, $transaction_key)); |
169
|
|
|
|
170
|
2 |
|
$curl = new Curl(); |
171
|
2 |
|
$curl->setOpt(CURLOPT_RETURNTRANSFER, true); |
172
|
2 |
|
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); |
173
|
2 |
|
$curl->setOpt(CURLOPT_HEADER, false); |
174
|
2 |
|
$curl->setOpt(CURLOPT_TIMEOUT, 10); |
175
|
2 |
|
$curl->setOpt(CURLOPT_CONNECTTIMEOUT, 10); |
176
|
2 |
|
$curl->setHeader('Content-Type', 'application/json'); |
177
|
2 |
|
$curl->setHeader('Authorization', sprintf('Basic %s', $base64credentials)); |
178
|
|
|
|
179
|
2 |
|
$object = new AuthnetWebhooksRequest($api_url); |
180
|
2 |
|
$object->setProcessHandler($curl); |
181
|
|
|
|
182
|
2 |
|
return $object; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Gets the API endpoint to be used for a SIM API call. |
187
|
|
|
* |
188
|
|
|
* @param int $server ID of which server to use |
189
|
|
|
* @return string The URL endpoint the request is to be sent to |
190
|
|
|
* @throws AuthnetInvalidServerException |
191
|
|
|
*/ |
192
|
3 |
|
protected static function getWebhooksURL(int $server): string |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$urls = [ |
195
|
3 |
|
static::USE_PRODUCTION_SERVER => 'https://api.authorize.net/rest/v1/', |
196
|
3 |
|
static::USE_DEVELOPMENT_SERVER => 'https://apitest.authorize.net/rest/v1/', |
197
|
|
|
]; |
198
|
3 |
|
if (array_key_exists($server, $urls)) { |
199
|
2 |
|
return $urls[$server]; |
200
|
|
|
} |
201
|
1 |
|
throw new AuthnetInvalidServerException('You did not provide a valid server.'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker
.