Skip to content

Commit

Permalink
Merge pull request #8 from boolfly/feature/upgrade_new_api
Browse files Browse the repository at this point in the history
Upgrade Momo to new version support 3 methods
  • Loading branch information
thaopw authored Dec 6, 2023
2 parents bace298 + 44bddc8 commit 679c991
Show file tree
Hide file tree
Showing 48 changed files with 1,457 additions and 950 deletions.
23 changes: 9 additions & 14 deletions Block/Info.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
<?php

/************************************************************
* *
* * Copyright © Boolfly. All rights reserved.
* * See COPYING.txt for license details.
* *
* * @author [email protected]
* * @project Momo Wallet
/**
* Copyright © Boolfly. All rights reserved.
* See COPYING.txt for license details.
*
* @author [email protected]
* @project Momo Wallet
*/

declare(strict_types=1);

namespace Boolfly\MomoWallet\Block;

use Magento\Framework\Phrase;
use Magento\Payment\Block\ConfigurableInfo;

/**
* Class Info
*
* @package Boolfly\MomoWallet\Block
*/
class Info extends ConfigurableInfo
{
/**
Expand All @@ -29,7 +24,7 @@ class Info extends ConfigurableInfo
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getLabel($field)
protected function getLabel($field): Phrase
{
switch ($field) {
case 'transaction_type':
Expand Down
125 changes: 71 additions & 54 deletions Controller/Payment/Ipn.php
Original file line number Diff line number Diff line change
@@ -1,113 +1,130 @@
<?php

/************************************************************
* *
* * Copyright © Boolfly. All rights reserved.
* * See COPYING.txt for license details.
* *
* * @author [email protected]
* * @project Momo Wallet
/**
* Copyright © Boolfly. All rights reserved.
* See COPYING.txt for license details.
*
* @author [email protected]
* @project Momo Wallet
*/

declare(strict_types=1);

namespace Boolfly\MomoWallet\Controller\Payment;

use Magento\Framework\Serialize\SerializerInterface;
use Boolfly\MomoWallet\Gateway\Helper\TransactionReader;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action as AppAction;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
use Magento\Payment\Gateway\Helper\ContextHelper;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Payment\Model\MethodInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Sales\Model\OrderFactory;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Action\Context;
use Psr\Log\LoggerInterface;

/**
* Class Ipn
*
* @package Boolfly\MomoWallet\Controller\Payment
*/
class Ipn extends AppAction implements CsrfAwareActionInterface
class Ipn implements CsrfAwareActionInterface
{
/**
* @var CommandPoolInterface
*/
private $commandPool;
private CommandPoolInterface $commandPool;

/**
* @var Session
* @var MethodInterface
*/
private $checkoutSession;
private MethodInterface $method;

/**
* @var OrderRepositoryInterface
* @var PaymentDataObjectFactory
*/
private $orderRepository;
private PaymentDataObjectFactory $paymentDataObjectFactory;

/**
* @var MethodInterface
* @var OrderFactory
*/
private $method;
private OrderFactory $orderFactory;

/**
* @var PaymentDataObjectFactory
* @var RequestInterface
*/
private $paymentDataObjectFactory;
private RequestInterface $getRequest;

/**
* @var OrderFactory
* @var ResultFactory
*/
private $orderFactory;
private ResultFactory $resultFactory;

/**
* @var ObjectManagerInterface
*/
private ObjectManagerInterface $objectManager;

/**
* @var ManagerInterface
*/
private ManagerInterface $messageManager;

/**
* @var SerializerInterface
*/
private SerializerInterface $serializer;

/**
* Ipn constructor.
*
* @param Context $context
* @param Session $checkoutSession
* @param MethodInterface $method
* @param MethodInterface $method
* @param PaymentDataObjectFactory $paymentDataObjectFactory
* @param OrderRepositoryInterface $orderRepository
* @param OrderFactory $orderFactory
* @param CommandPoolInterface $commandPool
* @param OrderFactory $orderFactory
* @param CommandPoolInterface $commandPool
* @param Context $context
* @param SerializerInterface $serializer
*/
public function __construct(
Context $context,
Session $checkoutSession,
MethodInterface $method,
PaymentDataObjectFactory $paymentDataObjectFactory,
OrderRepositoryInterface $orderRepository,
OrderFactory $orderFactory,
CommandPoolInterface $commandPool
CommandPoolInterface $commandPool,
Context $context,
SerializerInterface $serializer
) {
parent::__construct($context);
$this->commandPool = $commandPool;
$this->checkoutSession = $checkoutSession;
$this->orderRepository = $orderRepository;
$this->method = $method;
$this->serializer = $serializer;
$this->commandPool = $commandPool;
$this->method = $method;
$this->paymentDataObjectFactory = $paymentDataObjectFactory;
$this->orderFactory = $orderFactory;
$this->orderFactory = $orderFactory;
$this->getRequest = $context->getRequest();
$this->resultFactory = $context->getResultFactory();
$this->objectManager = $context->getObjectManager();
$this->messageManager = $context->getMessageManager();
}

/**
* @return \Magento\Framework\App\ResponseInterface|Json|\Magento\Framework\Controller\ResultInterface
* Execute
*
* @return Json|ResultInterface|ResponseInterface|null
*/
public function execute()
public function execute(): Json|ResultInterface|ResponseInterface|null
{
if (!$this->getRequest()->isPost()) {
return;
if (!$this->getRequest->isPost()) {
return null;
}
/** @var Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$data = [
'errors' => true,
'messages' => __('Something went wrong white execute.')
'messages' => __('Something went wrong while execute.'),
];
try {
$response = $this->getRequest()->getPostValue();
$requestContent = $this->getRequest->getContent();
$response = $this->serializer->unserialize($requestContent);
$orderIncrementId = TransactionReader::readOrderId($response);
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
$payment = $order->getPayment();
Expand All @@ -128,7 +145,7 @@ public function execute()
];
}
} catch (\Exception $e) {
$this->_objectManager->get('\Psr\Log\LoggerInterface')->critical($e->getMessage());
$this->objectManager->get(LoggerInterface::class)->critical($e->getMessage());
$this->messageManager->addErrorMessage(__('Transaction has been declined. Please try again later.'));
$resultJson->setHttpResponseCode(500);
}
Expand All @@ -138,10 +155,10 @@ public function execute()

/**
* Create exception in case CSRF validation failed.
*
* Return null if default exception will suffice.
*
* @param RequestInterface $request
*
* @return InvalidRequestException|null
*/
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
Expand All @@ -151,10 +168,10 @@ public function createCsrfValidationException(RequestInterface $request): ?Inval

/**
* Perform custom request validation.
*
* Return null if default validation is needed.
*
* @param RequestInterface $request
*
* @return boolean|null
*/
public function validateForCsrf(RequestInterface $request): ?bool
Expand Down
Loading

0 comments on commit 679c991

Please sign in to comment.