Skip to content

Commit

Permalink
Refund scope update (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jul 23, 2024
1 parent fe55e45 commit bc1b90e
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.8
2.0.9
10 changes: 5 additions & 5 deletions Api/TpayConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public function getInvoiceSendMail(): string;

public function useSandboxMode(?int $storeId = null): bool;

public function getCardApiKey(): ?string;
public function getCardApiKey(?int $storeId = null): ?string;

public function getCardApiPassword(): ?string;
public function getCardApiPassword(?int $storeId = null): ?string;

public function getCardSaveEnabled(): bool;

public function getRSAKey(): ?string;
public function getRSAKey(?int $storeId = null): ?string;

public function getHashType(): ?string;
public function getHashType(?int $storeId = null): ?string;

public function getVerificationCode(): ?string;
public function getVerificationCode(?int $storeId = null): ?string;

public function isAllowSpecific(): bool;

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.9]
### Fixed
- Fixed refund scope

## [2.0.8]
### Fixed
- Fixed admin scope config
Expand Down
92 changes: 29 additions & 63 deletions Controller/Tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Service\TpayService;
Expand All @@ -35,9 +33,6 @@ class Notification implements CsrfAwareActionInterface
/** @var TpayTokensService */
private $tokensService;

/** @var StoreManagerInterface */
private $storeManager;

/** @var ResponseInterface */
private $response;

Expand All @@ -46,55 +41,27 @@ public function __construct(
TpayConfigInterface $tpayConfig,
TpayService $tpayService,
TpayTokensService $tokensService,
StoreManagerInterface $storeManager,
ResponseInterface $response
) {
$this->tpay = $tpayModel;
$this->tpayConfig = $tpayConfig;
$this->tpayService = $tpayService;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
$this->response = $response;
Util::$loggingEnabled = false;
}

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

return $this->executeNotification();
}

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

foreach ($this->storeManager->getStores() as $store) {
$response = $this->extractNotification($store);
$orderId = base64_decode($_POST['order_id']);

if (Response::STATUS_CODE_200 === $response->getStatusCode()) {
break;
}
return $this->extractCardNotification($this->getOrderStore($orderId));
}

return $response;
}

public function executeCardNotification(): ?Response
{
$response = null;
$orderId = base64_decode($_POST['tr_crc']);

foreach ($this->storeManager->getStores() as $store) {
$response = $this->extractCardNotification($store);

if (Response::STATUS_CODE_200 === $response->getStatusCode()) {
break;
}
}

return $response;
return $this->extractNotification($this->getOrderStore($orderId));
}

public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
Expand Down Expand Up @@ -160,10 +127,8 @@ private function saveOriginCard(array $notification, string $orderId)
}
}

private function extractNotification(StoreInterface $store): Response
private function extractNotification(?int $storeId = null): Response
{
$storeId = (int) $store->getStoreId();

try {
$notification = (new JWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode($storeId),
Expand All @@ -184,25 +149,14 @@ private function extractNotification(StoreInterface $store): Response

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()
)
);
$this->handleException($e);

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

private function extractCardNotification(StoreInterface $store): ?Response
private function extractCardNotification(?int $storeId = null): ?Response
{
$storeId = (int) $store->getStoreId();

try {
$notification = (new OriginJWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode($storeId),
Expand All @@ -216,18 +170,30 @@ private function extractCardNotification(StoreInterface $store): ?Response

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()
)
);
$this->handleException($e);

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

private function handleException(Exception $e)
{
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);
}

private function getOrderStore(string $orderId): ?int
{
$order = $this->tpayService->getOrderById($orderId);

return $order->getStoreId() ? (int) $order->getStoreId() : null;
}
}
4 changes: 4 additions & 0 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ private function handleDataStructure(array $data): array
$paymentData['pay']['channelId'] = $data['channel'];
}

if ($data['tax_id']) {
$paymentData['payer']['taxId'] = $data['tax_id'];
}

return $paymentData;
}

Expand Down
12 changes: 8 additions & 4 deletions Model/ApiFacade/Refund/RefundApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ class RefundApiFacade
/** @var bool */
private $useOpenApi;

/** @var null|bool */
private $storeId;

private $cache;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
public function __construct(TpayConfigInterface $tpay, CacheInterface $cache, ?int $storeId = null)
{
$this->tpay = $tpay;
$this->cache = $cache;
$this->storeId = $storeId;
}

public function makeRefund(InfoInterface $payment, float $amount)
Expand All @@ -37,7 +41,7 @@ public function makeRefund(InfoInterface $payment, float $amount)
return $this->getCurrentApi()->makeRefund($payment, $amount);
}
if (!empty($payment->getAdditionalInformation('card_data'))) {
return (new RefundCardOriginApi($this->tpay))->makeCardRefund($payment, $amount);
return (new RefundCardOriginApi($this->tpay, $this->storeId))->makeCardRefund($payment, $amount);
}

return $this->originApi->makeRefund($payment, $amount);
Expand All @@ -61,7 +65,7 @@ private function connectApi()
private function createRefundOriginApiInstance(TpayConfigInterface $tpay)
{
try {
$this->originApi = new RefundOriginApi($tpay);
$this->originApi = new RefundOriginApi($tpay, $this->storeId);
} catch (Exception $exception) {
$this->originApi = null;
}
Expand All @@ -70,7 +74,7 @@ private function createRefundOriginApiInstance(TpayConfigInterface $tpay)
private function createOpenApiInstance(TpayConfigInterface $tpay)
{
try {
$this->openApi = new OpenApi($tpay, $this->cache);
$this->openApi = new OpenApi($tpay, $this->cache, $this->storeId);
$this->useOpenApi = true;
} catch (Exception $exception) {
$this->openApi = null;
Expand Down
12 changes: 6 additions & 6 deletions Model/ApiFacade/Refund/RefundCardOriginApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

class RefundCardOriginApi extends CardRefunds
{
public function __construct(TpayConfigInterface $tpay)
public function __construct(TpayConfigInterface $tpay, ?int $storeId = null)
{
Util::$loggingEnabled = false;
$this->cardApiKey = $tpay->getCardApiKey();
$this->cardApiPass = $tpay->getCardApiPassword();
$this->cardVerificationCode = $tpay->getVerificationCode();
$this->cardKeyRSA = $tpay->getRSAKey();
$this->cardHashAlg = $tpay->getHashType();
$this->cardApiKey = $tpay->getCardApiKey($storeId);
$this->cardApiPass = $tpay->getCardApiPassword($storeId);
$this->cardVerificationCode = $tpay->getVerificationCode($storeId);
$this->cardKeyRSA = $tpay->getRSAKey($storeId);
$this->cardHashAlg = $tpay->getHashType($storeId);
parent::__construct();
}

Expand Down
12 changes: 6 additions & 6 deletions Model/ApiFacade/Refund/RefundOriginApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

class RefundOriginApi extends BasicRefunds
{
public function __construct(TpayConfigInterface $tpay)
public function __construct(TpayConfigInterface $tpay, ?int $storeId = null)
{
$this->trApiKey = $tpay->getApiPassword();
$this->trApiPass = $tpay->getApiKey();
$this->merchantId = $tpay->getMerchantId();
$this->merchantSecret = $tpay->getSecurityCode();
$this->trApiKey = $tpay->getApiPassword($storeId);
$this->trApiPass = $tpay->getApiKey($storeId);
$this->merchantId = $tpay->getMerchantId($storeId);
$this->merchantSecret = $tpay->getSecurityCode($storeId);
parent::__construct();
if ($tpay->useSandboxMode()) {
if ($tpay->useSandboxMode($storeId)) {
$this->apiURL = 'https://secure.sandbox.tpay.com/api/gw/';
}
}
Expand Down
1 change: 1 addition & 0 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function originApiFieldCorrect(array $data): array
unset($data['channel']);
unset($data['currency']);
unset($data['language']);
unset($data['tax_id']);
}

return $data;
Expand Down
4 changes: 3 additions & 1 deletion Model/TpayPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public function getTpayFormData(?string $orderId = null): array
'city' => $this->escaper->escapeHtml($order->getBillingAddress()->getData('city')),
'zip' => $this->escaper->escapeHtml($order->getBillingAddress()->getData('postcode')),
'country' => $this->escaper->escapeHtml($order->getBillingAddress()->getData('country_id')),
'tax_id' => $this->escaper->escapeHtml($order->getBillingAddress()->getData('vat_id')),
'return_error_url' => $this->urlBuilder->getUrl('magento2basic/tpay/error'),
'result_url' => $this->urlBuilder->getUrl('magento2basic/tpay/notification'),
'return_url' => $this->urlBuilder->getUrl('magento2basic/tpay/success'),
Expand Down Expand Up @@ -255,7 +256,8 @@ public function assignData(DataObject $data)
*/
public function refund(InfoInterface $payment, $amount)
{
$refundService = new RefundApiFacade($this->configurationProvider, $this->cache);
$storeId = $payment->getOrder()->getStoreId() ? (int) $payment->getOrder()->getStoreId() : null;
$refundService = new RefundApiFacade($this->configurationProvider, $this->cache, $storeId);

$refundResult = $refundService->makeRefund($payment, (float) $amount);
try {
Expand Down
20 changes: 10 additions & 10 deletions Provider/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ public function getApiKey(?int $storeId = null): ?string
return $this->getConfigData('originapi_settings/api_key_tpay', $storeId);
}

public function getCardApiKey(): ?string
public function getCardApiKey(?int $storeId = null): ?string
{
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/card_api_key_tpay');
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/card_api_key_tpay', $storeId);
}

public function getApiPassword(?int $storeId = null): ?string
{
return $this->getConfigData('originapi_settings/api_password', $storeId);
}

public function getCardApiPassword(): ?string
public function getCardApiPassword(?int $storeId = null): ?string
{
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/card_api_password');
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/card_api_password', $storeId);
}

public function getInvoiceSendMail(): string
Expand Down Expand Up @@ -174,19 +174,19 @@ public function getCardSaveEnabled(): bool
return (bool) $this->getConfigData('cardpayment_settings/card_save_enabled');
}

public function getRSAKey(): ?string
public function getRSAKey(?int $storeId = null): ?string
{
return $this->getConfigData('cardpayment_settings/rsa_key');
return $this->getConfigData('cardpayment_settings/rsa_key', $storeId);
}

public function getHashType(): ?string
public function getHashType(?int $storeId = null): ?string
{
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/hash_type');
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/hash_type', $storeId);
}

public function getVerificationCode(): ?string
public function getVerificationCode(?int $storeId = null): ?string
{
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/verification_code');
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/verification_code', $storeId);
}

public function isAllowSpecific(): bool
Expand Down
4 changes: 1 addition & 3 deletions view/base/web/css/tpay.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions view/base/web/css/tpaycards.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bc1b90e

Please sign in to comment.