Skip to content

Commit

Permalink
Merge pull request #68 from nuryagdym/refactor-hash-realted-methods
Browse files Browse the repository at this point in the history
Refactor hash related methods
  • Loading branch information
nuryagdym authored Apr 16, 2022
2 parents b7229ee + a1b3894 commit ef7d5b6
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 155 deletions.
12 changes: 1 addition & 11 deletions examples/_main_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$ip = $request->getClientIp();

/*$redis = new Redis();
$redis->connect('pos_redis', 6379);
$sessionHandler = new \Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler($redis);
$sessionHandler = new \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage([
'cookie_samesite' => 'None'
], $sessionHandler);
*/

$sessionHandler = new \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage([
'cookie_samesite' => 'None'
'cookie_samesite' => 'None',
]);
$session = new \Symfony\Component\HttpFoundation\Session\Session($sessionHandler);
//banktan donuste eski session'a devam edemiyor, yeni session olusturuluyor
Expand All @@ -41,5 +33,3 @@ function getGateway(\Mews\Pos\Entity\Account\AbstractPosAccount $account): ?\Mew
dd($e);
}
}

//$hostUrl .= '/pos/examples';
8 changes: 4 additions & 4 deletions examples/garanti/_payment_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ function doPayment(\Mews\Pos\PosInterface $pos, string $transaction, ?\Mews\Pos\

$testCards = [
'visa1' => new \Mews\Pos\Entity\Card\CreditCardGarantiPos(
'4090700101174272',
30,
12,
'000',
'4282209004348015',
22,
'08',
123,
'John Doe',
AbstractCreditCard::CARD_TYPE_VISA
),
Expand Down
1 change: 1 addition & 0 deletions src/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class AbstractGateway implements PosInterface
const MODEL_NON_SECURE = 'regular';

protected const HASH_ALGORITHM = 'sha1';
protected const HASH_SEPARATOR = '';

private $config;

Expand Down
45 changes: 34 additions & 11 deletions src/Gateways/EstPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mews\Pos\Gateways;

use GuzzleHttp\Client;
use Mews\Pos\Entity\Account\AbstractPosAccount;
use Mews\Pos\Entity\Account\EstPosAccount;
use Mews\Pos\Entity\Card\CreditCardEstPos;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -110,19 +111,41 @@ public function createXML(array $nodes, string $encoding = 'ISO-8859-9', bool $i
/**
* Create 3D Hash
*
* @param AbstractPosAccount $account
* @param $order
* @param string $txType
*
* @return string
*/
public function create3DHash(): string
{
$hashStr = '';

if ($this->account->getModel() === self::MODEL_3D_SECURE) {
$hashStr = $this->account->getClientId().$this->order->id.$this->order->amount.$this->order->success_url.$this->order->fail_url.$this->order->rand.$this->account->getStoreKey();
} elseif ($this->account->getModel() === self::MODEL_3D_PAY || $this->account->getModel() === self::MODEL_3D_HOST) {
$hashStr = $this->account->getClientId().$this->order->id.$this->order->amount.$this->order->success_url.$this->order->fail_url.$this->type.$this->order->installment.$this->order->rand.$this->account->getStoreKey();
public function create3DHash(AbstractPosAccount $account, $order, string $txType): string
{
$hashData = [];
if ($account->getModel() === self::MODEL_3D_SECURE) {
$hashData = [
$account->getClientId(),
$order->id,
$order->amount,
$order->success_url,
$order->fail_url,
$order->rand,
$account->getStoreKey(),
];
} elseif ($account->getModel() === self::MODEL_3D_PAY || $account->getModel() === self::MODEL_3D_HOST) {
$hashData = [
$account->getClientId(),
$order->id,
$order->amount,
$order->success_url,
$order->fail_url,
$txType,
$order->installment,
$order->rand,
$account->getStoreKey(),
];
}
$hashStr = implode(static::HASH_SEPARATOR, $hashData);

return base64_encode(sha1($hashStr, true));
return $this->hashString($hashStr);
}

/**
Expand All @@ -147,7 +170,7 @@ public function check3DHash(array $data): bool
}

$hashVal = $paramsVal.$this->account->getStoreKey();
$hash = base64_encode(sha1($hashVal, true));
$hash = $this->hashString($hashVal);

$return = false;
if ($hashParams && !($paramsVal !== $hashParamsVal || $hashParam !== $hash)) {
Expand Down Expand Up @@ -208,7 +231,7 @@ public function get3DFormData(): array
return [];
}

$this->order->hash = $this->create3DHash();
$this->order->hash = $this->create3DHash($this->account, $this->order, $this->type);

$inputs = [
'clientid' => $this->account->getClientId(),
Expand Down
88 changes: 56 additions & 32 deletions src/Gateways/GarantiPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use Mews\Pos\Entity\Account\GarantiPosAccount;
use Mews\Pos\Entity\Card\AbstractCreditCard;
use Mews\Pos\Entity\Card\CreditCardGarantiPos;
use Mews\Pos\Exceptions\NotImplementedException;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -206,7 +207,7 @@ public function get3DFormData(): array
return [];
}

$hashData = $this->create3DHash();
$hashData = $this->create3DHash($this->account, $this->order, $this->type);

$inputs = [
'secure3dsecuritylevel' => $this->secureTypeMappings[$this->account->getModel()],
Expand Down Expand Up @@ -261,7 +262,7 @@ public function createRegularPaymentXML()
'Terminal' => [
'ProvUserID' => $this->account->getUsername(),
'UserID' => $this->account->getUsername(),
'HashData' => $this->createHashData(),
'HashData' => $this->createHashData($this->account, $this->order, $this->type, $this->card),
'ID' => $this->account->getTerminalId(),
'MerchantID' => $this->account->getClientId(),
],
Expand Down Expand Up @@ -318,7 +319,7 @@ public function createRegularPostXML()
'Terminal' => [
'ProvUserID' => $this->account->getUsername(),
'UserID' => $this->account->getUsername(),
'HashData' => $this->createHashData(),
'HashData' => $this->createHashData($this->account, $this->order, $this->type, $this->card),
'ID' => $this->account->getTerminalId(),
'MerchantID' => $this->account->getClientId(),
],
Expand Down Expand Up @@ -352,7 +353,7 @@ public function create3DPaymentXML($responseData)
'Terminal' => [
'ProvUserID' => $this->account->getUsername(),
'UserID' => $this->account->getUsername(),
'HashData' => $this->createHashData(),
'HashData' => $this->createHashData($this->account, $this->order, $this->type, $this->card),
'ID' => $this->account->getTerminalId(),
'MerchantID' => $this->account->getClientId(),
],
Expand Down Expand Up @@ -414,7 +415,7 @@ public function createCancelXML()
'Terminal' => [
'ProvUserID' => $this->account->getRefundUsername(),
'UserID' => $this->account->getRefundUsername(),
'HashData' => $this->createHashData(),
'HashData' => $this->createHashData($this->account, $this->order, $this->type),
'ID' => $this->account->getTerminalId(),
'MerchantID' => $this->account->getClientId(),
],
Expand Down Expand Up @@ -452,7 +453,7 @@ public function createRefundXML()
'Terminal' => [
'ProvUserID' => $this->account->getRefundUsername(),
'UserID' => $this->account->getRefundUsername(),
'HashData' => $this->createHashData(),
'HashData' => $this->createHashData($this->account, $this->order, $this->type),
'ID' => $this->account->getTerminalId(),
'MerchantID' => $this->account->getClientId(),
],
Expand Down Expand Up @@ -490,7 +491,7 @@ public function createHistoryXML($customQueryData)
'Terminal' => [
'ProvUserID' => $this->account->getUsername(),
'UserID' => $this->account->getUsername(),
'HashData' => $this->createHashData(),
'HashData' => $this->createHashData($this->account, $this->order, $this->type),
'ID' => $this->account->getTerminalId(),
'MerchantID' => $this->account->getClientId(),
],
Expand Down Expand Up @@ -525,7 +526,7 @@ public function createHistoryXML($customQueryData)
*/
public function createStatusXML()
{
$hashData = $this->createHashData();
$hashData = $this->createHashData($this->account, $this->order, $this->type);

$requestData = [
'Mode' => $this->getMode(),
Expand Down Expand Up @@ -567,42 +568,51 @@ public function createStatusXML()
/**
* Make Hash Data
*
* @param GarantiPosAccount $account
* @param $order
* @param string $txType
* @param AbstractCreditCard|null $card
*
* @return string
*/
public function createHashData(): string
public function createHashData(GarantiPosAccount $account, $order, string $txType, ?AbstractCreditCard $card = null): string
{
$map = [
$this->order->id,
$this->account->getTerminalId(),
isset($this->card) ? $this->card->getNumber() : null,
$this->order->amount,
$this->createSecurityData(),
$order->id,
$account->getTerminalId(),
isset($card) ? $card->getNumber() : null,
$order->amount,
$this->createSecurityData($account, $txType),
];

return strtoupper(sha1(implode('', $map)));
return $this->hashString(implode(static::HASH_SEPARATOR, $map));
}


/**
* Make 3d Hash Data
*
* @param GarantiPosAccount $account
* @param $order
* @param string $txType
*
* @return string
*/
public function create3DHash(): string
public function create3DHash(GarantiPosAccount $account, $order, string $txType): string
{
$map = [
$this->account->getTerminalId(),
$this->order->id,
$this->order->amount,
$this->order->success_url,
$this->order->fail_url,
$this->type,
$this->order->installment,
$this->account->getStoreKey(),
$this->createSecurityData(),
$account->getTerminalId(),
$order->id,
$order->amount,
$order->success_url,
$order->fail_url,
$txType,
$order->installment,
$account->getStoreKey(),
$this->createSecurityData($account, $txType),
];

return strtoupper(sha1(implode('', $map)));
return $this->hashString(implode(static::HASH_SEPARATOR, $map));
}

/**
Expand Down Expand Up @@ -997,23 +1007,37 @@ protected function prepareRefundOrder(array $order)
return $this->prepareCancelOrder($order);
}

/**
* @param string $str
*
* @return string
*/
protected function hashString(string $str): string
{
return strtoupper(hash(static::HASH_ALGORITHM, $str));
}

/**
* Make Security Data
*
* @param GarantiPosAccount $account
* @param string $txType
*
* @return string
*/
private function createSecurityData(): string
private function createSecurityData(GarantiPosAccount $account, string $txType): string
{
if ($this->type === $this->types[self::TX_REFUND] || $this->type === $this->types[self::TX_CANCEL]) {
$password = $this->account->getRefundPassword();
if ($txType === $this->types[self::TX_REFUND] || $txType === $this->types[self::TX_CANCEL]) {
$password = $account->getRefundPassword();
} else {
$password = $this->account->getPassword();
$password = $account->getPassword();
}

$map = [
$password,
str_pad((int) $this->account->getTerminalId(), 9, 0, STR_PAD_LEFT),
str_pad((int) $account->getTerminalId(), 9, 0, STR_PAD_LEFT),
];

return strtoupper(sha1(implode('', $map)));
return $this->hashString(implode(static::HASH_SEPARATOR, $map));
}
}
39 changes: 22 additions & 17 deletions src/Gateways/InterPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,29 @@ public function setCard($card)
/**
* Create 3D Hash
*
* @param InterPosAccount $account
* @param $order
* @param AbstractPosAccount $account
* @param $order
* @param string $txType
*
* @return string
*/
public function create3DHash(InterPosAccount $account, $order): string
{
$hashStr = $account->getClientId()
.$order->id
.$order->amount
.$order->success_url
.$order->fail_url
.$this->type
.$order->installment
.$order->rand
.$account->getStoreKey();
public function create3DHash(AbstractPosAccount $account, $order, string $txType): string
{
$hashData = [
$account->getClientId(),
$order->id,
$order->amount,
$order->success_url,
$order->fail_url,
$txType,
$order->installment,
$order->rand,
$account->getStoreKey(),
];

$hashStr = implode(static::HASH_SEPARATOR, $hashData);

return base64_encode(sha1($hashStr, true));
return $this->hashString($hashStr);
}

/**
Expand Down Expand Up @@ -183,8 +188,8 @@ public function check3DHash(AbstractPosAccount $account, array $data): bool
}
}

$calculatedHash = $calculatedHashParamsVal.$account->getStoreKey();
$hash = base64_encode(sha1($calculatedHash, true));
$hashStr = $calculatedHashParamsVal.$account->getStoreKey();
$hash = $this->hashString($hashStr);

return $hashParams && !($calculatedHashParamsVal !== $actualHashParamsVal || $actualHash !== $hash);
}
Expand Down Expand Up @@ -713,7 +718,7 @@ private function getCommon3DFormData(AbstractPosAccount $account, $order, string
if (!$order) {
return [];
}
$hash = $this->create3DHash($this->account, $this->order);
$hash = $this->create3DHash($this->account, $this->order, $txType);

$inputs = [
'ShopCode' => $account->getClientId(),
Expand Down
Loading

0 comments on commit ef7d5b6

Please # to comment.