/**
  * @dataProvider createDataProvider
  * @param string|null $transactionId
  * @param int $orderId
  * @param int $paymentId
  * @param bool $failSafe
  * @param string $type
  * @param bool $isPaymentTransactionClosed
  * @param array $additionalInfo
  * @param bool $document
  * @param bool $isTransactionExists
  */
 public function testCreate($transactionId, $orderId, $paymentId, $failSafe, $type, $isPaymentTransactionClosed, $additionalInfo, $document, $isTransactionExists)
 {
     $parentTransactionId = "12";
     $shouldCloseParentTransaction = true;
     $parentTransactionIsClosed = false;
     if ($document) {
         $document = $this->expectDocument($transactionId);
     }
     $parentTransaction = $this->expectTransaction($orderId, $paymentId);
     $transaction = $this->expectTransaction($orderId, $paymentId);
     $transaction->expects($this->atLeastOnce())->method('getTxnId')->willReturn($transactionId);
     $transaction->expects($this->once())->method('setPayment')->withAnyParameters()->willReturnSelf();
     $transaction->expects($this->once())->method('setOrder')->withAnyParameters()->willReturnSelf();
     if ($isTransactionExists) {
         $this->repositoryMock->method('getByTransactionId')->withConsecutive([$transactionId, $paymentId, $orderId], [$parentTransactionId, $paymentId, $orderId])->willReturnOnConsecutiveCalls($transaction, $parentTransaction);
     } else {
         $this->repositoryMock->method('getByTransactionId')->withConsecutive([$transactionId, $paymentId, $orderId], [$parentTransactionId, $paymentId, $orderId])->willReturnOnConsecutiveCalls(false, $parentTransaction);
         $this->repositoryMock->method('create')->willReturn($transaction);
         $transaction->expects($this->once())->method('setTxnId')->with($transactionId)->willReturn($transaction);
     }
     $this->expectSetPaymentObject($transaction, $type, $failSafe);
     $this->expectsIsPaymentTransactionClosed($isPaymentTransactionClosed, $transaction);
     $this->expectsIsPaymentTransactionClosed($isPaymentTransactionClosed, $transaction);
     $this->expectSetPaymentObject($transaction, $type, $failSafe);
     $this->expectsLinkWithParentTransaction($transaction, $parentTransactionId, $shouldCloseParentTransaction, $parentTransaction, $parentTransactionIsClosed);
     if ($additionalInfo) {
         $transaction->expects($this->exactly(count($additionalInfo)))->method('setAdditionalInformation');
     }
     $builder = $this->builder->setPayment($this->paymentMock)->setOrder($this->orderMock)->setAdditionalInformation($additionalInfo)->setFailSafe($failSafe)->setTransactionId($transactionId);
     if ($document) {
         $builder->setSalesDocument($document);
     }
     $this->assertSame($transaction, $builder->build($type));
 }
 /**
  * Revert refund
  *
  * @param \Magento\Framework\DataObject|InfoInterface|\Magento\Sales\Model\Order\Payment $payment
  * @param Transaction $transaction
  *
  * @return $this
  * @throws \Exception
  * @throws LocalizedException
  */
 public function refundReversal(\Magento\Payment\Model\InfoInterface $payment, Transaction $transaction)
 {
     $order = $payment->getOrder();
     $this->_logger->debug(__METHOD__);
     $addInfo = $transaction->getAdditionalInformation('raw_details_info');
     if (!isset($addInfo['orderNumber']) || !isset($addInfo['creditNumber'])) {
         throw new LocalizedException($this->_dataHelper->__('Unable to revert refund, creditNumber and/or orderNumber not found!'));
     }
     if (!$this->_dataHelper->isBackendAvailable()) {
         return $this;
     }
     $backendClient = $this->_dataHelper->getBackendClient();
     $ret = $backendClient->refundReversal($addInfo['orderNumber'], $addInfo['creditNumber']);
     if ($ret->hasFailed()) {
         $msg = implode(',', array_map(function ($e) {
             /** @var \WirecardCEE_QMore_Error $e */
             return $e->getConsumerMessage();
         }, $ret->getErrors()));
         $this->_logger->debug(__METHOD__ . ':' . $msg);
         throw new \Exception($msg);
     }
     $transactionId = $transaction->getTxnId() . '-reversal';
     $payment->setTransactionId($transactionId);
     /** @var \Magento\Sales\Model\Order\Payment\Transaction $transaction */
     $transaction = $this->_transactionBuilder->setPayment($payment)->setOrder($order)->setTransactionId($transactionId)->build(Transaction::TYPE_VOID);
     $transaction->setParentId($transaction->getTransactionId());
     $transaction->save();
     $payment->addTransactionCommentsToOrder($transaction, 'refund reversal: orderNumber:' . $addInfo['orderNumber'] . ' creditNumber:' . $addInfo['creditNumber']);
     $order->save();
     return $this;
 }
 /**
  * @param $type
  * @param $message
  * @param Order $order
  * @param \WirecardCEE_Stdlib_Return_ReturnAbstract $return
  *
  * @return \Magento\Sales\Api\Data\TransactionInterface|null
  */
 public function saveTransaction($type, $message, $order, $return)
 {
     $additionalInformation = array();
     foreach ($return->getReturned() as $fieldName => $fieldValue) {
         $additionalInformation[htmlentities($fieldName)] = htmlentities($fieldValue);
     }
     $payment = $order->getPayment();
     $tid = '';
     if ($return instanceof \WirecardCEE_Stdlib_Return_Success) {
         $tid = $return->getGatewayReferenceNumber();
     }
     /* generate dummy GwRef for pending payments */
     if (!strlen($tid)) {
         $tid = uniqid('tmp_');
     }
     $transaction = $this->_transactionBuilder->setPayment($payment)->setOrder($order)->setTransactionId($tid)->build($type);
     $transaction->setIsClosed(0);
     /* must be set as RAW_DETAILS, otherwise they are not displayed in the admin area */
     $transaction->setAdditionalInformation(Transaction::RAW_DETAILS, $additionalInformation);
     $payment->addTransactionCommentsToOrder($transaction, $message);
     return $transaction;
 }