PaymentPlugin.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Plugin;
  7. use Magento\Payment\Model\InfoInterface;
  8. use Magento\Payment\Model\MethodInterface;
  9. use Magento\Signifyd\Api\GuaranteeCancelingServiceInterface;
  10. /**
  11. * Plugin for Magento\Payment\Model\MethodInterface.
  12. *
  13. * @see MethodInterface
  14. */
  15. class PaymentPlugin
  16. {
  17. /**
  18. * @var GuaranteeCancelingServiceInterface
  19. */
  20. private $guaranteeCancelingService;
  21. /**
  22. * @param GuaranteeCancelingServiceInterface $guaranteeCancelingService
  23. */
  24. public function __construct(
  25. GuaranteeCancelingServiceInterface $guaranteeCancelingService
  26. ) {
  27. $this->guaranteeCancelingService = $guaranteeCancelingService;
  28. }
  29. /**
  30. * Performs Signifyd guarantee cancel operation after payment denying.
  31. *
  32. * @see MethodInterface::denyPayment
  33. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  34. *
  35. * @param MethodInterface $subject
  36. * @param MethodInterface|bool $result
  37. * @param InfoInterface $payment
  38. * @return bool|MethodInterface
  39. */
  40. public function afterDenyPayment(MethodInterface $subject, $result, InfoInterface $payment)
  41. {
  42. if ($this->isPaymentDenied($payment, $result)) {
  43. $this->guaranteeCancelingService->cancelForOrder($payment->getParentId());
  44. }
  45. return $result;
  46. }
  47. /**
  48. * Checks if deny payment operation was successful.
  49. *
  50. * Result not false check for payment methods using AbstractMethod.
  51. * Transaction is closed check for payment methods using Gateway.
  52. *
  53. * @param InfoInterface $payment
  54. * @param MethodInterface $result
  55. * @return bool
  56. */
  57. private function isPaymentDenied($payment, $result)
  58. {
  59. return $result !== false || $payment->getIsTransactionClosed();
  60. }
  61. }