CancelingService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\Guarantee;
  7. use Magento\Signifyd\Api\CaseManagementInterface;
  8. use Magento\Signifyd\Api\GuaranteeCancelingServiceInterface;
  9. use Magento\Signifyd\Model\CaseServices\UpdatingServiceFactory;
  10. use Magento\Signifyd\Model\SignifydGateway\Gateway;
  11. use Magento\Signifyd\Model\SignifydGateway\GatewayException;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Sends request to Signifyd to cancel guarantee and updates case entity.
  15. */
  16. class CancelingService implements GuaranteeCancelingServiceInterface
  17. {
  18. /**
  19. * @var CaseManagementInterface
  20. */
  21. private $caseManagement;
  22. /**
  23. * @var UpdatingServiceFactory
  24. */
  25. private $serviceFactory;
  26. /**
  27. * @var Gateway
  28. */
  29. private $gateway;
  30. /**
  31. * @var CancelGuaranteeAbility
  32. */
  33. private $cancelGuaranteeAbility;
  34. /**
  35. * @var LoggerInterface
  36. */
  37. private $logger;
  38. /**
  39. * CancelingService constructor.
  40. *
  41. * @param CaseManagementInterface $caseManagement
  42. * @param UpdatingServiceFactory $serviceFactory
  43. * @param Gateway $gateway
  44. * @param CancelGuaranteeAbility $cancelGuaranteeAbility
  45. * @param LoggerInterface $logger
  46. */
  47. public function __construct(
  48. CaseManagementInterface $caseManagement,
  49. UpdatingServiceFactory $serviceFactory,
  50. Gateway $gateway,
  51. CancelGuaranteeAbility $cancelGuaranteeAbility,
  52. LoggerInterface $logger
  53. ) {
  54. $this->caseManagement = $caseManagement;
  55. $this->serviceFactory = $serviceFactory;
  56. $this->gateway = $gateway;
  57. $this->cancelGuaranteeAbility = $cancelGuaranteeAbility;
  58. $this->logger = $logger;
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function cancelForOrder($orderId)
  64. {
  65. if (!$this->cancelGuaranteeAbility->isAvailable($orderId)) {
  66. return false;
  67. }
  68. $caseEntity = $this->caseManagement->getByOrderId($orderId);
  69. try {
  70. $disposition = $this->gateway->cancelGuarantee($caseEntity->getCaseId());
  71. } catch (GatewayException $e) {
  72. $this->logger->error($e->getMessage());
  73. return false;
  74. }
  75. $updatingService = $this->serviceFactory->create('guarantees/cancel');
  76. $data = [
  77. 'guaranteeDisposition' => $disposition
  78. ];
  79. $updatingService->update($caseEntity, $data);
  80. return true;
  81. }
  82. }