Kp.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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\Payment;
  11. use Klarna\Core\Helper\ConfigHelper;
  12. use Klarna\Kp\Api\KlarnaPaymentMethodInterface;
  13. use Klarna\Kp\Api\SessionInitiatorInterface;
  14. use Magento\Framework\App\Config\ScopeConfigInterface;
  15. use Magento\Framework\DataObject;
  16. use Magento\Framework\Locale\Resolver;
  17. use Magento\Payment\Model\InfoInterface;
  18. use Magento\Payment\Model\Method\Adapter;
  19. use Magento\Payment\Model\MethodInterface;
  20. use Magento\Quote\Api\Data\CartInterface;
  21. use Magento\Store\Model\ScopeInterface;
  22. /**
  23. * Klarna Payment
  24. *
  25. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  26. * @SuppressWarnings(PHPMD.TooManyPublicMethods)
  27. * @SuppressWarnings(PHPMD.TooManyMethods)
  28. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) //TODO: Remove in 6.0 when deprecated methods are dropped
  29. */
  30. class Kp implements MethodInterface, KlarnaPaymentMethodInterface
  31. {
  32. const METHOD_CODE = ConfigHelper::KP_METHOD_CODE;
  33. /**
  34. * @deprecated 5.3.0
  35. */
  36. const KLARNA_LOGO_SLICE_IT = 'https://cdn.klarna.com/1.0/shared/image/generic/badge/%s/slice_it/standard/pink.svg';
  37. /**
  38. * @deprecated 5.3.0
  39. */
  40. // @codingStandardsIgnoreLine
  41. const KLARNA_LOGO_PAY_LATER = 'https://cdn.klarna.com/1.0/shared/image/generic/badge/%s/pay_later/standard/pink.svg';
  42. /**
  43. * @deprecated 5.3.0
  44. */
  45. const KLARNA_LOGO_PAY_NOW = 'https://cdn.klarna.com/1.0/shared/image/generic/badge/%s/pay_now/standard/pink.svg';
  46. /**
  47. * @deprecated 5.3.0
  48. */
  49. const KLARNA_LOGO = 'https://cdn.klarna.com/1.0/shared/image/generic/badge/%s/%s/standard/pink.svg';
  50. /**
  51. * @deprecated 5.3.0
  52. */
  53. const KLARNA_METHODS = [
  54. 'klarna_pay_now',
  55. 'klarna_pay_later',
  56. 'klarna_pay_over_time',
  57. 'klarna_direct_debit',
  58. 'klarna_direct_bank_transfer'
  59. ];
  60. /**
  61. * @var Resolver
  62. */
  63. protected $resolver;
  64. /**
  65. * @var Adapter
  66. */
  67. private $adapter;
  68. /**
  69. * @var string
  70. */
  71. private $code = 'klarna_kp';
  72. /**
  73. * @var ScopeConfigInterface
  74. */
  75. private $config;
  76. /**
  77. * @var \Klarna\Kp\Model\SessionInitiatorFactory
  78. */
  79. private $sessionInitiatorFactory;
  80. /**
  81. * Kco constructor.
  82. *
  83. * @param Adapter $adapter
  84. * @param Resolver $resolver
  85. * @param ScopeConfigInterface $config
  86. * @param \Klarna\Kp\Model\SessionInitiatorFactory $sessionInitiatorFactory
  87. */
  88. public function __construct(
  89. Adapter $adapter,
  90. Resolver $resolver,
  91. ScopeConfigInterface $config,
  92. \Klarna\Kp\Model\SessionInitiatorFactory $sessionInitiatorFactory
  93. ) {
  94. $this->adapter = $adapter;
  95. $this->resolver = $resolver;
  96. $this->config = $config;
  97. $this->sessionInitiatorFactory = $sessionInitiatorFactory;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function isActive($storeId = null)
  103. {
  104. $scope = ($storeId === null ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORES);
  105. return $this->config->isSetFlag('payment/' . self::METHOD_CODE . '/active', $scope, $storeId);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getFormBlockType()
  111. {
  112. return $this->adapter->getFormBlockType();
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function setStore($storeId)
  118. {
  119. $this->adapter->setStore($storeId);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function getStore()
  125. {
  126. return $this->adapter->getStore();
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function canOrder()
  132. {
  133. return $this->adapter->canOrder();
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function canAuthorize()
  139. {
  140. return $this->adapter->canAuthorize();
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function canCapture()
  146. {
  147. return $this->adapter->canCapture();
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function canCapturePartial()
  153. {
  154. return $this->adapter->canCapturePartial();
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function canCaptureOnce()
  160. {
  161. return $this->adapter->canCaptureOnce();
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function canRefund()
  167. {
  168. return $this->adapter->canRefund();
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function canRefundPartialPerInvoice()
  174. {
  175. return $this->adapter->canRefundPartialPerInvoice();
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function canVoid()
  181. {
  182. return $this->adapter->canVoid();
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function canUseInternal()
  188. {
  189. return $this->adapter->canUseInternal();
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function canUseCheckout()
  195. {
  196. return $this->adapter->canUseCheckout();
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function canEdit()
  202. {
  203. return $this->adapter->canEdit();
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. public function canFetchTransactionInfo()
  209. {
  210. return $this->adapter->canFetchTransactionInfo();
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
  216. {
  217. return $this->adapter->fetchTransactionInfo($payment, $transactionId);
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function isGateway()
  223. {
  224. return $this->adapter->isGateway();
  225. }
  226. /**
  227. * {@inheritdoc}
  228. */
  229. public function isOffline()
  230. {
  231. return $this->adapter->isOffline();
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public function isInitializeNeeded()
  237. {
  238. return $this->adapter->isInitializeNeeded();
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function canUseForCountry($country)
  244. {
  245. return $this->adapter->canUseForCountry($country);
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function canUseForCurrency($currencyCode)
  251. {
  252. return $this->adapter->canUseForCurrency($currencyCode);
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function getInfoBlockType()
  258. {
  259. return $this->adapter->getInfoBlockType();
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function getInfoInstance()
  265. {
  266. return $this->adapter->getInfoInstance();
  267. }
  268. /**
  269. * {@inheritdoc}
  270. */
  271. public function setInfoInstance(InfoInterface $info)
  272. {
  273. $this->adapter->setInfoInstance($info);
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function validate()
  279. {
  280. $this->adapter->validate();
  281. return $this;
  282. }
  283. /**
  284. * {@inheritdoc}
  285. */
  286. public function order(InfoInterface $payment, $amount)
  287. {
  288. $this->adapter->order($payment, $amount);
  289. return $this;
  290. }
  291. /**
  292. * {@inheritdoc}
  293. */
  294. public function authorize(InfoInterface $payment, $amount)
  295. {
  296. $this->adapter->authorize($payment, $amount);
  297. return $this;
  298. }
  299. /**
  300. * {@inheritdoc}
  301. */
  302. public function capture(InfoInterface $payment, $amount)
  303. {
  304. $this->adapter->capture($payment, $amount);
  305. return $this;
  306. }
  307. /**
  308. * {@inheritdoc}
  309. */
  310. public function refund(InfoInterface $payment, $amount)
  311. {
  312. $this->adapter->refund($payment, $amount);
  313. return $this;
  314. }
  315. /**
  316. * {@inheritdoc}
  317. */
  318. public function cancel(InfoInterface $payment)
  319. {
  320. $this->adapter->cancel($payment);
  321. return $this;
  322. }
  323. /**
  324. * {@inheritdoc}
  325. */
  326. public function void(InfoInterface $payment)
  327. {
  328. $this->adapter->void($payment);
  329. return $this;
  330. }
  331. /**
  332. * {@inheritdoc}
  333. */
  334. public function canReviewPayment()
  335. {
  336. return $this->adapter->canReviewPayment();
  337. }
  338. /**
  339. * {@inheritdoc}
  340. */
  341. public function acceptPayment(InfoInterface $payment)
  342. {
  343. $this->adapter->acceptPayment($payment);
  344. return $this;
  345. }
  346. /**
  347. * {@inheritdoc}
  348. */
  349. public function denyPayment(InfoInterface $payment)
  350. {
  351. $this->adapter->denyPayment($payment);
  352. return $this;
  353. }
  354. /**
  355. * {@inheritdoc}
  356. */
  357. public function getConfigData($field, $storeId = null)
  358. {
  359. return $this->adapter->getConfigData($field, $storeId);
  360. }
  361. /**
  362. * {@inheritdoc}
  363. */
  364. public function getTitle()
  365. {
  366. return $this->adapter->getTitle();
  367. }
  368. /**
  369. * {@inheritdoc}
  370. */
  371. public function getLogoUrl()
  372. {
  373. $locale = strtolower($this->resolver->getLocale());
  374. $code = $this->getCode();
  375. $klarna_code = substr($code, 7);
  376. if ($klarna_code === 'pay_over_time') {
  377. $klarna_code = 'slice_it';
  378. }
  379. if ($klarna_code === 'direct_debit') {
  380. $klarna_code = 'pay_now';
  381. }
  382. if ($klarna_code === 'direct_bank_transfer') {
  383. $klarna_code = 'pay_now';
  384. }
  385. return sprintf(self::KLARNA_LOGO, $locale, $klarna_code);
  386. }
  387. /**
  388. * {@inheritdoc}
  389. */
  390. public function getCode()
  391. {
  392. return $this->code;
  393. }
  394. /**
  395. * {@inheritdoc}
  396. */
  397. public function setCode($code)
  398. {
  399. $this->code = $code;
  400. return $this;
  401. }
  402. /**
  403. * Get translated tag-line text
  404. *
  405. * @return \Magento\Framework\Phrase
  406. */
  407. public function getTagLine()
  408. {
  409. $code = $this->getCode();
  410. $text = 'Klarna Payments';
  411. switch ($code) {
  412. case 'klarna_pay_now':
  413. $text = 'Simple and secure.';
  414. break;
  415. case 'klarna_pay_later':
  416. $text = 'Pay X days after delivery';
  417. break;
  418. case 'klarna_pay_over_time':
  419. $text = 'Pay over time';
  420. break;
  421. case 'klarna_direct_debit':
  422. $text = 'Fast and simple';
  423. break;
  424. case 'klarna_direct_bank_transfer':
  425. $text = 'Simple and secure.';
  426. break;
  427. }
  428. return __($text);
  429. }
  430. /**
  431. * {@inheritdoc}
  432. */
  433. public function assignData(DataObject $data)
  434. {
  435. $this->adapter->assignData($data);
  436. return $this;
  437. }
  438. /**
  439. * {@inheritdoc}
  440. */
  441. public function isAvailable(CartInterface $quote = null)
  442. {
  443. $result = $this->adapter->isAvailable($quote);
  444. if (!$result) {
  445. return $result;
  446. }
  447. return $this->getSessionInitiator()->checkAvailable($quote, $this->getCode());
  448. }
  449. /**
  450. * @return SessionInitiatorInterface
  451. */
  452. private function getSessionInitiator()
  453. {
  454. return $this->sessionInitiatorFactory->create();
  455. }
  456. /**
  457. * {@inheritdoc}
  458. */
  459. public function initialize($paymentAction, $stateObject)
  460. {
  461. return $this->adapter->initialize($paymentAction, $stateObject);
  462. }
  463. /**
  464. * {@inheritdoc}
  465. */
  466. public function getConfigPaymentAction()
  467. {
  468. return $this->adapter->getConfigPaymentAction();
  469. }
  470. }