| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?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\Api\BuilderInterface;
- use Klarna\Core\Model\Api\Exception as KlarnaApiException;
- use Klarna\Kp\Api\CreditApiInterface;
- use Klarna\Kp\Api\Data\RequestInterface;
- use Klarna\Kp\Api\Data\ResponseInterface;
- use Klarna\Kp\Api\QuoteInterface;
- use Klarna\Kp\Api\QuoteRepositoryInterface;
- use Klarna\Kp\Model\QuoteFactory as KlarnaQuoteFactory;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Class Session
- *
- * @package Klarna\Kp\Model
- */
- class Session
- {
- /**
- * Request Builder
- *
- * @var BuilderInterface
- */
- private $builder;
- /**
- * Klarna Payments API
- *
- * @var CreditApiInterface
- */
- private $api;
- /**
- * Klarna Quote Repository
- *
- * @var QuoteRepositoryInterface
- */
- private $kQuoteRepository;
- /**
- * Klarna Quote Factory
- *
- * @var KlarnaQuoteFactory
- */
- private $klarnaQuoteFactory;
- /**
- * @var ResponseInterface
- */
- private $apiResponse;
- /**
- * @var QuoteInterface
- */
- private $klarnaQuote;
- /**
- * @var \Magento\Checkout\Model\Session
- */
- private $session;
- /**
- * Session constructor.
- *
- * @param \Magento\Checkout\Model\Session $session
- * @param CreditApiInterface $api
- * @param BuilderInterface $builder
- * @param QuoteRepositoryInterface $kQuoteRepository
- * @param KlarnaQuoteFactory $klarnaQuoteFactory
- */
- public function __construct(
- \Magento\Checkout\Model\Session $session,
- CreditApiInterface $api,
- BuilderInterface $builder,
- QuoteRepositoryInterface $kQuoteRepository,
- KlarnaQuoteFactory $klarnaQuoteFactory
- ) {
- $this->api = $api;
- $this->builder = $builder;
- $this->kQuoteRepository = $kQuoteRepository;
- $this->klarnaQuoteFactory = $klarnaQuoteFactory;
- $this->session = $session;
- }
- /**
- * Initialize Session
- *
- * @param string $sessionId
- * @return ResponseInterface
- * @throws \Klarna\Core\Model\Api\Exception
- * @throws \Klarna\Core\Exception
- */
- public function init($sessionId = null)
- {
- $klarnaResponse = $this->getApiResponse();
- if (!$klarnaResponse) {
- $klarnaResponse = $this->requestKlarnaSession($sessionId);
- $klarnaQuote = $this->generateKlarnaQuote($klarnaResponse);
- $this->setKlarnaQuote($klarnaQuote);
- $this->setApiResponse($klarnaResponse);
- }
- return $klarnaResponse;
- }
- /**
- * Get API Response
- *
- * @return ResponseInterface
- */
- public function getApiResponse()
- {
- return $this->apiResponse;
- }
- /**
- * Set API Response
- *
- * @param ResponseInterface $klarnaQuote
- * @return $this
- */
- public function setApiResponse($klarnaQuote)
- {
- $this->apiResponse = $klarnaQuote;
- return $this;
- }
- /**
- * Start a Klarna Session
- *
- * @param string $sessionId
- * @return ResponseInterface
- * @throws \Klarna\Core\Model\Api\Exception
- * @throws \Klarna\Core\Exception
- */
- private function requestKlarnaSession($sessionId = null)
- {
- if (null === $sessionId) {
- return $this->initWithoutSession();
- }
- return $this->initWithSession($sessionId);
- }
- /**
- * Create a new Klarna Session
- *
- * @return ResponseInterface
- * @throws \Klarna\Core\Model\Api\Exception
- * @throws \Klarna\Core\Exception
- */
- private function initWithoutSession()
- {
- $data = $this->getGeneratedCreateRequest();
- $klarnaResponse = $this->initKlarnaQuote($data);
- return $klarnaResponse;
- }
- /**
- * Get the create request
- *
- * @return RequestInterface|string[]
- * @throws \Klarna\Core\Exception
- */
- private function getGeneratedCreateRequest()
- {
- return $this->builder->setObject($this->getQuote())->generateRequest(BuilderInterface::GENERATE_TYPE_CREATE)
- ->getRequest();
- }
- /**
- * @return \Magento\Quote\Model\Quote
- */
- public function getQuote()
- {
- return $this->session->getQuote();
- }
- /**
- * Initialize Klarna Quote
- *
- * @param RequestInterface $data
- * @return \Klarna\Kp\Api\Data\ResponseInterface
- */
- private function initKlarnaQuote(RequestInterface $data)
- {
- try {
- $klarnaQuote = $this->kQuoteRepository->getActiveByQuote($this->getQuote());
- $sessionId = $klarnaQuote->getSessionId();
- if (null === $sessionId) {
- $this->kQuoteRepository->markInactive($klarnaQuote);
- return $this->api->createSession($data);
- }
- $resp = $this->updateOrCreateSession($sessionId, $data);
- if ($resp->getSessionId() !== $sessionId) {
- $this->kQuoteRepository->markInactive($klarnaQuote);
- }
- return $resp;
- } catch (NoSuchEntityException $e) {
- return $this->api->createSession($data);
- }
- }
- /**
- * Update existing session. Create a new session if update fails
- *
- * @param string $sessionId
- * @param RequestInterface $data
- * @return ResponseInterface
- */
- private function updateOrCreateSession($sessionId, RequestInterface $data)
- {
- $resp = $this->api->updateSession($sessionId, $data);
- if ($resp->isSuccessfull()) {
- return $resp;
- }
- return $this->api->createSession($data);
- }
- /**
- * Attempt to lookup existing session
- *
- * @param string $sessionId
- * @return ResponseInterface
- * @throws \Klarna\Core\Model\Api\Exception
- * @throws \Klarna\Core\Exception
- */
- private function initWithSession($sessionId)
- {
- $data = $this->getGeneratedCreateRequest();
- $klarnaResponse = $this->updateOrCreateSession($sessionId, $data);
- if (!$klarnaResponse->isSuccessfull()) {
- throw new KlarnaApiException(__('Unable to initialize Klarna payments session'));
- }
- return $klarnaResponse;
- }
- /**
- * Lookup or create Klarna Quote
- *
- * @param ResponseInterface $klarnaResponse
- * @return QuoteInterface
- */
- private function generateKlarnaQuote(ResponseInterface $klarnaResponse)
- {
- try {
- $klarnaQuote = $this->kQuoteRepository->getBySessionId($klarnaResponse->getSessionId());
- $klarnaQuote->setPaymentMethods(
- $this->extractPaymentMethods($klarnaResponse->getPaymentMethodCategories())
- );
- $klarnaQuote->setPaymentMethodInfo($klarnaResponse->getPaymentMethodCategories());
- $this->kQuoteRepository->save($klarnaQuote);
- return $klarnaQuote;
- } catch (NoSuchEntityException $e) {
- return $this->createNewQuote($klarnaResponse);
- }
- }
- /**
- * @param $quoteData
- * @return mixed
- */
- private function extractPaymentMethods($categories)
- {
- $payment_methods = [];
- foreach ($categories as $category) {
- $payment_methods[] = 'klarna_' . $category['identifier'];
- }
- return implode(',', $payment_methods);
- }
- /**
- * Create a new Klarna quote object
- *
- * @param ResponseInterface $resp
- * @return QuoteInterface
- */
- private function createNewQuote(ResponseInterface $resp)
- {
- if (!$this->getQuote()->getId()) {
- throw new KlarnaApiException(__('Unable to initialize Klarna payments session'));
- }
- /** @var QuoteInterface $klarnaQuote */
- $klarnaQuote = $this->klarnaQuoteFactory->create();
- $klarnaQuote->setSessionId($resp->getSessionId());
- $klarnaQuote->setClientToken($resp->getClientToken());
- $klarnaQuote->setIsActive(1);
- $klarnaQuote->setQuoteId($this->getQuote()->getId());
- $klarnaQuote->setPaymentMethods($this->extractPaymentMethods($resp->getPaymentMethodCategories()));
- $klarnaQuote->setPaymentMethodInfo($resp->getPaymentMethodCategories());
- $this->kQuoteRepository->save($klarnaQuote);
- return $klarnaQuote;
- }
- /**
- * Get Klarna Quote
- *
- * @return \Klarna\Kp\Api\QuoteInterface
- */
- public function getKlarnaQuote()
- {
- return $this->klarnaQuote;
- }
- /**
- * Set Klarna Quote
- *
- * @param QuoteInterface $klarnaQuote
- * @return $this
- */
- public function setKlarnaQuote(QuoteInterface $klarnaQuote)
- {
- $this->klarnaQuote = $klarnaQuote;
- return $this;
- }
- }
|