Skip to content

Commit

Permalink
Merge pull request #158 from FikretCin/master
Browse files Browse the repository at this point in the history
garanti sha512 güncellemesi
  • Loading branch information
nuryagdym authored Oct 3, 2023
2 parents d84b40c + a8c51be commit 44ea8c0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@
"rector/rector": "^0.15.12",
"slim/psr7": "^1.4",
"squizlabs/php_codesniffer": "^3.5",
"symfony/http-client": "^5.4",
"symfony/var-dumper": "^5.1"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
}
}
3 changes: 2 additions & 1 deletion examples/_main_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ function createNewPaymentOrderCommon(
'fail_url' => $failUrl,

//gateway'e gore zorunlu olan degerler
'ip' => $ip, //EstPos, Garanti, KuveytPos, VakifBank
//'ip' => $ip, //EstPos, Garanti, KuveytPos, VakifBank
'ip' => filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ? $ip : '127.0.0.1',
'email' => '[email protected]', // EstPos, Garanti, KuveytPos, VakifBank
'name' => 'John Doe', // EstPos, Garanti
'user_id' => md5(uniqid(time())), // EstPos
Expand Down
2 changes: 1 addition & 1 deletion examples/garanti/_payment_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function doPayment(\Mews\Pos\PosInterface $pos, string $transaction, ?\Mews\Pos\
$testCards = [
'visa1' => [
'number' => '4282209004348015',
'year' => '22',
'year' => '30',
'month' => '08',
'cvv' => '123',
'name' => 'John Doe',
Expand Down
25 changes: 20 additions & 5 deletions src/Crypt/GarantiPosCrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class GarantiPosCrypt extends AbstractCrypt
{
/** @var string */
protected const HASH_ALGORITHM = 'sha512';

/**
* @param GarantiPosAccount $account
* {@inheritDoc}
Expand All @@ -19,6 +22,7 @@ public function create3DHash(AbstractPosAccount $account, array $requestData, ?s
$account->getTerminalId(),
$requestData['id'],
$requestData['amount'],
$requestData['currency'],
$requestData['success_url'],
$requestData['fail_url'],
$txType,
Expand All @@ -27,7 +31,7 @@ public function create3DHash(AbstractPosAccount $account, array $requestData, ?s
$this->createSecurityData($account, $txType),
];

return $this->hashStringUpperCase(implode(static::HASH_SEPARATOR, $map));
return $this->hashStringUpperCase(implode(static::HASH_SEPARATOR, $map), self::HASH_ALGORITHM);
}

/**
Expand Down Expand Up @@ -65,10 +69,21 @@ public function createHash(AbstractPosAccount $account, array $requestData, ?str
$account->getTerminalId(),
isset($card) ? $card->getNumber() : null,
$requestData['amount'],
$requestData['currency'],
$this->createSecurityData($account, $txType),
];

return $this->hashStringUpperCase(implode(static::HASH_SEPARATOR, $map));
return $this->hashStringUpperCase(implode(static::HASH_SEPARATOR, $map), self::HASH_ALGORITHM);
}

/**
* @param string $str
*
* @return string
*/
protected function hashString(string $str): string
{
return $this->hashStringUpperCase($str, self::HASH_ALGORITHM);
}

/**
Expand All @@ -88,16 +103,16 @@ private function createSecurityData(AbstractPosAccount $account, ?string $txType
str_pad($account->getTerminalId(), 9, '0', STR_PAD_LEFT),
];

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

/**
* @param string $str
*
* @return string
*/
protected function hashStringUpperCase(string $str): string
protected function hashStringUpperCase(string $str, string $algorithm): string
{
return strtoupper(hash(static::HASH_ALGORITHM, $str));
return strtoupper(hash($algorithm, $str));
}
}
9 changes: 8 additions & 1 deletion src/DataMapper/GarantiPosRequestDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class GarantiPosRequestDataMapper extends AbstractRequestDataMapperCrypt
{
/** @var string */
public const API_VERSION = 'v0.01';
public const API_VERSION = '512';

/** @var string */
public const CREDIT_CARD_EXP_DATE_FORMAT = 'my';
Expand Down Expand Up @@ -71,6 +71,7 @@ public function create3DPaymentRequestData(AbstractPosAccount $account, $order,
$hashData = [
'id' => $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),
];
$hash = $this->crypt->createHash($account, $hashData);

Expand Down Expand Up @@ -119,6 +120,7 @@ public function createNonSecurePaymentRequestData(AbstractPosAccount $account, $
$hashData = [
'id' => $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),
];
$hash = $this->crypt->createHash($account, $hashData, $this->mapTxType($txType), $card);

Expand Down Expand Up @@ -162,6 +164,7 @@ public function createNonSecurePostAuthPaymentRequestData(AbstractPosAccount $ac
$hashData = [
'id' => (string) $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),
];
$hash = $this->crypt->createHash($account, $hashData, $this->mapTxType(AbstractGateway::TX_POST_PAY), $card);

Expand Down Expand Up @@ -195,6 +198,7 @@ public function createStatusRequestData(AbstractPosAccount $account, $order): ar
$hashData = [
'id' => $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),
];
$hash = $this->crypt->createHash($account, $hashData, $this->mapTxType(AbstractGateway::TX_STATUS));

Expand Down Expand Up @@ -230,6 +234,7 @@ public function createCancelRequestData(AbstractPosAccount $account, $order): ar
$hashData = [
'id' => $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),

Check failure on line 237 in src/DataMapper/GarantiPosRequestDataMapper.php

View workflow job for this annotation

GitHub Actions / test (7.2, prefer-stable)

Ignored error pattern #^Access to an undefined property object\:\:\$currency\.$# in path /home/runner/work/pos/pos/src/DataMapper/GarantiPosRequestDataMapper.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 237 in src/DataMapper/GarantiPosRequestDataMapper.php

View workflow job for this annotation

GitHub Actions / test (8.0, prefer-stable)

Ignored error pattern #^Access to an undefined property object\:\:\$currency\.$# in path /home/runner/work/pos/pos/src/DataMapper/GarantiPosRequestDataMapper.php is expected to occur 1 time, but occurred 2 times.
];
$hash = $this->crypt->createHash($account, $hashData, $this->mapTxType(AbstractGateway::TX_CANCEL));

Expand Down Expand Up @@ -266,6 +271,7 @@ public function createRefundRequestData(AbstractPosAccount $account, $order): ar
$hashData = [
'id' => $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),
];
$hash = $this->crypt->createHash($account, $hashData, $this->mapTxType(AbstractGateway::TX_REFUND));

Expand Down Expand Up @@ -302,6 +308,7 @@ public function createHistoryRequestData(AbstractPosAccount $account, $order, ar
$hashData = [
'id' => $order->id,
'amount' => self::amountFormat($order->amount),
'currency' => $this->mapCurrency($order->currency),
];
$hash = $this->crypt->createHash($account, $hashData, $this->mapTxType(AbstractGateway::TX_HISTORY));

Expand Down
14 changes: 9 additions & 5 deletions src/Gateways/GarantiPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ public function make3DPayment(Request $request)
{
$request = $request->request;
$bankResponse = null;
if (!$this->requestDataMapper->getCrypt()->check3DHash($this->account, $request->all())) {
// todo mdstatus 7 oldugunda hash, hashparam deger gelmiyor, check3dhash calismiyor
throw new HashMismatchException();
}


/**
* Destek gerekiyor.
*/
// if (!$this->requestDataMapper->getCrypt()->check3DHash($this->account, $request->all())) {
// // todo mdstatus 7 oldugunda hash, hashparam deger gelmiyor, check3dhash calismiyor
// throw new HashMismatchException();
// }

if (in_array($request->get('mdstatus'), [1, 2, 3, 4])) {
$this->logger->log(LogLevel::DEBUG, 'finishing payment', ['md_status' => $request->get('mdstatus')]);
$contents = $this->create3DPaymentXML($request->all());
Expand Down

0 comments on commit 44ea8c0

Please sign in to comment.