Manager.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Transaction;
  7. use Magento\Sales\Api\Data\OrderPaymentInterface;
  8. use Magento\Sales\Model\Order\Payment;
  9. use Magento\Sales\Model\Order\Payment\Transaction;
  10. class Manager implements ManagerInterface
  11. {
  12. /**
  13. * @var \Magento\Sales\Api\TransactionRepositoryInterface
  14. */
  15. protected $transactionRepository;
  16. /**
  17. * @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
  18. */
  19. public function __construct(\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository)
  20. {
  21. $this->transactionRepository = $transactionRepository;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function getAuthorizationTransaction($parentTransactionId, $paymentId, $orderId)
  27. {
  28. $transaction = false;
  29. if ($parentTransactionId) {
  30. $transaction = $this->transactionRepository->getByTransactionId(
  31. $parentTransactionId,
  32. $paymentId,
  33. $orderId
  34. );
  35. }
  36. return $transaction ?: $this->transactionRepository->getByTransactionType(
  37. Transaction::TYPE_AUTH,
  38. $paymentId,
  39. $orderId
  40. );
  41. }
  42. /**
  43. * Checks if transaction exists by txt id
  44. *
  45. * @param string $transactionId
  46. * @param int $paymentId
  47. * @param int $orderId
  48. * @return bool
  49. */
  50. public function isTransactionExists($transactionId, $paymentId, $orderId)
  51. {
  52. return $transactionId && $this->transactionRepository->getByTransactionId($transactionId, $paymentId, $orderId);
  53. }
  54. /**
  55. * Update transaction ids for further processing
  56. * If no transactions were set before invoking, may generate an "offline" transaction id
  57. *
  58. * @param OrderPaymentInterface $payment
  59. * @param string $type
  60. * @param bool|Transaction $transactionBasedOn
  61. * @return string|null
  62. */
  63. public function generateTransactionId(OrderPaymentInterface $payment, $type, $transactionBasedOn = false)
  64. {
  65. if (!$payment->getParentTransactionId() && !$payment->getTransactionId() && $transactionBasedOn) {
  66. $payment->setParentTransactionId($transactionBasedOn->getTxnId());
  67. }
  68. // generate transaction id for an offline action or payment method that didn't set it
  69. if (($parentTxnId = $payment->getParentTransactionId()) && !$payment->getTransactionId()) {
  70. return "{$parentTxnId}-{$type}";
  71. }
  72. return $payment->getTransactionId();
  73. }
  74. }