CompleteSaleHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Gateway\Response;
  17. use Magento\Payment\Gateway\Response\HandlerInterface;
  18. use Magento\Payment\Model\Method\Logger;
  19. use Amazon\Payment\Gateway\Helper\SubjectReader;
  20. use Amazon\Core\Helper\Data;
  21. use Amazon\Payment\Api\Data\PendingAuthorizationInterfaceFactory;
  22. class CompleteSaleHandler implements HandlerInterface
  23. {
  24. /**
  25. * @var Data
  26. */
  27. private $coreHelper;
  28. /**
  29. * @var Logger
  30. */
  31. private $logger;
  32. /**
  33. * @var SubjectReader
  34. */
  35. private $subjectReader;
  36. /**
  37. * @var PendingAuthorizationInterfaceFactory
  38. */
  39. private $pendingAuthorizationFactory;
  40. /**
  41. * CompleteAuthHandler constructor.
  42. *
  43. * @param Logger $logger
  44. * @param SubjectReader $subjectReader
  45. * @param PendingAuthorizationInterfaceFactory $pendingAuthorizationFactory
  46. * @param Data $coreHelper
  47. */
  48. public function __construct(
  49. Logger $logger,
  50. SubjectReader $subjectReader,
  51. PendingAuthorizationInterfaceFactory $pendingAuthorizationFactory,
  52. Data $coreHelper
  53. ) {
  54. $this->logger = $logger;
  55. $this->subjectReader = $subjectReader;
  56. $this->coreHelper = $coreHelper;
  57. $this->pendingAuthorizationFactory = $pendingAuthorizationFactory;
  58. }
  59. /**
  60. * @param array $handlingSubject
  61. * @param array $response
  62. * @throws \Exception
  63. */
  64. public function handle(array $handlingSubject, array $response)
  65. {
  66. $paymentDO = $this->subjectReader->readPayment($handlingSubject);
  67. $payment = $paymentDO->getPayment();
  68. if ($response['status']) {
  69. $payment->setTransactionId($response['capture_transaction_id']);
  70. $payment->setParentTransactionId($response['authorize_transaction_id']);
  71. if ($response['timeout']) {
  72. $order = $this->subjectReader->getOrder();
  73. $this->pendingAuthorizationFactory->create()
  74. ->setAuthorizationId($response['authorize_transaction_id'])
  75. ->setCaptureId($response['capture_transaction_id'])
  76. ->setCapture(true)
  77. ->save();
  78. $payment->setIsTransactionPending(true);
  79. $order->setState($order::STATE_PAYMENT_REVIEW)->setStatus($order::STATE_PAYMENT_REVIEW);
  80. $payment->setIsTransactionClosed(false);
  81. } else {
  82. $payment->setIsTransactionClosed(true);
  83. }
  84. if (!isset($handlingSubject['partial_capture']) || !$handlingSubject['partial_capture']) {
  85. $quoteLink = $this->subjectReader->getQuoteLink();
  86. $quoteLink->setConfirmed(true)->save();
  87. }
  88. }
  89. }
  90. }