OrderProcessor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\PendingAuthorizationInterface;
  18. use Amazon\Payment\Api\Ipn\ProcessorInterface;
  19. use Amazon\Payment\Model\PaymentManagement\Authorization;
  20. use Amazon\Payment\Domain\Details\AmazonOrderDetailsFactory;
  21. use Amazon\Payment\Model\ResourceModel\OrderLink;
  22. use Amazon\Payment\Model\ResourceModel\PendingAuthorization\CollectionFactory;
  23. use Magento\Store\Model\StoreManagerInterface;
  24. class OrderProcessor implements ProcessorInterface
  25. {
  26. /**
  27. * @var AmazonOrderDetailsFactory
  28. */
  29. private $amazonOrderDetailsFactory;
  30. /**
  31. * @var AuthorizationInterface
  32. */
  33. private $authorization;
  34. /**
  35. * @var CollectionFactory
  36. */
  37. private $collectionFactory;
  38. /**
  39. * @var StoreManagerInterface
  40. */
  41. private $storeManager;
  42. public function __construct(
  43. AmazonOrderDetailsFactory $amazonOrderDetailsFactory,
  44. Authorization $authorization,
  45. CollectionFactory $collectionFactory,
  46. StoreManagerInterface $storeManager
  47. ) {
  48. $this->amazonOrderDetailsFactory = $amazonOrderDetailsFactory;
  49. $this->collectionFactory = $collectionFactory;
  50. $this->authorization = $authorization;
  51. $this->storeManager = $storeManager;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function supports(array $ipnData)
  57. {
  58. return (isset($ipnData['NotificationType']) && 'OrderReferenceNotification' === $ipnData['NotificationType']);
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function process(array $ipnData)
  64. {
  65. $details = $this->amazonOrderDetailsFactory->create([
  66. 'details' => $ipnData['OrderReference']
  67. ]);
  68. $collection = $this->collectionFactory
  69. ->create()
  70. ->addFieldToFilter(PendingAuthorizationInterface::PROCESSED, ['eq' => 1])
  71. ->setPageSize(1)
  72. ->setCurPage(1);
  73. $collection->getSelect()
  74. ->join(['so' => $collection->getTable('sales_order')], 'main_table.order_id = so.entity_id', [])
  75. ->where('so.store_id = ?', $this->storeManager->getStore()->getId())
  76. ->join(['ao' => $collection->getTable(OrderLink::TABLE_NAME)], 'main_table.order_id = ao.order_id', [])
  77. ->where('ao.amazon_order_reference_id = ?', $details->getOrderReferenceId());
  78. if (count($items = $collection->getItems())) {
  79. $pendingAuthorization = current($items);
  80. $this->authorization->setThrowExceptions(true);
  81. $this->authorization->updateAuthorization($pendingAuthorization->getId(), null, $details);
  82. }
  83. }
  84. }