RefundProcessor.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Model\Ipn;
  17. use Amazon\Payment\Api\Data\PendingRefundInterface;
  18. use Amazon\Payment\Api\Ipn\ProcessorInterface;
  19. use Amazon\Payment\Domain\Details\AmazonRefundDetailsFactory;
  20. use Amazon\Payment\Model\QueuedRefundUpdater;
  21. use Amazon\Payment\Model\ResourceModel\PendingRefund\CollectionFactory;
  22. use Magento\Store\Model\StoreManagerInterface;
  23. class RefundProcessor implements ProcessorInterface
  24. {
  25. /**
  26. * @var AmazonRefundDetailsFactory
  27. */
  28. private $amazonRefundDetailsFactory;
  29. /**
  30. * @var QueuedRefundUpdater
  31. */
  32. private $queuedRefundUpdater;
  33. /**
  34. * @var CollectionFactory
  35. */
  36. private $collectionFactory;
  37. /**
  38. * @var StoreManagerInterface
  39. */
  40. private $storeManager;
  41. public function __construct(
  42. AmazonRefundDetailsFactory $amazonRefundDetailsFactory,
  43. QueuedRefundUpdater $queuedRefundUpdater,
  44. CollectionFactory $collectionFactory,
  45. StoreManagerInterface $storeManager
  46. ) {
  47. $this->amazonRefundDetailsFactory = $amazonRefundDetailsFactory;
  48. $this->queuedRefundUpdater = $queuedRefundUpdater;
  49. $this->collectionFactory = $collectionFactory;
  50. $this->storeManager = $storeManager;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function supports(array $ipnData)
  56. {
  57. return (isset($ipnData['NotificationType']) && 'PaymentRefund' === $ipnData['NotificationType']);
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function process(array $ipnData)
  63. {
  64. $details = $this->amazonRefundDetailsFactory->create([
  65. 'details' => $ipnData['RefundDetails']
  66. ]);
  67. $collection = $this->collectionFactory
  68. ->create()
  69. ->addFieldToFilter(PendingRefundInterface::REFUND_ID, ['eq' => $details->getRefundId()])
  70. ->setPageSize(1)
  71. ->setCurPage(1);
  72. $collection->getSelect()
  73. ->join(['so' => $collection->getTable('sales_order')], 'main_table.order_id = so.entity_id', [])
  74. ->where('so.store_id = ?', $this->storeManager->getStore()->getId());
  75. if (count($items = $collection->getItems())) {
  76. $pendingRefund = current($items);
  77. $this->queuedRefundUpdater->setThrowExceptions(true);
  78. $this->queuedRefundUpdater->checkAndUpdateRefund($pendingRefund->getId(), $details);
  79. }
  80. }
  81. }