Skip to content

Commit

Permalink
Merge pull request #3 from s4ddly/fix/DX-162
Browse files Browse the repository at this point in the history
Use OriginAPI JWS verification
  • Loading branch information
s4ddly authored Oct 31, 2023
2 parents d531793 + 550ed7b commit c88dcf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
33 changes: 15 additions & 18 deletions Controller/tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Magento\Framework\App\Response\Http;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Sales\Model\Order;
use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Service\TpayService;
use tpayLibs\src\_class_tpay\Utilities\Util;
use tpaySDK\Webhook\JWSVerifiedPaymentNotification;

class Notification extends Action implements CsrfAwareActionInterface
{
Expand Down Expand Up @@ -45,31 +45,26 @@ public function __construct(
parent::__construct($context);
}

public function execute(): bool
public function execute()
{
try {
$id = $this->tpay->getMerchantId();
$code = $this->tpay->getSecurityCode();
$notification = (new JWSVerifiedPaymentNotification($code, !$this->tpay->useSandboxMode()))->getNotification();
$orderId = base64_decode($notification['tr_crc']);

$validParams = $this->NotificationHandler->checkPayment('');
$orderId = base64_decode($notification->tr_crc->getValue());
if ('PAID' === $notification->tr_status->getValue()) {
if ('PAID' === $notification['tr_status']) {
$response = $this->getPaidTransactionResponse($orderId);

return $this
->getResponse()
->setStatusCode(Http::STATUS_CODE_200)
->setContent($response);
return $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent($response);
}
$this->tpayService->SetOrderStatus($orderId, $validParams, $this->tpay);

return $this
->getResponse()
->setStatusCode(Http::STATUS_CODE_200)
->setContent('TRUE');
$this->tpayService->SetOrderStatus($orderId, $notification, $this->tpay);

return $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
return false;
Util::log('Notification exception', "{$e->getMessage()} in file {$e->getFile()} line: {$e->getLine()} \n\n {$e->getTraceAsString()}");

return $this->getResponse()->setStatusCode(Http::STATUS_CODE_500)->setContent('FALSE');
}
}

Expand Down Expand Up @@ -98,12 +93,14 @@ public function validateForCsrf(RequestInterface $request): ?bool
*
* @return string response for Tpay server
*/
protected function getPaidTransactionResponse(int $orderId): string
protected function getPaidTransactionResponse(string $orderId): string
{
$order = $this->tpayService->getOrderById($orderId);

if (!$order->getId()) {
throw new Exception('Unable to get order by orderId %s', $orderId);
throw new Exception(sprintf('Unable to get order by orderId %s', $orderId));
}

if (Order::STATE_CANCELED === $order->getState()) {
return 'FALSE';
}
Expand Down
16 changes: 9 additions & 7 deletions Service/TpayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\Payment\Operations\RegisterCaptureNotificationOperation;
use Magento\Sales\Model\Order\Payment\State\CommandInterface;
use Magento\Sales\Model\Order\Payment\Transaction;
Expand Down Expand Up @@ -50,9 +50,8 @@ public function __construct(
}

/** Change order state and notify user if needed */
public function setOrderStatePendingPayment(string $orderId, bool $sendEmail): Order
public function setOrderStatePendingPayment(string $orderId, bool $sendEmail): OrderInterface
{
/** @var Order $order */
$order = $this->orderRepository->getByIncrementId($orderId);

$order->setTotalDue($order->getGrandTotal())
Expand Down Expand Up @@ -87,16 +86,19 @@ public function getPayment(string $orderId): OrderPaymentInterface
/**
* Validate order and set appropriate state
*
* @return bool|Order
* @return bool|OrderInterface
* @throws \Exception
*/
public function SetOrderStatus(int $orderId, array $validParams, TpayInterface $tpayModel)
public function SetOrderStatus(string $orderId, array $validParams, TpayInterface $tpayModel)
{
$order = $this->getOrderById($orderId);

if (!$order->getId()) {
return false;
}

$sendNewInvoiceMail = (bool) $tpayModel->getInvoiceSendMail();
$orderAmount = (float) number_format($order->getGrandTotal(), 2, '.', '');
$orderAmount = (float) number_format((float) $order->getGrandTotal(), 2, '.', '');
$trStatus = $validParams['tr_status'];
$emailNotify = false;

Expand Down Expand Up @@ -137,7 +139,7 @@ public function SetOrderStatus(int $orderId, array $validParams, TpayInterface $
}

/** Get Order object by orderId */
public function getOrderById(int $orderId): Order
public function getOrderById(string $orderId): OrderInterface
{
return $this->orderRepository->getByIncrementId($orderId);
}
Expand Down

0 comments on commit c88dcf6

Please sign in to comment.