Last active
May 16, 2019 14:46
-
-
Save mkorkmaz/c511a641797f98c15571287e7124c6a0 to your computer and use it in GitHub Desktop.
Garanti Pay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config = [ | |
'garanti-pay' => [ | |
'merchant-id' => '3424113', | |
'terminal-id' => '30690133', | |
'provision-password' => '123qweASD/', | |
'type' => 'gpdatarequest', | |
'installment-ount' => '', | |
'3d-secure-key' => '12345678', | |
'terminal-provision-user-id' => 'PROVOOS', | |
'terminal-user-id' => 'OZCAN', | |
'company-name' => 'FALANCA A.Ş', | |
'garanti-pay-success-url' => '/sign-up/payment-success', | |
'garanti-pay-failed-url' => '/sign-up/payment-failed', | |
'environment' => 'TEST' // TEST, PROD | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MyApp\Helper; | |
use Psr\Log\LoggerInterface as Logger; | |
class GarantiPOS | |
{ | |
private const GARANTI_PAY_TYPE = 'gpdatarequest'; | |
private $logger; | |
private $config; | |
public function __construct(Logger $logger, array $config) | |
{ | |
$this->logger = $logger; | |
$this->config = $config; | |
} | |
private function generateGarantiPayRequestHash(string $baseUrl, string $orderId, string $totalPrice) : string | |
{ | |
$paddedTerminalId = str_pad($this->config['terminal-id'], 9, '0', STR_PAD_LEFT); | |
$securityData = strtoupper( | |
sha1($this->config['provision-password'] . $paddedTerminalId) | |
); | |
$stringToBeHashed = $this->config['terminal-id'] . | |
$orderId . | |
$totalPrice . | |
$baseUrl . $this->config['garanti-pay-success-url'] . | |
$baseUrl . $this->config['garanti-pay-failed-url'] . | |
self::GARANTI_PAY_TYPE . | |
$this->config['installment-count'] . | |
$this->config['3d-secure-key'] . | |
$securityData; | |
return $hashData = strtoupper(sha1($stringToBeHashed)); | |
} | |
public function getGarantiPayFormValues(array $formParameters) : array | |
{ | |
$hashData = $this->generateGarantiPayRequestHash( | |
$formParameters['base_url'], | |
(string) $formParameters['order_id'], | |
(string) $formParameters['total_price'] | |
); | |
$garantiHiddenFormParameters = [ | |
'orderid' => $formParameters['order_id'], | |
'txnamount' => $formParameters['total_price'], | |
'terminalprovuserid' => $this->config['terminal-provision-user-id'], | |
'terminaluserid' => $this->config['terminal-user-id'], | |
'terminalid' => $this->config['terminal-id'], | |
'terminalmerchantid' => $this->config['merchant-id'], | |
'mode' => $this->config['environment'], | |
'apiversion' => 'v1.0', | |
'secure3dsecuritylevel' => 'CUSTOM_PAY', | |
'txntype' => self::GARANTI_PAY_TYPE, | |
'txnsubtype' => 'sales', | |
'garantipay' => 'Y', | |
'companyname' => $this->config['company-name'], | |
'txnamount' => $formParameters['total_price'], | |
'txncurrencycode' => $formParameters['currency'] ?? '949', | |
'lang' => $formParameters['lang'] ?? 'tr', | |
'secure3dhash' => $hashData, | |
'txninstallmentcount' => $this->config['installment-count'], | |
'successurl' => $formParameters['base_url'] . $this->config['garanti-pay-success-url'], | |
'errorurl' => $formParameters['base_url'] . $this->config['garanti-pay-failed-url'], | |
'txntimestamp' => time(), | |
'bnsuseflag' => 'Y', | |
'fbbuseflag' => 'Y', | |
'chequeuseflag' => 'Y', | |
'mileuseflag' => 'Y', | |
'addcampaigninstallment' => 'Y', | |
'customeripaddress' => $formParameters['customer_ip_address'] | |
]; | |
unset( | |
$formParameters['order_id'], | |
$formParameters['total_price'], | |
$formParameters['base_url'], | |
$formParameters['customer_ip_address'] | |
); | |
return array_merge($formParameters, $garantiHiddenFormParameters); | |
} | |
public function verifyGarantiPayPayment(array $queryParams) : bool | |
{ | |
$stringToBeHashed = $queryParams['clientid'] . | |
$queryParams['oid'] . | |
$queryParams['authcode'] . | |
$queryParams['procreturncode'] . | |
$queryParams['gpinstallmentamount'] . | |
$queryParams['gpinstallment'] . | |
$this->config['3d-secure-key']; | |
$hash = base64_encode(sha1($stringToBeHashed, true)); | |
if ($queryParams['gphashdata'] === $hash && $queryParams['procreturncode'] === '00') { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$logger = new Logger(); // Psr\Log\LoggerInterface | |
$price = 15.50; | |
$tax = 0.18; // %18 | |
$orderId = 'unique-order-id-generated-by-you'; | |
/** | |
* @var $garantiPosConfig array | |
*/ | |
$garantiPosConfig = $config['garanti-pay']; | |
$packageName = $this->args['packageName']; | |
$totalPrice = round($price * (1 + $tax)*100); | |
$garantiPos = new GarantiPOS($logger, $garantiPosConfig); | |
$hiddenFormParameters = [ | |
'base_url' => $this->getBaseUrl(), | |
'order_id' => $orderId, | |
'total_price' => $totalPrice, | |
'customer_ip_address' => '192.168.0.1', | |
'my_input' => 'value', | |
'my_other_input' => 'value' | |
]; | |
$garantiPayVariables => $garantiPos->getGarantiPayFormValues($hiddenFormParameters); | |
echo render('template.html', $garantiPayVariables); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$logger = new Logger(); // Psr\Log\LoggerInterface | |
$queryParams = $request->getQueryParams(); | |
$garantiPosConfig = $config['garanti-pay']; | |
$garantiPos = new GarantiPOS($logger, $garantiPosConfig); | |
if ($garantiPos->verifyGarantiPayPayment($queryParams)) { | |
// payment verified | |
} | |
// payment verify failed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment