TransactionIdHandler.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Response;
  7. use Magento\Braintree\Gateway\SubjectReader;
  8. use Magento\Payment\Gateway\Response\HandlerInterface;
  9. use Magento\Sales\Model\Order\Payment;
  10. class TransactionIdHandler implements HandlerInterface
  11. {
  12. /**
  13. * @var SubjectReader
  14. */
  15. private $subjectReader;
  16. /**
  17. * TransactionIdHandler constructor.
  18. * @param SubjectReader $subjectReader
  19. */
  20. public function __construct(
  21. SubjectReader $subjectReader
  22. ) {
  23. $this->subjectReader = $subjectReader;
  24. }
  25. /**
  26. * Handles response
  27. *
  28. * @param array $handlingSubject
  29. * @param array $response
  30. * @return void
  31. */
  32. public function handle(array $handlingSubject, array $response)
  33. {
  34. $paymentDO = $this->subjectReader->readPayment($handlingSubject);
  35. if ($paymentDO->getPayment() instanceof Payment) {
  36. /** @var \Braintree\Transaction $transaction */
  37. $transaction = $this->subjectReader->readTransaction($response);
  38. /** @var Payment $orderPayment */
  39. $orderPayment = $paymentDO->getPayment();
  40. $this->setTransactionId(
  41. $orderPayment,
  42. $transaction
  43. );
  44. $orderPayment->setIsTransactionClosed($this->shouldCloseTransaction());
  45. $closed = $this->shouldCloseParentTransaction($orderPayment);
  46. $orderPayment->setShouldCloseParentTransaction($closed);
  47. }
  48. }
  49. /**
  50. * @param Payment $orderPayment
  51. * @param \Braintree\Transaction $transaction
  52. * @return void
  53. */
  54. protected function setTransactionId(Payment $orderPayment, \Braintree\Transaction $transaction)
  55. {
  56. $orderPayment->setTransactionId($transaction->id);
  57. }
  58. /**
  59. * Whether transaction should be closed
  60. *
  61. * @return bool
  62. */
  63. protected function shouldCloseTransaction()
  64. {
  65. return false;
  66. }
  67. /**
  68. * Whether parent transaction should be closed
  69. *
  70. * @param Payment $orderPayment
  71. * @return bool
  72. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  73. */
  74. protected function shouldCloseParentTransaction(Payment $orderPayment)
  75. {
  76. return false;
  77. }
  78. }