Skip to content

Commit

Permalink
card notification config update (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jun 18, 2024
1 parent dfabe43 commit 2a4fc12
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.7]
### Added
- Added regulations in english
### Fixed
- Fixed card notification

## [2.0.6]
### Fixed
- Fixed currency validation
Expand Down
60 changes: 59 additions & 1 deletion Controller/Tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Tpay\Magento2\Service\TpayService;
use Tpay\Magento2\Service\TpayTokensService;
use Tpay\OriginApi\Utilities\Util;
use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification as OriginJWSVerifiedPaymentNotification;
use tpaySDK\Webhook\JWSVerifiedPaymentNotification;

class Notification implements CsrfAwareActionInterface
Expand Down Expand Up @@ -58,6 +59,15 @@ public function __construct(
}

public function execute(): ?Response
{
if (isset($_POST['card'])) {
return $this->executeCardNotification();
}

return $this->executeNotification();
}

public function executeNotification(): ?Response
{
$response = null;

Expand All @@ -72,6 +82,36 @@ public function execute(): ?Response
return $response;
}

public function executeCardNotification(): ?Response
{
try {
$notification = (new OriginJWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode(),
!$this->tpayConfig->useSandboxMode()
))->getNotification();

$orderId = base64_decode($notification['order_id']);

$this->tpayService->setCardOrderStatus($orderId, $notification, $this->tpayConfig);
$this->saveOriginCard($notification, $orderId);

return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);

return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE');
}
}

public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
{
return null;
Expand Down Expand Up @@ -108,7 +148,7 @@ private function saveCard(array $notification, string $orderId)

if (isset($notification['card_token']) && !$this->tpay->isCustomerGuest($orderId)) {
$token = $this->tokensService->getWithoutAuthCustomerTokens(
$order->getCustomerId(),
(string) $order->getCustomerId(),
$notification['tr_crc']
);

Expand All @@ -118,6 +158,23 @@ private function saveCard(array $notification, string $orderId)
}
}

private function saveOriginCard(array $notification, string $orderId)
{
$order = $this->tpayService->getOrderById($orderId);

$payment = $this->tpayService->getPayment($orderId);
$additionalPaymentInformation = $payment->getData()['additional_information'];

if (isset($notification['cli_auth']) && $this->tpayConfig->getCardSaveEnabled() && !$this->tpay->isCustomerGuest($orderId)) {
$this->tokensService->setCustomerToken(
(string) $order->getCustomerId(),
$notification['cli_auth'],
$notification['card'],
$additionalPaymentInformation['card_vendor']
);
}
}

private function extractNotification(StoreInterface $store): Response
{
$storeId = (int) $store->getStoreId();
Expand All @@ -127,6 +184,7 @@ private function extractNotification(StoreInterface $store): Response
$this->tpayConfig->getSecurityCode($storeId),
!$this->tpayConfig->useSandboxMode($storeId)
))->getNotification();

$notification = $notification->getNotificationAssociative();
$orderId = base64_decode($notification['tr_crc']);

Expand Down
1 change: 1 addition & 0 deletions Model/ApiFacade/CardTransaction/CardApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private function connectApi()
return;
}

$this->cardOrigin = null;
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->tokensService, $this->tpayService);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/CardTransaction/CardOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig
$this->tokensService = $tokensService;
$this->tpayService = $tpayService;
$this->tpayApi = new TpayApi($tpayConfig->getOpenApiClientId(), $tpayConfig->getOpenApiPassword(), !$tpayConfig->useSandboxMode());
$this->tpayApi->authorization()->setClientName($tpayConfig->buildMagentoInfo());
$this->tpayApi->transactions()->setClientName($tpayConfig->buildMagentoInfo());
}

public function makeFullCardTransactionProcess(string $orderId, ?array $customerToken = null): string
Expand Down
4 changes: 2 additions & 2 deletions Model/ApiFacade/CardTransaction/CardOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function makeFullCardTransactionProcess(string $orderId, ?array $customer

private function processSavedCardPayment(string $orderId, int $cardId, ?array $customerToken = null): string
{
$customerToken = $customerToken ? $customerToken : $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId));
$customerToken = $customerToken ? $customerToken : $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId), false);

if ($customerToken) {
$token = $customerToken['cli_auth'];
Expand All @@ -81,7 +81,7 @@ private function processSavedCardPayment(string $orderId, int $cardId, ?array $c
}

if (1 === (int) $paymentResult['result'] && isset($paymentResult['status']) && 'correct' === $paymentResult['status']) {
$this->tpayService->setOrderStatus($orderId, $paymentResult, $this->tpayConfig);
$this->tpayService->setCardOrderStatus($orderId, $paymentResult, $this->tpayConfig);
$this->tpayService->addCommentToHistory($orderId, 'Successful payment by saved card');

return 'magento2basic/tpay/success';
Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
if ($token) {
$this->tpayApi->setCustomToken(unserialize($token));
}
$this->tpayApi->authorization()->setClientName($tpay->buildMagentoInfo());
$this->tpayApi->transactions()->setClientName($tpay->buildMagentoInfo());
if (!$token) {
$this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay));
}
Expand Down
4 changes: 3 additions & 1 deletion Provider/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ private function getTpayPluginVersion(): string
{
$dir = __DIR__.'/../.version';
if (file_exists($dir)) {
return file_get_contents(__DIR__.'/../.version');
$version = file_get_contents(__DIR__.'/../.version');

return rtrim($version, "\n");
}

return 'n/a';
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext-json": "*",
"magento/framework": "^102.0 || ^103.0",
"tpay-com/tpay-openapi-php": "^1.7.1",
"tpay-com/tpay-php": "^2.4.4"
"tpay-com/tpay-php": "^2.4.7"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 2a4fc12

Please sign in to comment.