Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
8 
15 
19 class Builder implements BuilderInterface
20 {
24  protected $payment;
25 
29  protected $order;
30 
34  protected $document;
35 
39  protected $failSafe = false;
40 
44  protected $message;
45 
49  protected $transactionId;
50 
55 
60 
65  {
66  $this->transactionRepository = $transactionRepository;
67  }
68 
73  {
74  $this->payment = $payment;
75  return $this;
76  }
77 
81  public function setOrder(OrderInterface $order)
82  {
83  $this->order = $order;
84  return $this;
85  }
86 
90  public function setSalesDocument(\Magento\Sales\Model\AbstractModel $document)
91  {
92  $this->document = $document;
93  return $this;
94  }
95 
99  public function setFailSafe($failSafe)
100  {
101  $this->failSafe = $failSafe;
102  return $this;
103  }
104 
108  public function setMessage($message)
109  {
110  $this->message = $message;
111  return $this;
112  }
113 
118  {
119  $this->transactionId = $transactionId;
120  return $this;
121  }
122 
126  public function setAdditionalInformation(array $value)
127  {
128  $this->transactionAdditionalInfo = $value;
129  return $this;
130  }
131 
135  public function addAdditionalInformation($key, $value)
136  {
137  $this->transactionAdditionalInfo[$key] = $value;
138  return $this;
139  }
140 
144  public function reset()
145  {
146  unset($this->payment);
147  unset($this->document);
148  unset($this->order);
149  unset($this->message);
150  unset($this->transactionId);
151  $this->failSafe = false;
152  $this->transactionAdditionalInfo = [];
153  return $this;
154  }
155 
161  protected function isPaymentExists()
162  {
163  if ($this->payment) {
164  if ($this->payment->getSkipTransactionCreation()) {
165  $this->payment->unsTransactionId();
166  return false;
167  }
168  return true;
169  }
170  return false;
171  }
172 
176  public function build($type)
177  {
178  if ($this->isPaymentExists() && $this->transactionId !== null) {
179  $transaction = $this->transactionRepository->getByTransactionId(
180  $this->transactionId,
181  $this->payment->getId(),
182  $this->order->getId()
183  );
184  if (!$transaction) {
185  $transaction = $this->transactionRepository->create()->setTxnId($this->transactionId);
186  }
187  $transaction->setPaymentId($this->payment->getId())
188  ->setPayment($this->payment)
189  ->setOrderId($this->order->getId())
190  ->setOrder($this->order)
191  ->setTxnType($type)
192  ->isFailsafe($this->failSafe);
193 
194  if ($this->payment->hasIsTransactionClosed()) {
195  $transaction->setIsClosed((int)$this->payment->getIsTransactionClosed());
196  }
197  if ($this->transactionAdditionalInfo) {
198  foreach ($this->transactionAdditionalInfo as $key => $value) {
199  $transaction->setAdditionalInformation($key, $value);
200  }
201  }
202  $this->transactionAdditionalInfo = [];
203 
204  $this->payment->setLastTransId($transaction->getTxnId());
205  $this->payment->setCreatedTransaction($transaction);
206  $this->order->addRelatedObject($transaction);
207  if ($this->document && $this->document instanceof AbstractModel) {
208  $this->document->setTransactionId($transaction->getTxnId());
209  }
210 
212  }
213  return null;
214  }
215 
223  {
224  $parentTransactionId = $this->payment->getParentTransactionId();
225 
226  if ($parentTransactionId) {
227  $transaction->setParentTxnId($parentTransactionId);
228  if ($this->payment->getShouldCloseParentTransaction()) {
229  $parentTransaction = $this->transactionRepository->getByTransactionId(
230  $parentTransactionId,
231  $this->payment->getid(),
232  $this->order->getId()
233  );
234  if ($parentTransaction) {
235  if (!$parentTransaction->getIsClosed()) {
236  $parentTransaction->isFailsafe($this->failSafe)->close(false);
237  }
238  $this->order->addRelatedObject($parentTransaction);
239  }
240  }
241  }
242  return $transaction;
243  }
244 }
$transaction
setPayment(OrderPaymentInterface $payment)
Definition: Builder.php:72
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
linkWithParentTransaction(TransactionInterface $transaction)
Definition: Builder.php:222
__construct(TransactionRepositoryInterface $transactionRepository)
Definition: Builder.php:64
setSalesDocument(\Magento\Sales\Model\AbstractModel $document)
Definition: Builder.php:90