Session.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Kp\Model;
  11. use Klarna\Core\Api\BuilderInterface;
  12. use Klarna\Core\Model\Api\Exception as KlarnaApiException;
  13. use Klarna\Kp\Api\CreditApiInterface;
  14. use Klarna\Kp\Api\Data\RequestInterface;
  15. use Klarna\Kp\Api\Data\ResponseInterface;
  16. use Klarna\Kp\Api\QuoteInterface;
  17. use Klarna\Kp\Api\QuoteRepositoryInterface;
  18. use Klarna\Kp\Model\QuoteFactory as KlarnaQuoteFactory;
  19. use Magento\Framework\Exception\NoSuchEntityException;
  20. /**
  21. * Class Session
  22. *
  23. * @package Klarna\Kp\Model
  24. */
  25. class Session
  26. {
  27. /**
  28. * Request Builder
  29. *
  30. * @var BuilderInterface
  31. */
  32. private $builder;
  33. /**
  34. * Klarna Payments API
  35. *
  36. * @var CreditApiInterface
  37. */
  38. private $api;
  39. /**
  40. * Klarna Quote Repository
  41. *
  42. * @var QuoteRepositoryInterface
  43. */
  44. private $kQuoteRepository;
  45. /**
  46. * Klarna Quote Factory
  47. *
  48. * @var KlarnaQuoteFactory
  49. */
  50. private $klarnaQuoteFactory;
  51. /**
  52. * @var ResponseInterface
  53. */
  54. private $apiResponse;
  55. /**
  56. * @var QuoteInterface
  57. */
  58. private $klarnaQuote;
  59. /**
  60. * @var \Magento\Checkout\Model\Session
  61. */
  62. private $session;
  63. /**
  64. * Session constructor.
  65. *
  66. * @param \Magento\Checkout\Model\Session $session
  67. * @param CreditApiInterface $api
  68. * @param BuilderInterface $builder
  69. * @param QuoteRepositoryInterface $kQuoteRepository
  70. * @param KlarnaQuoteFactory $klarnaQuoteFactory
  71. */
  72. public function __construct(
  73. \Magento\Checkout\Model\Session $session,
  74. CreditApiInterface $api,
  75. BuilderInterface $builder,
  76. QuoteRepositoryInterface $kQuoteRepository,
  77. KlarnaQuoteFactory $klarnaQuoteFactory
  78. ) {
  79. $this->api = $api;
  80. $this->builder = $builder;
  81. $this->kQuoteRepository = $kQuoteRepository;
  82. $this->klarnaQuoteFactory = $klarnaQuoteFactory;
  83. $this->session = $session;
  84. }
  85. /**
  86. * Initialize Session
  87. *
  88. * @param string $sessionId
  89. * @return ResponseInterface
  90. * @throws \Klarna\Core\Model\Api\Exception
  91. * @throws \Klarna\Core\Exception
  92. */
  93. public function init($sessionId = null)
  94. {
  95. $klarnaResponse = $this->getApiResponse();
  96. if (!$klarnaResponse) {
  97. $klarnaResponse = $this->requestKlarnaSession($sessionId);
  98. $klarnaQuote = $this->generateKlarnaQuote($klarnaResponse);
  99. $this->setKlarnaQuote($klarnaQuote);
  100. $this->setApiResponse($klarnaResponse);
  101. }
  102. return $klarnaResponse;
  103. }
  104. /**
  105. * Get API Response
  106. *
  107. * @return ResponseInterface
  108. */
  109. public function getApiResponse()
  110. {
  111. return $this->apiResponse;
  112. }
  113. /**
  114. * Set API Response
  115. *
  116. * @param ResponseInterface $klarnaQuote
  117. * @return $this
  118. */
  119. public function setApiResponse($klarnaQuote)
  120. {
  121. $this->apiResponse = $klarnaQuote;
  122. return $this;
  123. }
  124. /**
  125. * Start a Klarna Session
  126. *
  127. * @param string $sessionId
  128. * @return ResponseInterface
  129. * @throws \Klarna\Core\Model\Api\Exception
  130. * @throws \Klarna\Core\Exception
  131. */
  132. private function requestKlarnaSession($sessionId = null)
  133. {
  134. if (null === $sessionId) {
  135. return $this->initWithoutSession();
  136. }
  137. return $this->initWithSession($sessionId);
  138. }
  139. /**
  140. * Create a new Klarna Session
  141. *
  142. * @return ResponseInterface
  143. * @throws \Klarna\Core\Model\Api\Exception
  144. * @throws \Klarna\Core\Exception
  145. */
  146. private function initWithoutSession()
  147. {
  148. $data = $this->getGeneratedCreateRequest();
  149. $klarnaResponse = $this->initKlarnaQuote($data);
  150. return $klarnaResponse;
  151. }
  152. /**
  153. * Get the create request
  154. *
  155. * @return RequestInterface|string[]
  156. * @throws \Klarna\Core\Exception
  157. */
  158. private function getGeneratedCreateRequest()
  159. {
  160. return $this->builder->setObject($this->getQuote())->generateRequest(BuilderInterface::GENERATE_TYPE_CREATE)
  161. ->getRequest();
  162. }
  163. /**
  164. * @return \Magento\Quote\Model\Quote
  165. */
  166. public function getQuote()
  167. {
  168. return $this->session->getQuote();
  169. }
  170. /**
  171. * Initialize Klarna Quote
  172. *
  173. * @param RequestInterface $data
  174. * @return \Klarna\Kp\Api\Data\ResponseInterface
  175. */
  176. private function initKlarnaQuote(RequestInterface $data)
  177. {
  178. try {
  179. $klarnaQuote = $this->kQuoteRepository->getActiveByQuote($this->getQuote());
  180. $sessionId = $klarnaQuote->getSessionId();
  181. if (null === $sessionId) {
  182. $this->kQuoteRepository->markInactive($klarnaQuote);
  183. return $this->api->createSession($data);
  184. }
  185. $resp = $this->updateOrCreateSession($sessionId, $data);
  186. if ($resp->getSessionId() !== $sessionId) {
  187. $this->kQuoteRepository->markInactive($klarnaQuote);
  188. }
  189. return $resp;
  190. } catch (NoSuchEntityException $e) {
  191. return $this->api->createSession($data);
  192. }
  193. }
  194. /**
  195. * Update existing session. Create a new session if update fails
  196. *
  197. * @param string $sessionId
  198. * @param RequestInterface $data
  199. * @return ResponseInterface
  200. */
  201. private function updateOrCreateSession($sessionId, RequestInterface $data)
  202. {
  203. $resp = $this->api->updateSession($sessionId, $data);
  204. if ($resp->isSuccessfull()) {
  205. return $resp;
  206. }
  207. return $this->api->createSession($data);
  208. }
  209. /**
  210. * Attempt to lookup existing session
  211. *
  212. * @param string $sessionId
  213. * @return ResponseInterface
  214. * @throws \Klarna\Core\Model\Api\Exception
  215. * @throws \Klarna\Core\Exception
  216. */
  217. private function initWithSession($sessionId)
  218. {
  219. $data = $this->getGeneratedCreateRequest();
  220. $klarnaResponse = $this->updateOrCreateSession($sessionId, $data);
  221. if (!$klarnaResponse->isSuccessfull()) {
  222. throw new KlarnaApiException(__('Unable to initialize Klarna payments session'));
  223. }
  224. return $klarnaResponse;
  225. }
  226. /**
  227. * Lookup or create Klarna Quote
  228. *
  229. * @param ResponseInterface $klarnaResponse
  230. * @return QuoteInterface
  231. */
  232. private function generateKlarnaQuote(ResponseInterface $klarnaResponse)
  233. {
  234. try {
  235. $klarnaQuote = $this->kQuoteRepository->getBySessionId($klarnaResponse->getSessionId());
  236. $klarnaQuote->setPaymentMethods(
  237. $this->extractPaymentMethods($klarnaResponse->getPaymentMethodCategories())
  238. );
  239. $klarnaQuote->setPaymentMethodInfo($klarnaResponse->getPaymentMethodCategories());
  240. $this->kQuoteRepository->save($klarnaQuote);
  241. return $klarnaQuote;
  242. } catch (NoSuchEntityException $e) {
  243. return $this->createNewQuote($klarnaResponse);
  244. }
  245. }
  246. /**
  247. * @param $quoteData
  248. * @return mixed
  249. */
  250. private function extractPaymentMethods($categories)
  251. {
  252. $payment_methods = [];
  253. foreach ($categories as $category) {
  254. $payment_methods[] = 'klarna_' . $category['identifier'];
  255. }
  256. return implode(',', $payment_methods);
  257. }
  258. /**
  259. * Create a new Klarna quote object
  260. *
  261. * @param ResponseInterface $resp
  262. * @return QuoteInterface
  263. */
  264. private function createNewQuote(ResponseInterface $resp)
  265. {
  266. if (!$this->getQuote()->getId()) {
  267. throw new KlarnaApiException(__('Unable to initialize Klarna payments session'));
  268. }
  269. /** @var QuoteInterface $klarnaQuote */
  270. $klarnaQuote = $this->klarnaQuoteFactory->create();
  271. $klarnaQuote->setSessionId($resp->getSessionId());
  272. $klarnaQuote->setClientToken($resp->getClientToken());
  273. $klarnaQuote->setIsActive(1);
  274. $klarnaQuote->setQuoteId($this->getQuote()->getId());
  275. $klarnaQuote->setPaymentMethods($this->extractPaymentMethods($resp->getPaymentMethodCategories()));
  276. $klarnaQuote->setPaymentMethodInfo($resp->getPaymentMethodCategories());
  277. $this->kQuoteRepository->save($klarnaQuote);
  278. return $klarnaQuote;
  279. }
  280. /**
  281. * Get Klarna Quote
  282. *
  283. * @return \Klarna\Kp\Api\QuoteInterface
  284. */
  285. public function getKlarnaQuote()
  286. {
  287. return $this->klarnaQuote;
  288. }
  289. /**
  290. * Set Klarna Quote
  291. *
  292. * @param QuoteInterface $klarnaQuote
  293. * @return $this
  294. */
  295. public function setKlarnaQuote(QuoteInterface $klarnaQuote)
  296. {
  297. $this->klarnaQuote = $klarnaQuote;
  298. return $this;
  299. }
  300. }