Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PayOperation.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  private $eventManager;
17 
21  public function __construct(
22  \Magento\Framework\Model\Context $context
23  ) {
24  $this->eventManager = $context->getEventDispatcher();
25  }
26 
34  public function execute(
35  \Magento\Sales\Api\Data\OrderInterface $order,
37  $capture
38  ) {
39  $this->calculateOrderItemsTotals(
40  $invoice->getItems()
41  );
42 
43  if ($this->canCapture($order, $invoice)) {
44  if ($capture) {
45  $invoice->capture();
46  } else {
47  $invoice->setCanVoidFlag(false);
48 
49  $invoice->pay();
50  }
51  } elseif (!$order->getPayment()->getMethodInstance()->isGateway() || !$capture) {
52  if (!$order->getPayment()->getIsTransactionPending()) {
53  $invoice->setCanVoidFlag(false);
54 
55  $invoice->pay();
56  }
57  }
58 
59  $this->calculateOrderTotals($order, $invoice);
60 
61  if (null === $invoice->getState()) {
62  $invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_OPEN);
63  }
64 
65  $this->eventManager->dispatch(
66  'sales_order_invoice_register',
67  ['invoice' => $invoice, 'order' => $order]
68  );
69 
70  return $order;
71  }
72 
80  private function calculateOrderItemsTotals($items)
81  {
82  foreach ($items as $item) {
83  if ($item->isDeleted()) {
84  continue;
85  }
86 
87  if ($item->getQty() > 0) {
88  $item->register();
89  } else {
90  $item->isDeleted(true);
91  }
92  }
93  }
94 
103  private function canCapture(
104  \Magento\Sales\Api\Data\OrderInterface $order,
105  \Magento\Sales\Api\Data\InvoiceInterface $invoice
106  ) {
107  return $invoice->getState() != \Magento\Sales\Model\Order\Invoice::STATE_CANCELED &&
108  $invoice->getState() != \Magento\Sales\Model\Order\Invoice::STATE_PAID &&
109  $order->getPayment()->canCapture();
110  }
111 
120  private function calculateOrderTotals(
121  \Magento\Sales\Api\Data\OrderInterface $order,
122  \Magento\Sales\Api\Data\InvoiceInterface $invoice
123  ) {
124  $order->setTotalInvoiced(
125  $order->getTotalInvoiced() + $invoice->getGrandTotal()
126  );
127  $order->setBaseTotalInvoiced(
128  $order->getBaseTotalInvoiced() + $invoice->getBaseGrandTotal()
129  );
130 
131  $order->setSubtotalInvoiced(
132  $order->getSubtotalInvoiced() + $invoice->getSubtotal()
133  );
134  $order->setBaseSubtotalInvoiced(
135  $order->getBaseSubtotalInvoiced() + $invoice->getBaseSubtotal()
136  );
137 
138  $order->setTaxInvoiced(
139  $order->getTaxInvoiced() + $invoice->getTaxAmount()
140  );
141  $order->setBaseTaxInvoiced(
142  $order->getBaseTaxInvoiced() + $invoice->getBaseTaxAmount()
143  );
144 
145  $order->setDiscountTaxCompensationInvoiced(
146  $order->getDiscountTaxCompensationInvoiced() + $invoice->getDiscountTaxCompensationAmount()
147  );
148  $order->setBaseDiscountTaxCompensationInvoiced(
149  $order->getBaseDiscountTaxCompensationInvoiced() + $invoice->getBaseDiscountTaxCompensationAmount()
150  );
151 
152  $order->setShippingTaxInvoiced(
153  $order->getShippingTaxInvoiced() + $invoice->getShippingTaxAmount()
154  );
155  $order->setBaseShippingTaxInvoiced(
156  $order->getBaseShippingTaxInvoiced() + $invoice->getBaseShippingTaxAmount()
157  );
158 
159  $order->setShippingInvoiced(
160  $order->getShippingInvoiced() + $invoice->getShippingAmount()
161  );
162  $order->setBaseShippingInvoiced(
163  $order->getBaseShippingInvoiced() + $invoice->getBaseShippingAmount()
164  );
165 
166  $order->setDiscountInvoiced(
167  $order->getDiscountInvoiced() + $invoice->getDiscountAmount()
168  );
169  $order->setBaseDiscountInvoiced(
170  $order->getBaseDiscountInvoiced() + $invoice->getBaseDiscountAmount()
171  );
172 
173  $order->setBaseTotalInvoicedCost(
174  $order->getBaseTotalInvoicedCost() + $invoice->getBaseCost()
175  );
176  }
177 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$order
Definition: order.php:55
execute(\Magento\Sales\Api\Data\OrderInterface $order, \Magento\Sales\Api\Data\InvoiceInterface $invoice, $capture)
$invoice
__construct(\Magento\Framework\Model\Context $context)
$items