Skip to content

Commit

Permalink
Merge pull request #5 from alegraio/test-dev
Browse files Browse the repository at this point in the history
Test dev
  • Loading branch information
cansozeri authored Nov 20, 2020
2 parents b3c8cf0 + 9eb8be3 commit 358b0a4
Show file tree
Hide file tree
Showing 23 changed files with 535 additions and 193 deletions.
25 changes: 7 additions & 18 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
namespace Omnipay\NestPay;

use Omnipay\Common\Message\NotificationInterface;
use Omnipay\NestPay\Messages\PreAuthorizeRequest;
use Omnipay\NestPay\Messages\CompletePurchaseRequest;
use Omnipay\NestPay\Messages\PurchaseRequest;
use Omnipay\Common\AbstractGateway;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\RequestInterface;
use Omnipay\NestPay\Messages\AuthorizeRequest;
use Omnipay\NestPay\Messages\CaptureRequest;
Expand Down Expand Up @@ -126,7 +124,7 @@ public function getStoreKey(): string

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function authorize(array $parameters = []): RequestInterface
{
Expand All @@ -135,16 +133,7 @@ public function authorize(array $parameters = []): RequestInterface

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
*/
public function preAuthorize(array $parameters = []): RequestInterface
{
return $this->createRequest(PreAuthorizeRequest::class, $parameters);
}

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function capture(array $parameters = []): RequestInterface
{
Expand All @@ -153,7 +142,7 @@ public function capture(array $parameters = []): RequestInterface

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function purchase(array $parameters = []): RequestInterface
{
Expand All @@ -162,7 +151,7 @@ public function purchase(array $parameters = []): RequestInterface

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function completePurchase(array $parameters = []): RequestInterface
{
Expand All @@ -171,7 +160,7 @@ public function completePurchase(array $parameters = []): RequestInterface

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function refund(array $parameters = []): RequestInterface
{
Expand All @@ -181,7 +170,7 @@ public function refund(array $parameters = []): RequestInterface

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function void(array $parameters = []): RequestInterface
{
Expand All @@ -190,7 +179,7 @@ public function void(array $parameters = []): RequestInterface

/**
* @param array $parameters
* @return AbstractRequest|RequestInterface
* @return Messages\AbstractRequest|RequestInterface
*/
public function status(array $parameters = []): RequestInterface
{
Expand Down
26 changes: 26 additions & 0 deletions src/Mask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Omnipay\NestPay;

class Mask
{
/**
* @param string $value
* @param null $maskSymbol
* @param int $showLast
* @return string
*/
public static function mask(string $value, $maskSymbol = null, $showLast = 3): string
{
$maskSymbol = $maskSymbol ?: 'X';
$showLast = max(0, $showLast);

if (false === $showLast || mb_strlen($value) <= ($showLast + 1) * 2) {
$showRegExpPart = "";
} else {
$showRegExpPart = "(?!(.){0,$showLast}$)";
}

return preg_replace("/(?!^.?)[^-_\s]$showRegExpPart/u", $maskSymbol, $value);
}
}
51 changes: 40 additions & 11 deletions src/Messages/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@

namespace Omnipay\NestPay\Messages;

use DOMDocument;
use DOMElement;
use Exception;
use InvalidArgumentException;
use Omnipay\Common\Exception\InvalidCreditCardException;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\ResponseInterface;
use Omnipay\NestPay\Mask;
use Omnipay\NestPay\RequestInterface;
use Omnipay\NestPay\ThreeDResponse;

abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest implements RequestInterface
{
use RequestTrait, ParametersTrait;

/** @var $root \DOMElement */
/** @var $root DOMElement */
private $root;

/** @var \DOMDocument */
/** @var DOMDocument */
private $document;

private $action = "purchase";

/** @var array */
protected $requestParams;

/**
* @return string
*/
Expand Down Expand Up @@ -99,11 +108,16 @@ public function setPassword(string $value): AbstractRequest
public function sendData($data)
{
try {
$shipInfo = isset($data['ship']) ? $data['ship'] : [];
$billInfo = isset($data['bill']) ? $data['bill'] : [];
$processType = $this->getProcessType();
if(!empty($processType))
{
$data['Type'] = $processType;
}
$shipInfo = $data['ship'] ?? [];
$billInfo = $data['bill'] ?? [];
unset($data['ship'], $data['bill']);

$this->document = new \DOMDocument('1.0', 'UTF-8');
$this->document = new DOMDocument('1.0', 'UTF-8');
$this->root = $this->document->createElement('CC5Request');

foreach ($data as $id => $value) {
Expand All @@ -125,7 +139,7 @@ public function sendData($data)
$response = (string)$httpRequest->getBody()->getContents();

return $this->response = $this->createResponse($response);
} catch (\Exception $e) {
} catch (Exception $e) {
throw new InvalidResponseException(
'Error communicating with payment gateway: ' . $e->getMessage(),
$e->getCode()
Expand Down Expand Up @@ -205,7 +219,7 @@ protected function getRequestParams(): array
$gateway = $this->getBank();

if (!array_key_exists($gateway, $this->baseUrls)) {
throw new \InvalidArgumentException('Invalid Gateway');
throw new InvalidArgumentException('Invalid Gateway');
}

$data['Mode'] = $this->getTestMode() ? 'T' : 'P';
Expand Down Expand Up @@ -245,7 +259,7 @@ protected function getCompletePurchaseParams(ThreeDResponse $threeDResponse): ar
$data['GroupId'] = $threeDResponse->getGroupId() ?? '';
$data['TransId'] = $threeDResponse->getTransId() ?? '';
$data['UserId'] = $threeDResponse->getUserId() ?? '';
$data['Type'] = 'Auth';
$data['Type'] = $this->getProcessType();
$data['Expires'] = '';
$data['Cvv2Val'] = '';
$data['Total'] = $threeDResponse->getAmount();
Expand Down Expand Up @@ -402,14 +416,29 @@ private function getShipTo(): array

public function getHash(array $data): string
{
$signature = $data['clientid'] .
return $data['clientid'] .
$data['oid'] .
$data['amount'] .
$data['okUrl'] .
$data['failUrl'] .
$data['taksit'].
$this->getRnd() .
$this->getStoreKey();
return $signature;
}

protected function setRequestParams(array $data): void
{
array_walk_recursive($data, [$this, 'updateValue']);
$this->requestParams = $data;
}

protected function updateValue(&$data, $key): void
{
$sensitiveData = $this->getSensitiveData();

if (\in_array($key, $sensitiveData, true)) {
$data = Mask::mask($data);
}

}
}
27 changes: 25 additions & 2 deletions src/Messages/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class AuthorizeRequest extends AbstractRequest
public function getData(): array
{
$data = $this->getRequestParams();
$data['Type'] = 'PostAuth';

$this->setRequestParams($data);
return $data;
}

Expand All @@ -30,5 +29,29 @@ protected function createResponse($data): AuthorizeResponse
{
return new AuthorizeResponse($this, $data);
}

/**
* @return array
*/
public function getSensitiveData(): array
{
return ['Number', 'Expires', 'Password'];
}

/**
* @inheritDoc
*/
public function getProcessName(): string
{
return 'Authorize';
}

/**
* @inheritDoc
*/
public function getProcessType(): string
{
return 'PreAuth';
}
}

4 changes: 0 additions & 4 deletions src/Messages/AuthorizeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@

class AuthorizeResponse extends AbstractResponse
{
public function isRedirect()
{
return false;
}
}
46 changes: 45 additions & 1 deletion src/Messages/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,52 @@

namespace Omnipay\NestPay\Messages;

class CaptureRequest extends AuthorizeRequest
use Omnipay\Common\Exception\InvalidRequestException;

class CaptureRequest extends AbstractRequest
{
/**
* @return array
* @throws InvalidRequestException
*/
public function getData(): array
{
$data = $this->getRequestParams();
$this->setRequestParams($data);
return $data;
}

/**
* @param $data
* @return CaptureResponse
*/
protected function createResponse($data): CaptureResponse
{
return new CaptureResponse($this, $data);
}

/**
* @inheritDoc
*/
public function getSensitiveData(): array
{
return ['Number', 'Expires', 'Password'];
}

/**
* @inheritDoc
*/
public function getProcessName(): string
{
return 'Capture';
}

/**
* @inheritDoc
*/
public function getProcessType(): string
{
return 'PostAuth';
}
}

11 changes: 11 additions & 0 deletions src/Messages/CaptureResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* NestPay Purchase Response
*/

namespace Omnipay\NestPay\Messages;

class CaptureResponse extends AbstractResponse
{

}
33 changes: 23 additions & 10 deletions src/Messages/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

class CompletePurchaseRequest extends AbstractRequest
{
use ParametersTrait;

/** @var ThreeDResponse */
private $threeDResponse;

Expand All @@ -30,8 +28,9 @@ public function getData()
if (!$this->checkHash()) {
throw new RuntimeException('Hash data invalid');
}

return $this->getCompletePurchaseParams($this->threeDResponse);
$data = $this->getCompletePurchaseParams($this->threeDResponse);
$this->setRequestParams($data);
return $data;
}

/**
Expand Down Expand Up @@ -89,13 +88,27 @@ private function getGeneratedHash(): string
$signature = $hashParamsVal . $storeKey;
return base64_encode(sha1($signature, true));
}
/**
* @inheritDoc
*/
public function getSensitiveData(): array
{
return ['Password', 'Number', 'Expires', 'Cvv2Val'];
}

/**
* @inheritDoc
*/
public function getProcessName(): string
{
return 'CompletePurchase';
}

public function getEndpoint(): string
/**
* @inheritDoc
*/
public function getProcessType(): string
{
$bank = $this->getBank();
if ($this->getTestMode()) {
return $this->baseUrls['test']['3d']['baseUrl'] . $this->url['test']['purchase'];
}
return $this->baseUrls[$bank]['baseUrl'] . $this->url['purchase'];
return 'Auth';
}
}
Loading

0 comments on commit 358b0a4

Please sign in to comment.