Skip to content

Commit 6e99851

Browse files
authored
Merge pull request #15 from alegraio/testCase
Added testcases.
2 parents 2c749d4 + 37eb90c commit 6e99851

35 files changed

+1667
-217
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"Omnipay\\Gvp\\": "src/"
1010
}
1111
},
12+
"autoload-dev": {
13+
"psr-4": {
14+
"OmnipayTest\\Gvp\\": "tests/"
15+
}
16+
},
1217
"require": {
1318
"php": "^7.3",
1419
"omnipay/common": "^3",

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Omnipay Garanti(Gvp) Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory>./src</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Gateway.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ public function getSecureKey(): string
104104
return $this->getParameter('secureKey');
105105
}
106106

107+
/**
108+
* Default parameters.
109+
*
110+
* @return array
111+
*/
112+
public function getDefaultParameters(): array
113+
{
114+
return [
115+
'merchantId' => '',
116+
'terminalId' => '',
117+
'password' => ''
118+
];
119+
}
120+
107121
/**
108122
* @param array $parameters
109123
* @return AbstractRequest|RequestInterface

src/Mask.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Omnipay\Gvp;
4+
5+
class Mask
6+
{
7+
/**
8+
* @param string $value
9+
* @param null $maskSymbol
10+
* @param int $showLast
11+
* @return string
12+
*/
13+
public static function mask(string $value, $maskSymbol = null, $showLast = 3): string
14+
{
15+
$maskSymbol = $maskSymbol ?: 'X';
16+
$showLast = max(0, $showLast);
17+
18+
if (false === $showLast || mb_strlen($value) <= ($showLast + 1) * 2) {
19+
$showRegExpPart = "";
20+
} else {
21+
$showRegExpPart = "(?!(.){0,$showLast}$)";
22+
}
23+
24+
return preg_replace("/(?!^.?)[^-_\s]$showRegExpPart/u", $maskSymbol, $value);
25+
}
26+
}

src/Messages/AbstractRequest.php

Lines changed: 97 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
use Omnipay\Common\Exception\InvalidResponseException;
99
use Omnipay\Common\Message\ResponseInterface;
10+
use Omnipay\Gvp\Mask;
11+
use Omnipay\Gvp\RequestInterface;
1012

11-
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
13+
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest implements RequestInterface
1214
{
1315
/** @var string */
1416
protected const USERNAME_AUT = 'PROVAUT';
@@ -36,6 +38,8 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
3638
'JPY' => 392
3739
];
3840

41+
protected $requestParams;
42+
3943
/**
4044
* @return string
4145
*/
@@ -256,44 +260,44 @@ protected function getTransactionHashRefundAndCancel(): string
256260
$this->getSecurityHash())));
257261
}
258262

263+
259264
/**
260265
* @return array
261-
* @throws \Omnipay\Common\Exception\InvalidRequestException
262266
*/
263267
protected function getSalesRequestParams(): array
264268
{
265269
$data = $this->getInfo();
266-
$data['Card'] = array(
270+
$data['Card'] = [
267271
'Number' => $this->getCard()->getNumber(),
268272
'ExpireDate' => $this->getCard()->getExpiryDate('my'),
269273
'CVV2' => $this->getCard()->getCvv()
270-
);
274+
];
271275

272-
$data['Order'] = array(
276+
$data['Order'] = [
273277
'OrderID' => $this->getOrderId()
274-
);
278+
];
275279

276-
$data['Customer'] = array(
280+
$data['Customer'] = [
277281
'IPAddress' => $this->getClientIp(),
278282
'EmailAddress' => $this->getCard()->getEmail()
279-
);
283+
];
280284

281285
$data['Terminal'] = [
282-
'ProvUserID' => self::USERNAME_AUT,
286+
'ProvUserID' => $this->getProcessName(),
283287
'HashData' => $this->getTransactionHash(),
284-
'UserID' => self::USERNAME_AUT,
288+
'UserID' => $this->getProcessName(),
285289
'ID' => $this->getTerminalId(),
286290
'MerchantID' => $this->getMerchantId()
287291
];
288292

289-
$data['Transaction'] = array(
290-
'Type' => 'sales',
293+
$data['Transaction'] = [
294+
'Type' => $this->getProcessType(),
291295
'InstallmentCnt' => $this->getInstallment(),
292296
'Amount' => $this->getAmountInteger(),
293297
'CurrencyCode' => $this->currency_list[$this->getCurrency()],
294-
'CardholderPresentCode' => "0",
295-
'MotoInd' => "N"
296-
);
298+
'CardholderPresentCode' => '0',
299+
'MotoInd' => 'N'
300+
];
297301

298302
return $data;
299303
}
@@ -306,28 +310,28 @@ protected function getCompleteSalesRequestParams(): array
306310
{
307311

308312
$data = $this->getInfo();
309-
$data['Order'] = array(
313+
$data['Order'] = [
310314
'OrderID' => $this->getOrderId()
311-
);
315+
];
312316

313-
$data['Customer'] = array(
317+
$data['Customer'] = [
314318
'IPAddress' => $this->getClientIp(),
315-
);
319+
];
316320

317321
$data['Terminal'] = [
318-
'ProvUserID' => self::USERNAME_AUT,
322+
'ProvUserID' => $this->getProcessName(),
319323
'HashData' => $this->getTransactionHashWithoutCardNumber(),
320-
'UserID' => self::USERNAME_AUT,
324+
'UserID' => $this->getProcessName(),
321325
'ID' => $this->getTerminalId(),
322326
'MerchantID' => $this->getMerchantId()
323327
];
324328

325-
$data['Transaction'] = array(
326-
'Type' => 'sales',
329+
$data['Transaction'] = [
330+
'Type' => $this->getProcessType(),
327331
'Amount' => $this->getAmountInteger(),
328332
'CurrencyCode' => $this->currency_list[$this->getCurrency()],
329-
'MotoInd' => "N"
330-
);
333+
'MotoInd' => 'N'
334+
];
331335

332336
return $data;
333337
}
@@ -339,46 +343,48 @@ protected function getAuthorizeRequestParams(): array
339343
{
340344
$data = $this->getInfo();
341345
$data['Terminal'] = [
342-
'ProvUserID' => self::USERNAME_AUT,
346+
'ProvUserID' => $this->getProcessName(),
343347
'HashData' => $this->getTransactionHash(),
344-
'UserID' => self::USERNAME_AUT,
348+
'UserID' => $this->getProcessName(),
345349
'ID' => $this->getTerminalId(),
346350
'MerchantID' => $this->getMerchantId()
347351
];
348-
$data['Customer'] = array(
352+
$data['Customer'] = [
349353
'IPAddress' => $this->getClientIp(),
350354
'EmailAddress' => $this->getCard()->getEmail()
351-
);
352-
$data['Card'] = array(
355+
];
356+
357+
$data['Card'] = [
353358
'Number' => $this->getCard()->getNumber(),
354359
'ExpireDate' => $this->getCard()->getExpiryDate('my')
355-
);
356-
$data['Order'] = array(
360+
];
361+
362+
$data['Order'] = [
357363
'OrderID' => $this->getOrderId()
358-
);
359-
$data['Transaction'] = array(
360-
'Type' => 'preauth',
364+
];
365+
366+
$data['Transaction'] = [
367+
'Type' => $this->getProcessType(),
361368
'InstallmentCnt' => $this->getInstallment(),
362369
'Amount' => $this->getAmountInteger(),
363370
'CurrencyCode' => $this->currency_list[$this->getCurrency()],
364-
'CardholderPresentCode' => "0",
365-
'MotoInd' => "N"
366-
);
371+
'CardholderPresentCode' => '0',
372+
'MotoInd' => 'N'
373+
];
367374

368375
return $data;
369376
}
370377

371378
/**
372379
* @return array
373-
* @throws \Omnipay\Common\Exception\InvalidRequestException
374380
*/
375381
protected function getSalesRequestParamsFor3d(): array
376382
{
377383
$expiryYear = \DateTime::createFromFormat('Y', $this->getCard()->getExpiryYear());
378384
$params['apiversion'] = $this->version;
379385
$params['mode'] = $this->getTestMode() ? 'TEST' : 'PROD';
380-
$params['terminalprovuserid'] = self::USERNAME_AUT;
381-
$params['terminaluserid'] = self::USERNAME_AUT;
386+
$params['terminalprovuserid'] = $this->getProcessName();
387+
$params['terminaluserid'] = $this->getProcessName();
382388
$params['terminalid'] = $this->getTerminalId();
383389
$params['terminalmerchantid'] = $this->getMerchantId();
384390
$params['txntype'] = 'sales';
@@ -405,14 +411,62 @@ protected function getSalesRequestParamsFor3d(): array
405411
return $params;
406412
}
407413

414+
/**
415+
* @return array
416+
*/
417+
protected function getRefundRequestParams(): array
418+
{
419+
$data = $this->getInfo();
420+
$data['Terminal'] = [
421+
'ProvUserID' => $this->getProcessName(),
422+
'HashData' => $this->getTransactionHashRefundAndCancel(),
423+
'UserID' => $this->getProcessName(),
424+
'ID' => $this->getTerminalId(),
425+
'MerchantID' => $this->getMerchantId()
426+
];
427+
428+
$data['Customer'] = [
429+
'IPAddress' => $this->getClientIp()
430+
];
431+
432+
$data['Order'] = [
433+
'OrderID' => $this->getOrderId()
434+
];
435+
436+
$data['Transaction'] = [
437+
'Type' => $this->getProcessType(),
438+
'Amount' => $this->getAmountInteger(),
439+
'CurrencyCode' => $this->currency_list[$this->getCurrency()]
440+
];
441+
442+
return $data;
443+
}
444+
445+
protected function setRequestParams(array $data): void
446+
{
447+
array_walk_recursive($data, [$this, 'updateValue']);
448+
$this->requestParams = $data;
449+
}
450+
451+
protected function updateValue(&$data, $key): void
452+
{
453+
$sensitiveData = $this->getSensitiveData();
454+
455+
if (\in_array($key, $sensitiveData, true)) {
456+
$data = Mask::mask($data);
457+
}
458+
459+
}
460+
408461
/**
409462
* @return array
410463
*/
411464
protected function getRequestParams(): array
412465
{
413466
return [
414467
'url' => $this->getEndPoint(),
415-
'data' => $this->getData(),
468+
'type' => $this->getProcessType(),
469+
'data' => $this->requestParams,
416470
'method' => $this->getHttpMethod()
417471
];
418472
}
@@ -428,7 +482,7 @@ private function getSecurityHash(): string
428482
/**
429483
* @return array
430484
*/
431-
private function getInfo(): array
485+
protected function getInfo(): array
432486
{
433487
$data['Version'] = $this->version;
434488
$data['Mode'] = $this->getTestMode() ? 'TEST' : 'PROD';

src/Messages/AuthorizeRequest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,34 @@ class AuthorizeRequest extends AbstractRequest
1212
*/
1313
public function getData(): array
1414
{
15-
return $this->getAuthorizeRequestParams();
15+
$data = $this->getAuthorizeRequestParams();
16+
$this->setRequestParams($data);
17+
18+
return $data;
19+
}
20+
21+
/**
22+
* @return string
23+
*/
24+
public function getProcessName(): string
25+
{
26+
return self::USERNAME_AUT;
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getProcessType(): string
33+
{
34+
return 'preauth';
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function getSensitiveData(): array
41+
{
42+
return ['Number', 'ExpireDate'];
1643
}
1744

1845
/**
@@ -27,5 +54,6 @@ protected function createResponse($data): AuthorizeResponse
2754

2855
return $response;
2956
}
57+
3058
}
3159

0 commit comments

Comments
 (0)