Skip to content

Commit

Permalink
Fix Payment Method checking logic to prevent null pointer and remove …
Browse files Browse the repository at this point in the history
…transaction scoping (#189)
  • Loading branch information
aashwin-rvvup authored Oct 3, 2024
1 parent 067da41 commit ac41788
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
30 changes: 14 additions & 16 deletions Model/ProcessOrder/Processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace Rvvup\Payments\Model\ProcessOrder;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Model\Order;
use Rvvup\Payments\Model\Logger;
use Rvvup\Payments\Gateway\Method;
use Magento\Sales\Api\Data\OrderInterface;
use Rvvup\Payments\Controller\Redirect\In;
use Rvvup\Payments\Model\RvvupConfigProvider;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Psr\Log\LoggerInterface;
use Rvvup\Payments\Api\Data\ProcessOrderResultInterface;
use Rvvup\Payments\Exception\PaymentValidationException;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Rvvup\Payments\Api\Data\ProcessOrderResultInterfaceFactory;
use Rvvup\Payments\Controller\Redirect\In;
use Rvvup\Payments\Exception\PaymentValidationException;
use Rvvup\Payments\Gateway\Method;
use Rvvup\Payments\Model\Logger;
use Rvvup\Payments\Model\RvvupConfigProvider;

class Processing implements ProcessorInterface
{
Expand Down Expand Up @@ -68,13 +68,11 @@ public function execute(OrderInterface $order, array $rvvupData, string $origin)
{
/** @var \Rvvup\Payments\Api\Data\ProcessOrderResultInterface $processOrderResult */
$processOrderResult = $this->processOrderResultFactory->create();

if ($order->getPayment() === null
|| strpos($order->getPayment()->getMethod(), Method::PAYMENT_TITLE_PREFIX) !== 0
) {
if (strpos($order->getPayment()->getMethod(), RvvupConfigProvider::CODE) !== 0) {
throw new PaymentValidationException(__('Order is not paid via Rvvup'));
}
if ($order->getPayment() === null) {
throw new PaymentValidationException(__('Missing payment for order.'));
}
if (strpos($order->getPayment()->getMethod(), RvvupConfigProvider::CODE) !== 0) {
throw new PaymentValidationException(__('Order is not paid via Rvvup'));
}

try {
Expand Down
12 changes: 0 additions & 12 deletions Service/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Payment;
use Magento\Quote\Model\QuoteManagement;
Expand All @@ -28,7 +27,6 @@
use Rvvup\Payments\Gateway\Method;
use Rvvup\Payments\Model\SdkProxy;
use Magento\Quote\Model\ResourceModel\Quote\Payment\CollectionFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;

class Capture
{
Expand All @@ -46,9 +44,6 @@ class Capture
/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var QuoteResource */
private $quoteResource;

/** @var QuoteManagement */
private $quoteManagement;

Expand Down Expand Up @@ -80,7 +75,6 @@ class Capture
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param OrderPaymentRepositoryInterface $orderPaymentRepository
* @param OrderRepositoryInterface $orderRepository
* @param QuoteResource $quoteResource
* @param QuoteManagement $quoteManagement
* @param SdkProxy $sdkProxy
* @param CollectionFactory $collectionFactory
Expand All @@ -96,7 +90,6 @@ public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
OrderPaymentRepositoryInterface $orderPaymentRepository,
OrderRepositoryInterface $orderRepository,
QuoteResource $quoteResource,
QuoteManagement $quoteManagement,
SdkProxy $sdkProxy,
CollectionFactory $collectionFactory,
Expand All @@ -112,7 +105,6 @@ public function __construct(
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->orderPaymentRepository = $orderPaymentRepository;
$this->orderRepository = $orderRepository;
$this->quoteResource = $quoteResource;
$this->quoteManagement = $quoteManagement;
$this->sdkProxy = $sdkProxy;
$this->collectionFactory = $collectionFactory;
Expand Down Expand Up @@ -204,7 +196,6 @@ public function validate(
*/
public function createOrder(string $rvvupId, Quote $quote, string $origin): ValidationInterface
{
$this->quoteResource->beginTransaction();
$payment = $quote->getPayment();

try {
Expand All @@ -228,7 +219,6 @@ public function createOrder(string $rvvupId, Quote $quote, string $origin): Vali
}

$orderId = $this->quoteManagement->placeOrder($quote->getEntityId(), $payment);
$this->quoteResource->commit();
return $this->validationInterfaceFactory->create(
[
'data' => [
Expand All @@ -238,7 +228,6 @@ public function createOrder(string $rvvupId, Quote $quote, string $origin): Vali
]
);
} catch (NoSuchEntityException $e) {
$this->quoteResource->rollback();
$this->logger->addRvvupError(
'Order placement within rvvup payment failed',
$e->getMessage(),
Expand All @@ -257,7 +246,6 @@ public function createOrder(string $rvvupId, Quote $quote, string $origin): Vali
]
);
} catch (\Exception $e) {
$this->quoteResource->rollback();
if (strpos($e->getMessage(), AdapterInterface::ERROR_ROLLBACK_INCOMPLETE_MESSAGE) !== false) {
$this->logger->addRvvupError(
'Order placement within rvvup payment failed',
Expand Down

0 comments on commit ac41788

Please sign in to comment.