Builder.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\OrderInterface;
  8. use Magento\Sales\Api\Data\OrderPaymentInterface;
  9. use Magento\Sales\Api\Data\TransactionInterface;
  10. use Magento\Sales\Api\TransactionRepositoryInterface;
  11. use Magento\Sales\Model\AbstractModel;
  12. use Magento\Sales\Model\Order\Payment;
  13. /**
  14. * Class Builder build transaction
  15. */
  16. class Builder implements BuilderInterface
  17. {
  18. /**
  19. * @var OrderPaymentInterface
  20. */
  21. protected $payment;
  22. /**
  23. * @var OrderInterface
  24. */
  25. protected $order;
  26. /**
  27. * @var AbstractModel
  28. */
  29. protected $document;
  30. /**
  31. * @var bool
  32. */
  33. protected $failSafe = false;
  34. /**
  35. * @var string
  36. */
  37. protected $message;
  38. /**
  39. * @var string
  40. */
  41. protected $transactionId;
  42. /**
  43. * @var array
  44. */
  45. protected $transactionAdditionalInfo = [];
  46. /**
  47. * @var TransactionRepositoryInterface
  48. */
  49. protected $transactionRepository;
  50. /**
  51. * @param TransactionRepositoryInterface $transactionRepository
  52. */
  53. public function __construct(TransactionRepositoryInterface $transactionRepository)
  54. {
  55. $this->transactionRepository = $transactionRepository;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function setPayment(OrderPaymentInterface $payment)
  61. {
  62. $this->payment = $payment;
  63. return $this;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setOrder(OrderInterface $order)
  69. {
  70. $this->order = $order;
  71. return $this;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function setSalesDocument(\Magento\Sales\Model\AbstractModel $document)
  77. {
  78. $this->document = $document;
  79. return $this;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function setFailSafe($failSafe)
  85. {
  86. $this->failSafe = $failSafe;
  87. return $this;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function setMessage($message)
  93. {
  94. $this->message = $message;
  95. return $this;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function setTransactionId($transactionId)
  101. {
  102. $this->transactionId = $transactionId;
  103. return $this;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function setAdditionalInformation(array $value)
  109. {
  110. $this->transactionAdditionalInfo = $value;
  111. return $this;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function addAdditionalInformation($key, $value)
  117. {
  118. $this->transactionAdditionalInfo[$key] = $value;
  119. return $this;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function reset()
  125. {
  126. unset($this->payment);
  127. unset($this->document);
  128. unset($this->order);
  129. unset($this->message);
  130. unset($this->transactionId);
  131. $this->failSafe = false;
  132. $this->transactionAdditionalInfo = [];
  133. return $this;
  134. }
  135. /**
  136. * Checks if payment was set
  137. *
  138. * @return bool
  139. */
  140. protected function isPaymentExists()
  141. {
  142. if ($this->payment) {
  143. if ($this->payment->getSkipTransactionCreation()) {
  144. $this->payment->unsTransactionId();
  145. return false;
  146. }
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function build($type)
  155. {
  156. if ($this->isPaymentExists() && $this->transactionId !== null) {
  157. $transaction = $this->transactionRepository->getByTransactionId(
  158. $this->transactionId,
  159. $this->payment->getId(),
  160. $this->order->getId()
  161. );
  162. if (!$transaction) {
  163. $transaction = $this->transactionRepository->create()->setTxnId($this->transactionId);
  164. }
  165. $transaction->setPaymentId($this->payment->getId())
  166. ->setPayment($this->payment)
  167. ->setOrderId($this->order->getId())
  168. ->setOrder($this->order)
  169. ->setTxnType($type)
  170. ->isFailsafe($this->failSafe);
  171. if ($this->payment->hasIsTransactionClosed()) {
  172. $transaction->setIsClosed((int)$this->payment->getIsTransactionClosed());
  173. }
  174. if ($this->transactionAdditionalInfo) {
  175. foreach ($this->transactionAdditionalInfo as $key => $value) {
  176. $transaction->setAdditionalInformation($key, $value);
  177. }
  178. }
  179. $this->transactionAdditionalInfo = [];
  180. $this->payment->setLastTransId($transaction->getTxnId());
  181. $this->payment->setCreatedTransaction($transaction);
  182. $this->order->addRelatedObject($transaction);
  183. if ($this->document && $this->document instanceof AbstractModel) {
  184. $this->document->setTransactionId($transaction->getTxnId());
  185. }
  186. return $this->linkWithParentTransaction($transaction);
  187. }
  188. return null;
  189. }
  190. /**
  191. * Links transaction with parent transaction
  192. *
  193. * @param TransactionInterface $transaction
  194. * @return TransactionInterface
  195. */
  196. protected function linkWithParentTransaction(TransactionInterface $transaction)
  197. {
  198. $parentTransactionId = $this->payment->getParentTransactionId();
  199. if ($parentTransactionId) {
  200. $transaction->setParentTxnId($parentTransactionId);
  201. if ($this->payment->getShouldCloseParentTransaction()) {
  202. $parentTransaction = $this->transactionRepository->getByTransactionId(
  203. $parentTransactionId,
  204. $this->payment->getid(),
  205. $this->order->getId()
  206. );
  207. if ($parentTransaction) {
  208. if (!$parentTransaction->getIsClosed()) {
  209. $parentTransaction->isFailsafe($this->failSafe)->close(false);
  210. }
  211. $this->order->addRelatedObject($parentTransaction);
  212. }
  213. }
  214. }
  215. return $transaction;
  216. }
  217. }