|
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 Exception; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Wrapper to simplify the creation of SIM data |
|
20
|
|
|
* |
|
21
|
|
|
* @author John Conde <[email protected]> |
|
22
|
|
|
* @copyright John Conde <[email protected]> |
|
23
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
|
24
|
|
|
* @link https://github.com/stymiee/authnetjson |
|
25
|
|
|
*/ |
|
26
|
|
|
class AuthnetSim |
|
27
|
|
|
{ |
|
|
|
|
|
|
28
|
|
|
/** |
|
|
|
|
|
|
29
|
|
|
* @var string Authorize.Net API login ID |
|
30
|
|
|
*/ |
|
31
|
|
|
private $login; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
|
|
|
|
|
34
|
|
|
* @var string Authorize.Net API signature key |
|
35
|
|
|
*/ |
|
36
|
|
|
private $signature; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
|
|
|
|
|
39
|
|
|
* @var string URL endpoint for processing a transaction |
|
40
|
|
|
*/ |
|
41
|
|
|
private $url; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
|
|
|
|
|
44
|
|
|
* @var int Randomly generated number |
|
45
|
|
|
*/ |
|
46
|
|
|
private $sequence; |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
|
|
|
|
|
49
|
|
|
* @var int Unix timestamp the request was made |
|
50
|
|
|
*/ |
|
51
|
|
|
private $timestamp; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Creates a SIM wrapper by setting the Authorize.Net credentials and URL of the endpoint to be used |
|
|
|
|
|
|
55
|
|
|
* for the API call |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $login Authorize.Net API login ID |
|
|
|
|
|
|
58
|
|
|
* @param string $signature Authorize.Net API Transaction Key |
|
|
|
|
|
|
59
|
|
|
* @param string $api_url URL endpoint for processing a transaction |
|
|
|
|
|
|
60
|
|
|
* @throws Exception |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function __construct($login, $signature, $api_url) |
|
|
|
|
|
|
63
|
|
|
{ |
|
|
|
|
|
|
64
|
1 |
|
$this->login = $login; |
|
65
|
1 |
|
$this->signature = $signature; |
|
66
|
1 |
|
$this->url = $api_url; |
|
|
|
|
|
|
67
|
1 |
|
$this->resetParameters(); |
|
68
|
1 |
|
} |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the hash for the SIM transaction |
|
72
|
|
|
* |
|
73
|
|
|
* @param float $amount The amount of the transaction |
|
|
|
|
|
|
74
|
|
|
* @return string Hash of five different unique transaction parameters |
|
|
|
|
|
|
75
|
|
|
* @throws AuthnetInvalidAmountException |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function getFingerprint(float $amount): string |
|
78
|
|
|
{ |
|
|
|
|
|
|
79
|
2 |
|
if (!filter_var($amount, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND)) { |
|
|
|
|
|
|
80
|
1 |
|
throw new AuthnetInvalidAmountException('You must enter a valid amount greater than zero.'); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return strtoupper( |
|
84
|
1 |
|
hash_hmac('sha512', |
|
85
|
1 |
|
sprintf('%s^%s^%s^%s^', |
|
86
|
1 |
|
$this->login, |
|
87
|
1 |
|
$this->sequence, |
|
88
|
1 |
|
$this->timestamp, |
|
89
|
1 |
|
$amount |
|
90
|
|
|
), |
|
91
|
1 |
|
hex2bin($this->signature) |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the sequence generated for a transaction |
|
98
|
|
|
* |
|
99
|
|
|
* @return int Current sequence |
|
|
|
|
|
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getSequence(): int |
|
102
|
|
|
{ |
|
|
|
|
|
|
103
|
1 |
|
return $this->sequence; |
|
104
|
|
|
} |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the timestamp for a transaction |
|
108
|
|
|
* |
|
109
|
|
|
* @return int Current timestamp |
|
|
|
|
|
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function getTimestamp(): int |
|
112
|
|
|
{ |
|
|
|
|
|
|
113
|
1 |
|
return $this->timestamp; |
|
114
|
|
|
} |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the account login ID |
|
118
|
|
|
* |
|
119
|
|
|
* @return string API login ID |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function getLogin(): string |
|
122
|
|
|
{ |
|
|
|
|
|
|
123
|
1 |
|
return $this->login; |
|
124
|
|
|
} |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the url endpoint for the transaction |
|
128
|
|
|
* |
|
129
|
|
|
* @return string url endpoint |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getEndpoint(): string |
|
132
|
|
|
{ |
|
|
|
|
|
|
133
|
1 |
|
return $this->url; |
|
134
|
|
|
} |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Resets the sequence and timestamp |
|
138
|
|
|
* |
|
139
|
|
|
* @throws Exception |
|
|
|
|
|
|
140
|
|
|
*/ |
|
|
|
|
|
|
141
|
1 |
|
public function resetParameters(): void |
|
142
|
|
|
{ |
|
|
|
|
|
|
143
|
1 |
|
$this->sequence = random_int(1, 1000); |
|
144
|
1 |
|
$this->timestamp = time(); |
|
145
|
1 |
|
} |
|
|
|
|
|
|
146
|
|
|
} |
|
|
|
|
|
|
147
|
|
|
|