PaymentVerificationFactory.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Payment\Api\PaymentVerificationInterface;
  9. use Magento\Payment\Gateway\ConfigInterface;
  10. use Magento\Framework\Exception\ConfigurationMismatchException;
  11. /**
  12. * Creates verification service for provided payment method, or PaymentVerificationInterface::class
  13. * if payment method does not support AVS, CVV verifications.
  14. */
  15. class PaymentVerificationFactory
  16. {
  17. /**
  18. * @var ConfigInterface
  19. */
  20. private $config;
  21. /**
  22. * @var ObjectManagerInterface
  23. */
  24. private $objectManager;
  25. /**
  26. * @var PaymentVerificationInterface
  27. */
  28. private $avsDefaultAdapter;
  29. /**
  30. * @var PaymentVerificationInterface
  31. */
  32. private $cvvDefaultAdapter;
  33. /**
  34. * @param ObjectManagerInterface $objectManager
  35. * @param ConfigInterface|Config $config
  36. * @param PaymentVerificationInterface $avsDefaultAdapter
  37. * @param PaymentVerificationInterface $cvvDefaultAdapter
  38. */
  39. public function __construct(
  40. ObjectManagerInterface $objectManager,
  41. ConfigInterface $config,
  42. PaymentVerificationInterface $avsDefaultAdapter,
  43. PaymentVerificationInterface $cvvDefaultAdapter
  44. ) {
  45. $this->config = $config;
  46. $this->objectManager = $objectManager;
  47. $this->avsDefaultAdapter = $avsDefaultAdapter;
  48. $this->cvvDefaultAdapter = $cvvDefaultAdapter;
  49. }
  50. /**
  51. * Creates instance of CVV code verification.
  52. * Exception will be thrown if CVV mapper does not implement PaymentVerificationInterface.
  53. *
  54. * @param string $paymentCode
  55. * @return PaymentVerificationInterface
  56. * @throws ConfigurationMismatchException
  57. */
  58. public function createPaymentCvv($paymentCode)
  59. {
  60. return $this->create($this->cvvDefaultAdapter, $paymentCode, 'cvv_ems_adapter');
  61. }
  62. /**
  63. * Creates instance of AVS code verification.
  64. * Exception will be thrown if AVS mapper does not implement PaymentVerificationInterface.
  65. *
  66. * @param string $paymentCode
  67. * @return PaymentVerificationInterface
  68. * @throws ConfigurationMismatchException
  69. */
  70. public function createPaymentAvs($paymentCode)
  71. {
  72. return $this->create($this->avsDefaultAdapter, $paymentCode, 'avs_ems_adapter');
  73. }
  74. /**
  75. * Creates instance of PaymentVerificationInterface.
  76. * Default implementation will be returned if payment method does not implement PaymentVerificationInterface.
  77. *
  78. * @param PaymentVerificationInterface $defaultAdapter
  79. * @param string $paymentCode
  80. * @param string $configKey
  81. * @return PaymentVerificationInterface
  82. * @throws ConfigurationMismatchException If payment verification instance
  83. * does not implement PaymentVerificationInterface.
  84. */
  85. private function create(PaymentVerificationInterface $defaultAdapter, $paymentCode, $configKey)
  86. {
  87. $this->config->setMethodCode($paymentCode);
  88. $verificationClass = $this->config->getValue($configKey);
  89. if ($verificationClass === null) {
  90. return $defaultAdapter;
  91. }
  92. $mapper = $this->objectManager->create($verificationClass);
  93. if (!$mapper instanceof PaymentVerificationInterface) {
  94. throw new ConfigurationMismatchException(
  95. __('%1 must implement %2', $verificationClass, PaymentVerificationInterface::class)
  96. );
  97. }
  98. return $mapper;
  99. }
  100. }