Skip to content

Commit 4f6f2aa

Browse files
author
yildiray.itisgen
committed
Added request params mask.
1 parent b5822e5 commit 4f6f2aa

File tree

9 files changed

+267
-95
lines changed

9 files changed

+267
-95
lines changed

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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ 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;
1619
}
1720

1821
/**
@@ -23,6 +26,22 @@ public function getProcessName(): string
2326
return self::USERNAME_AUT;
2427
}
2528

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'];
43+
}
44+
2645
/**
2746
* @param $data
2847
* @return AuthorizeResponse
@@ -35,5 +54,6 @@ protected function createResponse($data): AuthorizeResponse
3554

3655
return $response;
3756
}
57+
3858
}
3959

src/Messages/CaptureRequest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ class CaptureRequest extends AbstractRequest
1313
public function getData(): array
1414
{
1515
$data = $this->getAuthorizeRequestParams();
16-
$data['Transaction']['Type'] = 'postauth';
16+
$data['Transaction']['Type'] = $this->getProcessType();
1717
$data['Card']['CVV2'] = $this->getCard()->getCvv();
1818

19+
$this->setRequestParams($data);
20+
1921
return $data;
2022
}
2123

@@ -27,6 +29,22 @@ public function getProcessName(): string
2729
return self::USERNAME_AUT;
2830
}
2931

32+
/**
33+
* @return string
34+
*/
35+
public function getProcessType(): string
36+
{
37+
return 'postauth';
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function getSensitiveData(): array
44+
{
45+
return ['Number', 'CVV2', 'ExpireDate'];
46+
}
47+
3048
/**
3149
* @param $data
3250
* @return CaptureResponse

0 commit comments

Comments
 (0)