-
Notifications
You must be signed in to change notification settings - Fork 35
/
PayeezyAbstractRequest.php
364 lines (325 loc) · 9.03 KB
/
PayeezyAbstractRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* First Data Payeezy Abstract Request
*/
namespace Omnipay\FirstData\Message;
/**
* First Data Payeezy Abstract Request
*/
abstract class PayeezyAbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
/** @var string Method used to calculate the hmac strings. */
const METHOD_POST = 'POST';
/** @var string Content type use to calculate the hmac string */
const CONTENT_TYPE = 'application/json; charset=UTF-8';
/** API version to use. See the note about the hashing requirements for v12 or higher. */
const API_VERSION = 'v14';
/** @var string live endpoint URL base */
protected $liveEndpoint = 'https://api.globalgatewaye4.firstdata.com/transaction/';
/** @var string test endpoint URL base */
protected $testEndpoint = 'https://api.demo.globalgatewaye4.firstdata.com/transaction/';
/** @var int api transaction type */
protected $transactionType = '00';
//
// Transaction types
//
const TRAN_PURCHASE = '00';
const TRAN_PREAUTH = '01';
const TRAN_PREAUTHCOMPLETE = '02';
const TRAN_FORCEDPOST = '03';
const TRAN_REFUND = '04';
const TRAN_PREAUTHONLY = '05';
const TRAN_PAYPALORDER = '07';
const TRAN_VOID = '13';
const TRAN_TAGGEDPREAUTHCOMPLETE = '32';
const TRAN_TAGGEDVOID = '33';
const TRAN_TAGGEDREFUND = '34';
const TRAN_CASHOUT = '83';
const TRAN_ACTIVATION = '85';
const TRAN_BALANCEINQUIRY = '86';
const TRAN_RELOAD = '88';
const TRAN_DEACTIVATION = '89';
/** @var array Names of the credit card types. */
protected static $cardTypes = array(
'visa' => 'Visa',
'mastercard' => 'Mastercard',
'discover' => 'Discover',
'amex' => 'American Express',
'diners_club' => 'Diners Club',
'jcb' => 'JCB',
);
/**
* Get Gateway ID
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return string
*/
public function getGatewayId()
{
return $this->getParameter('gatewayId');
}
/**
* Set Gateway ID
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return PayeezyAbstractRequest provides a fluent interface.
*/
public function setGatewayId($value)
{
return $this->setParameter('gatewayId', $value);
}
/**
* Get Password
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return string
*/
public function getPassword()
{
return $this->getParameter('password');
}
/**
* Set Password
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return PayeezyAbstractRequest provides a fluent interface.
*/
public function setPassword($value)
{
return $this->setParameter('password', $value);
}
/**
* Get Key Id
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return mixed
*/
public function getKeyId()
{
return $this->getParameter('keyId');
}
/**
* Set Key Id
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return PayeezyAbstractRequest provides a fluent interface.
*/
public function setKeyId($value)
{
return $this->setParameter('keyId', $value);
}
/**
* Get Hmac
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return mixed
*/
public function getHmac()
{
return $this->getParameter('hmac');
}
/**
* Set Hmac
*
* Calls to the Payeezy Gateway API are secured with a gateway ID and
* password.
*
* @return PayeezyAbstractRequest provides a fluent interface.
*/
public function setHmac($value)
{
return $this->setParameter('hmac', $value);
}
/**
* Set transaction type
*
* @param int $transactionType
*
* @return PayeezyAbstractRequest provides a fluent interface.
*/
public function setTransactionType($transactionType)
{
$this->transactionType = $transactionType;
return $this;
}
/**
* Get transaction type
*
* @return int
*/
public function getTransactionType()
{
return $this->transactionType;
}
/**
* Get the base transaction data.
*
* @return array
*/
protected function getBaseData()
{
$data = array();
$data['gateway_id'] = $this->getGatewayID();
$data['password'] = $this->getPassword();
$data['transaction_type'] = $this->getTransactionType();
return $data;
}
/**
* Get the transaction headers.
*
* @return array
*/
protected function getHeaders()
{
return array(
'Content-Type' => self::CONTENT_TYPE,
'Accept' => 'application/json'
);
}
/**
* @return string
*/
protected function getGge4Date()
{
return gmdate("Y-m-d") . 'T' . gmdate("H:i:s") . 'Z';
}
/**
* Composes the hash string needed for the newer versions of the API
*
* @param $contentDigest
* @param $gge4Date
* @param $uri
*
* @return string
*/
protected function buildHashString($contentDigest, $gge4Date, $uri)
{
return sprintf(
"%s\n%s\n%s\n%s\n%s",
self::METHOD_POST,
self::CONTENT_TYPE,
$contentDigest,
$gge4Date,
$uri
);
}
/**
* Composes the auth string needed for the newer versions of the API
*
* @param $hashString
*
* @return string
*/
protected function buildAuthString($hashString)
{
return sprintf(
'GGE4_API %s:%s',
$this->getKeyId(),
base64_encode(hash_hmac("sha1", $hashString, $this->getHmac(), true))
);
}
/**
* Get the card type name, from the card type code.
*
* @param string $type
*
* @return string
*/
public static function getCardType($type)
{
if (isset(self::$cardTypes[$type])) {
return self::$cardTypes[$type];
}
return $type;
}
/**
* Get the AVS Hash.
*
* Important Note about v12 or higher of the Web Service API: Merchants wishing to use
* V12 or higher of the API must implement the API HMAC hash security calculation.
* Further information on this subject can be found at the link below.
*
* @link https://support.payeezy.com/entries/22069302-api-security-hmac-hash
*
* @return string
*/
public function getAVSHash()
{
$parts = array();
$parts[] = $this->getCard()->getAddress1();
$parts[] = $this->getCard()->getPostcode();
$parts[] = $this->getCard()->getCity();
$parts[] = $this->getCard()->getState();
$parts[] = $this->getCard()->getCountry();
return implode('|', $parts);
}
/**
* @return array
*/
public function getData()
{
$this->setTransactionType($this->action);
$data = $this->getBaseData();
return $data;
}
/**
* @param mixed $data
*
* @return PayeezyResponse
*/
public function sendData($data)
{
$headers = $this->getHeaders();
$gge4Date = $this->getGge4Date();
$endpoint = $this->getEndpoint();
$url = parse_url($endpoint);
$contentDigest = sha1(json_encode($data));
$hashString = $this->buildHashString($contentDigest, $gge4Date, $url['path']);
$authString = $this->buildAuthString($hashString);
$headers["X-GGe4-Content-SHA1"] = $contentDigest;
$headers["X-GGe4-Date"] = $gge4Date;
$headers["Authorization"] = $authString;
$client = $this->httpClient->post(
$endpoint,
$headers
);
$client->setBody(json_encode($data), $headers['Content-Type']);
$client->getCurlOptions()->set(CURLOPT_PORT, 443);
$httpResponse = $client->send();
return $this->createResponse($httpResponse->getBody());
}
/**
* Get the endpoint URL for the request.
*
* @return string
*/
protected function getEndpoint()
{
return ($this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint) . self::API_VERSION;
}
/**
* Create the response object.
*
* @param $data
*
* @return PayeezyResponse
*/
protected function createResponse($data)
{
return $this->response = new PayeezyResponse($this, $data);
}
}