AbstractOperation.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Payment\Operations;
  7. use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
  8. use Magento\Sales\Api\Data\OrderInterface;
  9. use Magento\Sales\Api\Data\OrderPaymentInterface;
  10. use Magento\Sales\Model\Order\Invoice;
  11. use Magento\Sales\Model\Order\Payment\State\CommandInterface;
  12. use Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface;
  13. use Magento\Sales\Model\Order\Payment\Transaction\ManagerInterface;
  14. /**
  15. * Class AbstractOperation
  16. */
  17. abstract class AbstractOperation
  18. {
  19. /**
  20. * @var CommandInterface
  21. */
  22. protected $stateCommand;
  23. /**
  24. * @var BuilderInterface
  25. */
  26. protected $transactionBuilder;
  27. /**
  28. * @var ManagerInterface
  29. */
  30. protected $transactionManager;
  31. /**
  32. * @var EventManagerInterface
  33. */
  34. protected $eventManager;
  35. /**
  36. * @param CommandInterface $stateCommand
  37. * @param BuilderInterface $transactionBuilder
  38. * @param ManagerInterface $transactionManager
  39. * @param EventManagerInterface $eventManager
  40. */
  41. public function __construct(
  42. CommandInterface $stateCommand,
  43. BuilderInterface $transactionBuilder,
  44. ManagerInterface $transactionManager,
  45. EventManagerInterface $eventManager
  46. ) {
  47. $this->stateCommand = $stateCommand;
  48. $this->transactionBuilder = $transactionBuilder;
  49. $this->transactionManager = $transactionManager;
  50. $this->eventManager = $eventManager;
  51. }
  52. /**
  53. * Create new invoice with maximum qty for invoice for each item
  54. * register this invoice and capture
  55. *
  56. * @param OrderPaymentInterface $payment
  57. * @return Invoice
  58. */
  59. protected function invoice(OrderPaymentInterface $payment)
  60. {
  61. /** @var Invoice $invoice */
  62. $invoice = $payment->getOrder()->prepareInvoice();
  63. $invoice->register();
  64. if ($payment->getMethodInstance()->canCapture()) {
  65. $invoice->capture();
  66. }
  67. $payment->getOrder()->addRelatedObject($invoice);
  68. return $invoice;
  69. }
  70. /**
  71. * Totals updater utility method
  72. * Updates self totals by keys in data array('key' => $delta)
  73. *
  74. * @param OrderPaymentInterface $payment
  75. * @param array $data
  76. * @return void
  77. */
  78. protected function updateTotals(OrderPaymentInterface $payment, $data)
  79. {
  80. foreach ($data as $key => $amount) {
  81. if (null !== $amount) {
  82. $was = $payment->getDataUsingMethod($key);
  83. $payment->setDataUsingMethod($key, $was + $amount);
  84. }
  85. }
  86. }
  87. /**
  88. * Return invoice model for transaction
  89. *
  90. * @param OrderInterface $order
  91. * @param string $transactionId
  92. * @return false|Invoice
  93. */
  94. protected function getInvoiceForTransactionId(OrderInterface $order, $transactionId)
  95. {
  96. foreach ($order->getInvoiceCollection() as $invoice) {
  97. if ($invoice->getTransactionId() == $transactionId) {
  98. $invoice->load($invoice->getId());
  99. // to make sure all data will properly load (maybe not required)
  100. return $invoice;
  101. }
  102. }
  103. foreach ($order->getInvoiceCollection() as $invoice) {
  104. if ($invoice->getState() == \Magento\Sales\Model\Order\Invoice::STATE_OPEN
  105. && $invoice->load($invoice->getId())
  106. ) {
  107. $invoice->setTransactionId($transactionId);
  108. return $invoice;
  109. }
  110. }
  111. return false;
  112. }
  113. }