Skip to content

Commit

Permalink
Request builders and response processors extracted from payment imple…
Browse files Browse the repository at this point in the history
…mentations
  • Loading branch information
Ibrahim Gunduz committed May 3, 2018
1 parent 56e3ba9 commit 97e29fb
Show file tree
Hide file tree
Showing 116 changed files with 3,549 additions and 1,075 deletions.
25 changes: 25 additions & 0 deletions src/Builder/AbstractBuilderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Paranoia\Builder;

use Paranoia\Configuration\AbstractConfiguration;

abstract class AbstractBuilderFactory
{
/** @var AbstractConfiguration */
protected $configuration;

/**
* AbstractBuilderFactory constructor.
* @param AbstractConfiguration $configuration
*/
public function __construct(AbstractConfiguration $configuration)
{
$this->configuration = $configuration;
}

/**
* @param $transactionType
* @return AbstractRequestBuilder
*/
abstract protected function createBuilder($transactionType);
}
22 changes: 22 additions & 0 deletions src/Builder/AbstractRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Paranoia\Builder;

use Paranoia\Configuration\AbstractConfiguration;
use Paranoia\Request;

abstract class AbstractRequestBuilder
{
/** @var AbstractConfiguration */
protected $configuration;

/**
* AbstractRequestBuilder constructor.
* @param AbstractConfiguration $configuration
*/
public function __construct(AbstractConfiguration $configuration)
{
$this->configuration = $configuration;
}

abstract public function build(Request $request);
}
123 changes: 123 additions & 0 deletions src/Builder/Gvp/BaseRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
namespace Paranoia\Builder\Gvp;

use Paranoia\Builder\AbstractRequestBuilder;
use Paranoia\Configuration\AbstractConfiguration;
use Paranoia\Configuration\Gvp as GvpConfiguration;
use Paranoia\Formatter\Gvp\ExpireDateFormatter;
use Paranoia\Formatter\IsoNumericCurrencyCodeFormatter;
use Paranoia\Formatter\MoneyFormatter;
use Paranoia\Formatter\SingleDigitInstallmentFormatter;
use Paranoia\Request;

abstract class BaseRequestBuilder extends AbstractRequestBuilder
{
const API_VERSION = '0.01';
const CARD_HOLDER_PRESENT_CODE_NON_3D = 0;
const CARD_HOLDER_PRESENT_CODE_3D = 13;

/** @var MoneyFormatter */
protected $amountFormatter;

/** @var IsoNumericCurrencyCodeFormatter */
protected $currencyCodeFormatter;

/** @var SingleDigitInstallmentFormatter */
protected $installmentFormatter;

/** @var ExpireDateFormatter */
protected $expireDateFormatter;

public function __construct(
AbstractConfiguration $configuration,
IsoNumericCurrencyCodeFormatter $currencyCodeFormatter,
MoneyFormatter $amountFormatter,
SingleDigitInstallmentFormatter $installmentFormatter,
ExpireDateFormatter $expireDateFormatter
) {
parent::__construct($configuration);
$this->currencyCodeFormatter = $currencyCodeFormatter;
$this->amountFormatter = $amountFormatter;
$this->installmentFormatter = $installmentFormatter;
$this->expireDateFormatter = $expireDateFormatter;
}

protected function buildBaseRequest(Request $request)
{
/** @var GvpConfiguration $configuration */
$configuration = $this->configuration;
return [
'Version' => self::API_VERSION,
'Mode' => $configuration->getMode(),
'Terminal' => $this->buildTerminal($request),
'Order' => $this->buildOrder($request),
'Customer' => $this->buildCustomer(),
'Transaction' => $this->buildTransaction($request)
];
}

abstract protected function buildTransaction(Request $request);
abstract protected function getCredentialPair();
abstract protected function buildHash(Request $request, $password);

protected function buildCustomer()
{
return [
'IPAddress' => '127.0.0.1',
'EmailAddress' => '[email protected]'
];
}

protected function generateSecurityHash($password)
{
/** @var GvpConfiguration $configuration */
$configuration = $this->configuration;

$tidPrefix = str_repeat('0', 9 - strlen($configuration->getTerminalId()));
$terminalId = sprintf('%s%s', $tidPrefix, $configuration->getTerminalId());
return strtoupper(SHA1(sprintf('%s%s', $password, $terminalId)));
}

protected function buildTerminal(Request $request)
{
/** @var GvpConfiguration $configuration */
$configuration = $this->configuration;

list($username, $password) = $this->getCredentialPair();

$hash = $this->buildHash($request, $password);

return array(
'ProvUserID' => $username,
'HashData' => $hash,
'UserID' => $username,
'ID' => $configuration->getTerminalId(),
'MerchantID' => $configuration->getMerchantId()
);
}

protected function buildCard(Request $request)
{
$expireMonth = $this->expireDateFormatter->format(
[
$request->getExpireMonth(),
$request->getExpireYear()
]
);

return array(
'Number' => $request->getCardNumber(),
'ExpireDate' => $expireMonth,
'CVV2' => $request->getSecurityCode()
);
}

protected function buildOrder(Request $request)
{
return [
'OrderID' => $request->getOrderId(),
'GroupID' => null,
'Description' => null
];
}
}
58 changes: 58 additions & 0 deletions src/Builder/Gvp/CancelRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Paranoia\Builder\Gvp;

use Paranoia\Common\Serializer\Serializer;
use Paranoia\Configuration\Gvp;
use Paranoia\Request;

class CancelRequestBuilder extends BaseRequestBuilder
{
const TRANSACTION_TYPE = 'void';
const ENVELOPE_NAME = 'GVPSRequest';

public function build(Request $request)
{
$data = $this->buildBaseRequest($request);
$serializer = new Serializer(Serializer::XML);
return $serializer->serialize($data, ['root_name' => self::ENVELOPE_NAME]);
}

protected function buildTransaction(Request $request)
{
return [
'Type' => self::TRANSACTION_TYPE,
'Amount' => 1,
'CurrencyCode' => null,

#TODO: Will be changed after 3D integration
'CardholderPresentCode' => self::CARD_HOLDER_PRESENT_CODE_NON_3D,

'MotoInd' => 'N',
'OriginalRetrefNum' => $request->getTransactionId()
];
}

protected function getCredentialPair()
{
/** @var Gvp $configuration */
$configuration = $this->configuration;
return [$configuration->getRefundUsername(), $configuration->getRefundPassword()];
}

protected function buildHash(Request $request, $password)
{
/** @var Gvp $configuration */
$configuration = $this->configuration;
return strtoupper(
sha1(
sprintf(
'%s%s%s%s',
$request->getOrderId(),
$configuration->getTerminalId(),
1,
$this->generateSecurityHash($password)
)
)
);
}
}
59 changes: 59 additions & 0 deletions src/Builder/Gvp/PostAuthorizationRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Paranoia\Builder\Gvp;

use Paranoia\Common\Serializer\Serializer;
use Paranoia\Configuration\Gvp;
use Paranoia\Request;

class PostAuthorizationRequestBuilder extends BaseRequestBuilder
{
const TRANSACTION_TYPE = 'postauth';
const ENVELOPE_NAME = 'GVPSRequest';

public function build(Request $request)
{
$data = $this->buildBaseRequest($request);

$serializer = new Serializer(Serializer::XML);
return $serializer->serialize($data, ['root_name' => self::ENVELOPE_NAME]);
}

protected function buildTransaction(Request $request)
{
return [
'Type' => self::TRANSACTION_TYPE,
'Amount' => $this->amountFormatter->format($request->getAmount()),
'CurrencyCode' => $this->currencyCodeFormatter->format($request->getCurrency()),

#TODO: Will be changed after 3D integration
'CardholderPresentCode' => self::CARD_HOLDER_PRESENT_CODE_NON_3D,

'MotoInd' => 'N',
'OriginalRetrefNum' => $request->getTransactionId(),
];
}

protected function getCredentialPair()
{
/** @var Gvp $configuration */
$configuration = $this->configuration;
return [$configuration->getAuthorizationUsername(), $configuration->getAuthorizationPassword()];
}

protected function buildHash(Request $request, $password)
{
/** @var Gvp $configuration */
$configuration = $this->configuration;
return strtoupper(
sha1(
sprintf(
'%s%s%s%s',
$request->getOrderId(),
$configuration->getTerminalId(),
$this->amountFormatter->format($request->getAmount()),
$this->generateSecurityHash($password)
)
)
);
}
}
63 changes: 63 additions & 0 deletions src/Builder/Gvp/PreAuthorizationRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Paranoia\Builder\Gvp;

use Paranoia\Common\Serializer\Serializer;
use Paranoia\Configuration\Gvp;
use Paranoia\Request;

class PreAuthorizationRequestBuilder extends BaseRequestBuilder
{
const TRANSACTION_TYPE = 'preauth';
const ENVELOPE_NAME = 'GVPSRequest';

public function build(Request $request)
{
$data = array_merge(
$this->buildBaseRequest($request),
['Card' => $this->buildCard($request)]
);

$serializer = new Serializer(Serializer::XML);
return $serializer->serialize($data, ['root_name' => self::ENVELOPE_NAME]);
}

protected function buildTransaction(Request $request)
{
return [
'Type' => self::TRANSACTION_TYPE,
'Amount' => $this->amountFormatter->format($request->getAmount()),
'CurrencyCode' => $this->currencyCodeFormatter->format($request->getCurrency()),

#TODO: Will be changed after 3D integration
'CardholderPresentCode' => self::CARD_HOLDER_PRESENT_CODE_NON_3D,

'MotoInd' => 'N',
'OriginalRetrefNum' => null,
];
}

protected function getCredentialPair()
{
/** @var Gvp $configuration */
$configuration = $this->configuration;
return [$configuration->getAuthorizationUsername(), $configuration->getAuthorizationPassword()];
}

protected function buildHash(Request $request, $password)
{
/** @var Gvp $configuration */
$configuration = $this->configuration;
return strtoupper(
sha1(
sprintf(
'%s%s%s%s%s',
$request->getOrderId(),
$configuration->getTerminalId(),
$request->getCardNumber(),
$this->amountFormatter->format($request->getAmount()),
$this->generateSecurityHash($password)
)
)
);
}
}
Loading

0 comments on commit 97e29fb

Please sign in to comment.