GetCreateTransaction.php 1.92 KB
Newer Older
Vincent Mrose's avatar
Vincent Mrose committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

namespace Trilix\CrefoPay\Controller\Payment;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Trilix\CrefoPay\Model\CrefoPayTransactionRepository;

/**
 * Class GetCreateTransaction
 * @package Trilix\CrefoPay\Controller\Payment
 */
class GetCreateTransaction extends Action
{
    /** @var CrefoPayTransactionRepository */
    private $crefoPayTransactionRepository;

    /** @var JsonFactory */
    private $resultJsonFactory;

    /**
     * GetCreateTransaction constructor.
     * @param Context $context
     * @param JsonFactory $resultJsonFactory
     * @param CrefoPayTransactionRepository $crefoPayTransactionRepository
     */
    public function __construct(
        Context $context,
        JsonFactory $resultJsonFactory,
        CrefoPayTransactionRepository $crefoPayTransactionRepository
    ) {
        parent::__construct($context);
        $this->resultJsonFactory = $resultJsonFactory;
        $this->crefoPayTransactionRepository = $crefoPayTransactionRepository;
    }

    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Json $resultJson */
        $resultJson = $this->resultJsonFactory->create();
        $quoteId = $this->getRequest()->getParam('quoteId');

Daniel Kazior's avatar
Daniel Kazior committed
45
        $allowedPaymentMethods = '[]';
Vincent Mrose's avatar
Vincent Mrose committed
46
47
48
        try {
            $crefoPayTransaction = $this->crefoPayTransactionRepository->getByQuoteId($quoteId);
            $crefoPayOrderId = $crefoPayTransaction->getCrefoPayOrderId();
Daniel Kazior's avatar
Daniel Kazior committed
49
            $allowedPaymentMethods = $crefoPayTransaction->getPaymentMethods();
Vincent Mrose's avatar
Vincent Mrose committed
50
51
52
53
54
55
        } catch (NoSuchEntityException $e) {
            $crefoPayOrderId = null;
        }

        return $resultJson->setData([
            'crefoPayOrderId' => $crefoPayOrderId,
Daniel Kazior's avatar
Daniel Kazior committed
56
            'areAnyPaymentMethodsAllowed' => ($allowedPaymentMethods !== '[]')
Vincent Mrose's avatar
Vincent Mrose committed
57
58
59
        ]);
    }
}