ProcessAmazonRefunds.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Cron;
  17. use Amazon\Core\Helper\Data;
  18. use Amazon\Core\Model\Config\Source\UpdateMechanism;
  19. use Amazon\Payment\Api\Data\PendingRefundInterface;
  20. use Amazon\Payment\Model\QueuedRefundUpdaterFactory;
  21. use Amazon\Payment\Model\ResourceModel\PendingRefund\CollectionFactory;
  22. use Magento\Framework\Data\Collection;
  23. class ProcessAmazonRefunds
  24. {
  25. /**
  26. * @var int
  27. */
  28. private $limit;
  29. /**
  30. * @var CollectionFactory
  31. */
  32. private $queuedRefundsCollectionFactory;
  33. /**
  34. * @var QueuedRefundUpdaterFactory
  35. */
  36. private $queuedRefundUpdater;
  37. /**
  38. * @var Data
  39. */
  40. private $coreHelper;
  41. /**
  42. * @param CollectionFactory $collectionFactory
  43. * @param QueuedRefundUpdaterFactory $queuedRefundUpdater
  44. * @param int $limit
  45. */
  46. public function __construct(
  47. CollectionFactory $collectionFactory,
  48. QueuedRefundUpdaterFactory $queuedRefundUpdater,
  49. Data $coreHelper,
  50. $limit = 100
  51. ) {
  52. $this->queuedRefundsCollectionFactory = $collectionFactory;
  53. $this->queuedRefundUpdater = $queuedRefundUpdater;
  54. $this->coreHelper = $coreHelper;
  55. $limit = (int)$limit;
  56. if ($limit < 1) {
  57. throw new \InvalidArgumentException('Limit must be greater than 1.');
  58. }
  59. $this->limit = $limit;
  60. }
  61. public function execute()
  62. {
  63. if (UpdateMechanism::IPN === $this->coreHelper->getUpdateMechanism()) {
  64. return;
  65. }
  66. $collection = $this->queuedRefundsCollectionFactory
  67. ->create()
  68. ->addOrder(PendingRefundInterface::CREATED_AT, Collection::SORT_ORDER_ASC)
  69. ->setPageSize($this->limit)
  70. ->setCurPage(1);
  71. $queuedRefundUpdater = $this->queuedRefundUpdater->create();
  72. foreach ($collection->getIdGenerator() as $pendingRefundId) {
  73. $queuedRefundUpdater->checkAndUpdateRefund($pendingRefundId);
  74. }
  75. }
  76. }