Payments.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\Api\Rest\Service;
  11. use Klarna\Core\Api\ServiceInterface;
  12. use Klarna\Core\Helper\ConfigHelper;
  13. use Klarna\Core\Helper\KlarnaConfig;
  14. use Klarna\Core\Helper\VersionInfo;
  15. use Klarna\Core\Model\Api\Exception as KlarnaApiException;
  16. use Klarna\Kp\Api\CreditApiInterface;
  17. use Klarna\Kp\Api\Data\RequestInterface;
  18. use Klarna\Kp\Api\Data\ResponseInterface;
  19. use Klarna\Kp\Model\Api\Response;
  20. use Klarna\Kp\Model\Api\ResponseFactory;
  21. use Magento\Framework\App\Config\ScopeConfigInterface;
  22. use Magento\Store\Api\Data\StoreInterface;
  23. use Magento\Store\Model\ScopeInterface;
  24. use Magento\Store\Model\StoreManagerInterface;
  25. use Psr\Log\LoggerInterface;
  26. /**
  27. * Class Payments
  28. *
  29. * @package Klarna\Kp\Model\Api\Rest\Service
  30. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  31. */
  32. class Payments implements CreditApiInterface
  33. {
  34. const API_VERSION = 'v1';
  35. /**
  36. * @var ServiceInterface
  37. */
  38. private $service;
  39. /**
  40. * @var VersionInfo
  41. */
  42. private $versionInfo;
  43. /**
  44. * @var LoggerInterface $log
  45. */
  46. private $log;
  47. /**
  48. * @var StoreInterface
  49. */
  50. private $store;
  51. /**
  52. * @var ConfigHelper
  53. */
  54. private $configHelper;
  55. /**
  56. * @var ResponseFactory
  57. */
  58. private $responseFactory;
  59. /**
  60. * @var ScopeConfigInterface
  61. */
  62. private $config;
  63. /**
  64. * @var KlarnaConfig
  65. */
  66. private $klarnaConfig;
  67. /**
  68. * Kasper constructor.
  69. *
  70. * @param ScopeConfigInterface $config
  71. * @param StoreManagerInterface $storeManager
  72. * @param LoggerInterface $log
  73. * @param VersionInfo $versionInfo
  74. * @param ResponseFactory $responseFactory
  75. * @param ConfigHelper $configHelper
  76. * @param KlarnaConfig $klarnaConfig
  77. * @param ServiceInterface $service
  78. */
  79. public function __construct(
  80. ScopeConfigInterface $config,
  81. StoreManagerInterface $storeManager,
  82. LoggerInterface $log,
  83. VersionInfo $versionInfo,
  84. ResponseFactory $responseFactory,
  85. ConfigHelper $configHelper,
  86. KlarnaConfig $klarnaConfig,
  87. ServiceInterface $service
  88. ) {
  89. $this->log = $log;
  90. $this->service = $service;
  91. $this->responseFactory = $responseFactory;
  92. $this->store = $storeManager->getStore();
  93. $this->config = $config;
  94. $this->versionInfo = $versionInfo;
  95. $this->configHelper = $configHelper;
  96. $this->klarnaConfig = $klarnaConfig;
  97. }
  98. /**
  99. * @param RequestInterface $request
  100. * @return ResponseInterface
  101. * @throws KlarnaApiException
  102. * @throws \Klarna\Core\Exception
  103. */
  104. public function createSession(RequestInterface $request)
  105. {
  106. return $this->processRequest('/payments/' . self::API_VERSION . '/sessions', $request);
  107. }
  108. /**
  109. * @param string $url
  110. * @param RequestInterface $request
  111. * @param string $method
  112. * @param string $klarnaId
  113. * @return Response
  114. * @throws \Klarna\Core\Exception
  115. */
  116. private function processRequest(
  117. $url,
  118. RequestInterface $request = null,
  119. $method = ServiceInterface::POST,
  120. $klarnaId = null
  121. ) {
  122. $body = '';
  123. if ($request) {
  124. $body = $request->toArray();
  125. }
  126. $this->connect();
  127. $response = $this->service->makeRequest($url, $body, $method, $klarnaId);
  128. $response['response_code'] = $response['response_status_code'];
  129. return $this->responseFactory->create(['data' => $response]);
  130. }
  131. /**
  132. * @return string
  133. * @throws \Klarna\Core\Exception
  134. */
  135. private function connect()
  136. {
  137. $version = sprintf(
  138. '%s;Core/%s;OM/%s',
  139. $this->versionInfo->getVersion('Klarna_Kp'),
  140. $this->versionInfo->getVersion('Klarna_Core'),
  141. $this->versionInfo->getVersion('Klarna_Ordermanagement')
  142. );
  143. $mageMode = $this->versionInfo->getMageMode();
  144. $mageVersion = $this->versionInfo->getMageEdition() . '/' . $this->versionInfo->getMageVersion();
  145. $mageInfo = "Magento {$mageVersion} {$mageMode} mode";
  146. $this->service->setUserAgent('Magento2_KP', $version, $mageInfo);
  147. $this->service->setHeader('Accept', '*/*');
  148. $username = $this->config->getValue('klarna/api/merchant_id', ScopeInterface::SCOPE_STORES, $this->store);
  149. $password = $this->config->getValue('klarna/api/shared_secret', ScopeInterface::SCOPE_STORES, $this->store);
  150. $test_mode = $this->config->getValue('klarna/api/test_mode', ScopeInterface::SCOPE_STORES, $this->store);
  151. $versionConfig = $this->klarnaConfig->getVersionConfig($this->store);
  152. $url = $versionConfig->getUrl($test_mode);
  153. $this->service->connect($username, $password, $url);
  154. }
  155. /**
  156. * @param string $sessionId
  157. * @param RequestInterface $request
  158. * @return ResponseInterface
  159. * @throws KlarnaApiException
  160. * @throws \Klarna\Core\Exception
  161. */
  162. public function updateSession($sessionId, RequestInterface $request)
  163. {
  164. $response = $this->processRequest(
  165. '/payments/' . self::API_VERSION . '/sessions/' . $sessionId,
  166. $request,
  167. ServiceInterface::POST,
  168. $sessionId
  169. );
  170. if ($response->getResponseCode() === 204) {
  171. return $this->readSession($sessionId);
  172. }
  173. return $response;
  174. }
  175. /**
  176. * @param string $sessionId
  177. * @param RequestInterface $request
  178. * @return ResponseInterface
  179. * @throws KlarnaApiException
  180. * @throws \Klarna\Core\Exception
  181. */
  182. public function readSession($sessionId)
  183. {
  184. $resp = $this->processRequest(
  185. '/payments/' . self::API_VERSION . '/sessions/' . $sessionId,
  186. null,
  187. ServiceInterface::GET,
  188. $sessionId
  189. );
  190. $response = $resp->toArray();
  191. $response['session_id'] = $sessionId;
  192. return $this->responseFactory->create(['data' => $response]);
  193. }
  194. /**
  195. * @param string $authorization_token
  196. * @param RequestInterface $request
  197. * @param null $klarnaId
  198. * @return ResponseInterface
  199. * @throws KlarnaApiException
  200. * @throws \Klarna\Core\Exception
  201. */
  202. public function placeOrder($authorization_token, RequestInterface $request, $klarnaId = null)
  203. {
  204. return $this->processRequest(
  205. '/payments/' . self::API_VERSION . '/authorizations/' . $authorization_token . '/order',
  206. $request,
  207. ServiceInterface::POST,
  208. $klarnaId
  209. );
  210. }
  211. /**
  212. * @param string $authorization_token
  213. * @param null $klarnaId
  214. * @return ResponseInterface
  215. * @throws KlarnaApiException
  216. * @throws \Klarna\Core\Exception
  217. */
  218. public function cancelOrder($authorization_token, $klarnaId = null)
  219. {
  220. return $this->processRequest(
  221. '/payments/' . self::API_VERSION . '/authorizations/' . $authorization_token,
  222. null,
  223. ServiceInterface::DELETE,
  224. $klarnaId
  225. );
  226. }
  227. }