|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Squareetlabs\AuthorizeNet; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use net\authorize\api\constants\ANetEnvironment; |
|
8
|
|
|
use net\authorize\api\contract\v1 as AnetAPI; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AuthorizeNet |
|
11
|
|
|
{ |
|
12
|
|
|
protected ?AnetAPI\MerchantAuthenticationType $merchantAuthentication = null; |
|
13
|
|
|
|
|
14
|
|
|
protected mixed $request = null; |
|
15
|
|
|
|
|
16
|
|
|
protected ?string $refId = null; |
|
17
|
|
|
|
|
18
|
|
|
protected ?AnetAPI\TransactionRequestType $transactionType = null; |
|
19
|
|
|
|
|
20
|
|
|
public Authenticatable|Model $user; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(Authenticatable|Model $user) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->user = $user; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Setup and get merchant authentication keys. |
|
29
|
|
|
* |
|
30
|
|
|
* @return AnetAPI\MerchantAuthenticationType |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getMerchantAuthentication(): AnetAPI\MerchantAuthenticationType |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->merchantAuthentication === null) { |
|
36
|
|
|
$this->merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); |
|
37
|
|
|
$this->merchantAuthentication->setName($this->getLoginId()); |
|
|
|
|
|
|
38
|
|
|
$this->merchantAuthentication->setTransactionKey($this->getTransactionKey()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->merchantAuthentication; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get login ID from configuration. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getLoginId(): string |
|
51
|
|
|
{ |
|
52
|
|
|
$loginId = config('authorizenet.login_id'); |
|
53
|
|
|
|
|
54
|
|
|
if (!$loginId) { |
|
55
|
|
|
throw new \Exception('Please provide Login ID in .env file. Which you can get from authorize.net'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $loginId; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get transaction key from configuration. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
* @throws \Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getTransactionKey(): string |
|
68
|
|
|
{ |
|
69
|
|
|
$transactionKey = config('authorizenet.transaction_key'); |
|
70
|
|
|
|
|
71
|
|
|
if (!$transactionKey) { |
|
72
|
|
|
throw new \Exception('Please provide transaction key in .env file. Which you can get from authorize.net'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $transactionKey; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set the request object. |
|
80
|
|
|
* |
|
81
|
|
|
* @param mixed $requestObject |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setRequest(mixed $requestObject): self |
|
85
|
|
|
{ |
|
86
|
|
|
$this->request = $requestObject; |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the request object. |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getRequest(): mixed |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->request; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set the reference ID. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $refId |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setRefId(string $refId): self |
|
108
|
|
|
{ |
|
109
|
|
|
$this->refId = $refId; |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the reference ID, or generate one if not set. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getRefId(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->refId ?? (string) time(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set the transaction type and amount. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $type |
|
128
|
|
|
* @param int $amount Amount in cents |
|
129
|
|
|
* @return $this |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setTransactionType(string $type, int $amount): self |
|
132
|
|
|
{ |
|
133
|
|
|
$this->transactionType = new AnetAPI\TransactionRequestType(); |
|
134
|
|
|
$this->transactionType->setTransactionType($type); |
|
135
|
|
|
$this->transactionType->setAmount($this->convertCentsToDollar($amount)); |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the transaction type. |
|
142
|
|
|
* |
|
143
|
|
|
* @return AnetAPI\TransactionRequestType|null |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getTransactionType(): ?AnetAPI\TransactionRequestType |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->transactionType; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Convert cents to dollars. |
|
152
|
|
|
* |
|
153
|
|
|
* @param int $cents |
|
154
|
|
|
* @return float |
|
155
|
|
|
*/ |
|
156
|
|
|
public function convertCentsToDollar(int $cents): float |
|
157
|
|
|
{ |
|
158
|
|
|
return $cents / 100; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Convert dollars to cents. |
|
163
|
|
|
* |
|
164
|
|
|
* @param float|int $dollars |
|
165
|
|
|
* @return int |
|
166
|
|
|
*/ |
|
167
|
|
|
public function convertDollarsToCents(float|int $dollars): int |
|
168
|
|
|
{ |
|
169
|
|
|
return (int) ($dollars * 100); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Execute the controller with the appropriate environment. |
|
174
|
|
|
* |
|
175
|
|
|
* @param mixed $controller |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
*/ |
|
178
|
|
|
public function execute(mixed $controller): mixed |
|
179
|
|
|
{ |
|
180
|
|
|
$env = config('app.env'); |
|
181
|
|
|
|
|
182
|
|
|
return match ($env) { |
|
183
|
|
|
'testing', 'local' => $controller->executeWithApiResponse(ANetEnvironment::SANDBOX), |
|
184
|
|
|
default => $controller->executeWithApiResponse(ANetEnvironment::PRODUCTION), |
|
185
|
|
|
}; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Execute controller with sandbox environment (for testing). |
|
190
|
|
|
* |
|
191
|
|
|
* @param mixed $controller |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
|
|
public function testingResponse(mixed $controller): mixed |
|
195
|
|
|
{ |
|
196
|
|
|
return $controller->executeWithApiResponse(ANetEnvironment::SANDBOX); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get the Authorize.Net environment based on app environment. |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getANetEnv(): string |
|
205
|
|
|
{ |
|
206
|
|
|
$env = config('app.env'); |
|
207
|
|
|
|
|
208
|
|
|
return in_array($env, ['testing', 'local']) |
|
209
|
|
|
? ANetEnvironment::SANDBOX |
|
210
|
|
|
: ANetEnvironment::PRODUCTION; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.