123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- /**
- * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License").
- * You may not use this file except in compliance with the License.
- * A copy of the License is located at
- *
- * http://aws.amazon.com/apache2.0
- *
- * or in the "license" file accompanying this file. This file is distributed
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
- * express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
- namespace Amazon\Payment\Model;
- use Amazon\Payment\Api\Data\PendingAuthorizationInterfaceFactory;
- use Amazon\Payment\Api\Data\PendingCaptureInterfaceFactory;
- use Amazon\Payment\Api\Data\PendingRefundInterfaceFactory;
- use Amazon\Payment\Domain\Details\AmazonAuthorizationDetails;
- use Amazon\Payment\Domain\Details\AmazonCaptureDetails;
- use Amazon\Payment\Domain\Details\AmazonRefundDetails;
- use Magento\Framework\Api\SearchCriteriaBuilderFactory;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Payment\Model\InfoInterface as PaymentInfoInterface;
- use Magento\Sales\Api\Data\OrderInterface;
- use Magento\Sales\Api\Data\TransactionInterface;
- use Magento\Sales\Api\OrderPaymentRepositoryInterface;
- use Magento\Sales\Api\OrderRepositoryInterface;
- use Magento\Sales\Api\TransactionRepositoryInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class PaymentManagement
- {
- /**
- * @var PendingCaptureInterfaceFactory
- */
- private $pendingCaptureFactory;
- /**
- * @var PendingAuthorizationInterfaceFactory
- */
- private $pendingAuthorizationFactory;
- /**
- * @var PendingRefundInterfaceFactory
- */
- private $pendingRefundFactory;
- /**
- * @var SearchCriteriaBuilderFactory
- */
- private $searchCriteriaBuilderFactory;
- /**
- * @var OrderPaymentRepositoryInterface
- */
- private $orderPaymentRepository;
- /**
- * @var OrderRepositoryInterface
- */
- private $orderRepository;
- /**
- * @var TransactionRepositoryInterface
- */
- private $transactionRepository;
- /**
- * PaymentManagement constructor.
- *
- * @param PendingCaptureInterfaceFactory $pendingCaptureFactory
- * @param PendingAuthorizationInterfaceFactory $pendingAuthorizationFactory
- * @param PendingRefundInterfaceFactory $pendingRefundFactory
- * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
- * @param OrderPaymentRepositoryInterface $orderPaymentRepository
- * @param OrderRepositoryInterface $orderRepository
- * @param TransactionRepositoryInterface $transactionRepository
- */
- public function __construct(
- PendingCaptureInterfaceFactory $pendingCaptureFactory,
- PendingAuthorizationInterfaceFactory $pendingAuthorizationFactory,
- PendingRefundInterfaceFactory $pendingRefundFactory,
- SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
- OrderPaymentRepositoryInterface $orderPaymentRepository,
- OrderRepositoryInterface $orderRepository,
- TransactionRepositoryInterface $transactionRepository
- ) {
- $this->pendingCaptureFactory = $pendingCaptureFactory;
- $this->pendingAuthorizationFactory = $pendingAuthorizationFactory;
- $this->pendingRefundFactory = $pendingRefundFactory;
- $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
- $this->orderPaymentRepository = $orderPaymentRepository;
- $this->orderRepository = $orderRepository;
- $this->transactionRepository = $transactionRepository;
- }
- /**
- * {@inheritdoc}
- */
- public function queuePendingCapture(AmazonCaptureDetails $details, $paymentId, $orderId)
- {
- $this->pendingCaptureFactory->create()
- ->setCaptureId($details->getTransactionId())
- ->setPaymentId($paymentId)
- ->setOrderId($orderId)
- ->save();
- }
- /**
- * {@inheritdoc}
- */
- public function queuePendingAuthorization(AmazonAuthorizationDetails $details, OrderInterface $order)
- {
- $pendingAuthorization = $this->pendingAuthorizationFactory->create()
- ->setAuthorizationId($details->getAuthorizeTransactionId());
- if ($details->hasCapture()) {
- $pendingAuthorization->setCaptureId($details->getCaptureTransactionId())
- ->setCapture(true);
- }
- $order->addRelatedObject($pendingAuthorization);
- }
- /**
- * {@inheritdoc}
- */
- public function queuePendingRefund(AmazonRefundDetails $details, PaymentInfoInterface $payment)
- {
- $this->pendingRefundFactory->create()
- ->setRefundId($details->getRefundId())
- ->setPaymentId($payment->getId())
- ->setOrderId($payment->getOrder()->getId())
- ->save();
- }
- /**
- * {@inheritdoc}
- */
- public function closeTransaction($transactionId, PaymentInfoInterface $payment, OrderInterface $order)
- {
- $this->getTransaction($transactionId, $payment, $order)->setIsClosed(1)->save();
- }
- /**
- * {@inheritdoc}
- */
- public function getTransaction($transactionId, PaymentInfoInterface $payment, OrderInterface $order)
- {
- $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
- $searchCriteriaBuilder->addFilter(
- TransactionInterface::TXN_ID,
- $transactionId
- );
- $searchCriteriaBuilder->addFilter(
- TransactionInterface::ORDER_ID,
- $order->getId()
- );
- $searchCriteriaBuilder->addFilter(
- TransactionInterface::PAYMENT_ID,
- $payment->getId()
- );
- $searchCriteria = $searchCriteriaBuilder
- ->setPageSize(1)
- ->setCurrentPage(1)
- ->create();
- $transactionList = $this->transactionRepository->getList($searchCriteria);
- if (count($items = $transactionList->getItems())) {
- $transaction = current($items);
- $transaction
- ->setPayment($payment)
- ->setOrder($order);
- return $transaction;
- }
- throw new NoSuchEntityException();
- }
- }
|