AuthorizationProcessor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\AmazonAuthorizationDetailsFactory;
  21. use Amazon\Payment\Model\ResourceModel\PendingAuthorization\CollectionFactory;
  22. use Magento\Store\Model\StoreManagerInterface;
  23. class AuthorizationProcessor implements ProcessorInterface
  24. {
  25. /**
  26. * @var AmazonAuthorizationDetailsFactory
  27. */
  28. private $amazonAuthorizationDetailsFactory;
  29. /**
  30. * @var Authorization
  31. */
  32. private $authorization;
  33. /**
  34. * @var CollectionFactory
  35. */
  36. private $collectionFactory;
  37. /**
  38. * @var StoreManagerInterface
  39. */
  40. private $storeManager;
  41. public function __construct(
  42. AmazonAuthorizationDetailsFactory $amazonAuthorizationDetailsFactory,
  43. Authorization $authorization,
  44. CollectionFactory $collectionFactory,
  45. StoreManagerInterface $storeManager
  46. ) {
  47. $this->amazonAuthorizationDetailsFactory = $amazonAuthorizationDetailsFactory;
  48. $this->authorization = $authorization;
  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']) && 'PaymentAuthorize' === $ipnData['NotificationType']);
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function process(array $ipnData)
  63. {
  64. $details = $this->amazonAuthorizationDetailsFactory->create(
  65. [
  66. 'details' => $ipnData['AuthorizationDetails']
  67. ]
  68. );
  69. $collection = $this->collectionFactory
  70. ->create()
  71. ->addFieldToFilter(
  72. PendingAuthorizationInterface::AUTHORIZATION_ID,
  73. [
  74. 'eq' => $details->getAuthorizeTransactionId()
  75. ]
  76. )
  77. ->setPageSize(1)
  78. ->setCurPage(1);
  79. $collection->getSelect()
  80. ->join(['so' => $collection->getTable('sales_order')], 'main_table.order_id = so.entity_id', [])
  81. ->where('so.store_id = ?', $this->storeManager->getStore()->getId());
  82. if (count($items = $collection->getItems())) {
  83. $pendingAuthorization = current($items);
  84. $this->authorization->setThrowExceptions(true);
  85. $this->authorization->updateAuthorization($pendingAuthorization->getId(), $details);
  86. }
  87. }
  88. }