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