SessionInitiator.php 3.79 KB
Newer Older
Ketan's avatar
Ketan 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php
/**
 * This file is part of the Klarna KP module
 *
 * (c) Klarna Bank AB (publ)
 *
 * For the full copyright and license information, please view the NOTICE
 * and LICENSE files that were distributed with this source code.
 */

namespace Klarna\Kp\Model;

use Klarna\Core\Exception as KlarnaException;
use Klarna\Core\Model\Api\Exception as KlarnaApiException;
use Klarna\Kp\Api\Data\ResponseInterface;
use Klarna\Kp\Api\QuoteRepositoryInterface;
use Klarna\Kp\Api\SessionInitiatorInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;

/**
 * Class SessionInitiator
 *
 * @package Klarna\Kp\Model
 */
class SessionInitiator implements SessionInitiatorInterface
{
    /**
     * @var QuoteRepositoryInterface
     */
    private $quoteRepository;
    /**
     * @var LoggerInterface
     */
    private $log;
    /**
     * @var Session
     */
    private $session;
    /**
     * @var ScopeConfigInterface
     */
    private $config;

    /**
     * Constructor
     *
     * @param QuoteRepositoryInterface $quoteRepository
     * @param Session                  $session
     * @param LoggerInterface          $log
     * @param ScopeConfigInterface     $config
     */
    public function __construct(
        QuoteRepositoryInterface $quoteRepository,
        Session $session,
        LoggerInterface $log,
        ScopeConfigInterface $config
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->session = $session;
        $this->log = $log;
        $this->config = $config;
    }

    /**
     * {@inheritdoc}
     */
    public function checkAvailable($quote, $code)
    {
        if (!$quote) {
            $quote = $this->checkQuote();
        }

        /** @var ResponseInterface $klarnaPayments */
        try {
            $klarnaPayments = $this->session->getApiResponse();
            if ($klarnaPayments === null) {
                $sessionId = $this->getSessionId($quote);
                $klarnaPayments = $this->session->init($sessionId);
            }
            return $klarnaPayments->isSuccessfull() && $this->checkMethodAvailable($quote, $code);
        } catch (NoSuchEntityException $e) {
            $this->log->error($e);
            return false;
        } catch (KlarnaApiException $e) {
            $this->log->error($e);
            return false;
        } catch (KlarnaException $e) {
            $this->log->error($e);
            return true;
        }
    }

    /**
     * @param \Magento\Quote\Model\Quote $quote
     * @return \Magento\Quote\Model\Quote
     */
    private function checkQuote()
    {
        $quote = $this->session->getQuote();
        if (!$quote) {
            return null;
        }

        $version = $this->config->getValue('klarna/api/api_version', ScopeInterface::SCOPE_STORES, $quote->getStore());
        if (!in_array($version, ['kp_na', 'kp_eu'], true)) {
            return null;
        }
        return $quote;
    }

    /**
     * @param CartInterface $quote
     * @return string
     */
    private function getSessionId(CartInterface $quote)
    {
        try {
            return $this->quoteRepository->getActiveByQuote($quote)->getSessionId();
        } catch (NoSuchEntityException $e) {
            return null;
        }
    }

    /**
     * Check to ensure that this payment method is in the list of methods returned by API
     *
     * @param CartInterface $quote
     * @param string        $code
     * @return bool
     * @throws NoSuchEntityException
     */
    private function checkMethodAvailable(CartInterface $quote, $code)
    {
        $kQuote = $this->quoteRepository->getActiveByQuote($quote);
        return (in_array($code, $kQuote->getPaymentMethods()));
    }
}