RegisterCaptureNotificationOperation.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Sales\Api\Data\OrderPaymentInterface;
  8. use Magento\Sales\Model\Order\Invoice;
  9. use Magento\Sales\Model\Order\Payment;
  10. use Magento\Sales\Model\Order\Payment\Transaction;
  11. class RegisterCaptureNotificationOperation extends AbstractOperation
  12. {
  13. /**
  14. * Registers capture notification.
  15. *
  16. * @param OrderPaymentInterface $payment
  17. * @param string|float $amount
  18. * @param bool|int $skipFraudDetection
  19. * @return OrderPaymentInterface
  20. */
  21. public function registerCaptureNotification(OrderPaymentInterface $payment, $amount, $skipFraudDetection = false)
  22. {
  23. /**
  24. * @var $payment Payment
  25. */
  26. $payment->setTransactionId(
  27. $this->transactionManager->generateTransactionId(
  28. $payment,
  29. Transaction::TYPE_CAPTURE,
  30. $payment->getAuthorizationTransaction()
  31. )
  32. );
  33. $order = $payment->getOrder();
  34. $amount = (double)$amount;
  35. $invoice = $this->getInvoiceForTransactionId($order, $payment->getTransactionId());
  36. // register new capture
  37. if (!$invoice) {
  38. if ($payment->isSameCurrency() && $payment->isCaptureFinal($amount)) {
  39. $invoice = $order->prepareInvoice()->register();
  40. $invoice->setOrder($order);
  41. $order->addRelatedObject($invoice);
  42. $payment->setCreatedInvoice($invoice);
  43. $payment->setShouldCloseParentTransaction(true);
  44. } else {
  45. $payment->setIsFraudDetected(!$skipFraudDetection);
  46. $this->updateTotals($payment, ['base_amount_paid_online' => $amount]);
  47. }
  48. }
  49. if (!$payment->getIsTransactionPending()) {
  50. if ($invoice && Invoice::STATE_OPEN == $invoice->getState()) {
  51. $invoice->setOrder($order);
  52. $invoice->pay();
  53. $this->updateTotals($payment, ['base_amount_paid_online' => $amount]);
  54. $order->addRelatedObject($invoice);
  55. }
  56. }
  57. $message = $this->stateCommand->execute($payment, $amount, $order);
  58. $transaction = $payment->addTransaction(
  59. Transaction::TYPE_CAPTURE,
  60. $invoice,
  61. true
  62. );
  63. $message = $payment->prependMessage($message);
  64. $payment->addTransactionCommentsToOrder($transaction, $message);
  65. return $payment;
  66. }
  67. }