SpecificationPlugin.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Method\Checks;
  7. use Magento\Payment\Model\MethodInterface;
  8. use Magento\Payment\Model\Checks\SpecificationInterface;
  9. use Magento\Paypal\Model\Config;
  10. use Magento\Paypal\Model\Billing\AgreementFactory;
  11. use Magento\Quote\Model\Quote;
  12. /**
  13. * Plugin for \Magento\Payment\Model\Checks\Composite
  14. */
  15. class SpecificationPlugin
  16. {
  17. /**
  18. * @var AgreementFactory
  19. */
  20. private $agreementFactory;
  21. /**
  22. * @param AgreementFactory $agreementFactory
  23. */
  24. public function __construct(AgreementFactory $agreementFactory)
  25. {
  26. $this->agreementFactory = $agreementFactory;
  27. }
  28. /**
  29. * Override check for Billing Agreements
  30. *
  31. * @param SpecificationInterface $specification
  32. * @param bool $result
  33. * @param MethodInterface $paymentMethod
  34. * @param Quote $quote
  35. * @return bool
  36. *
  37. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  38. */
  39. public function afterIsApplicable(
  40. SpecificationInterface $specification,
  41. $result,
  42. MethodInterface $paymentMethod,
  43. Quote $quote
  44. ) {
  45. if (!$result) {
  46. return false;
  47. }
  48. if ($paymentMethod->getCode() == Config::METHOD_BILLING_AGREEMENT) {
  49. if ($quote->getCustomerId()) {
  50. $availableBA = $this->agreementFactory->create()->getAvailableCustomerBillingAgreements(
  51. $quote->getCustomerId()
  52. );
  53. return count($availableBA) > 0;
  54. }
  55. return false;
  56. }
  57. return true;
  58. }
  59. }