protected function constructFinancialTransactionResult(FinancialTransactionInterface $transaction, $status, $reasonCode) { $this->financialTransaction = $transaction; $this->credit = $transaction->getCredit(); $this->payment = $transaction->getPayment(); $this->paymentInstruction = null !== $this->credit ? $this->credit->getPaymentInstruction() : $this->payment->getPaymentInstruction(); $this->status = $status; $this->reasonCode = $reasonCode; $this->recoverable = false; }
protected function createFinancialTransaction(PaymentInterface $payment) { if (!$payment instanceof Payment) { throw new Exception('This controller only supports Doctrine2 entities as Payment objects.'); } $class =& $this->options['financial_transaction_class']; $transaction = new $class(); $payment->addTransaction($transaction); return $transaction; }
protected function doReverseDeposit(PaymentInterface $payment, $amount) { $instruction = $payment->getPaymentInstruction(); if (PaymentInstructionInterface::STATE_VALID !== $instruction->getState()) { throw new InvalidPaymentInstructionException('PaymentInstruction must be in STATE_VALID.'); } if (PaymentInterface::STATE_APPROVED !== $payment->getState()) { throw new InvalidPaymentException('Payment must be in STATE_APPROVED.'); } $transaction = $instruction->getPendingTransaction(); if ($transaction === null) { if (1 === Number::compare($amount, $max = $instruction->getDepositedAmount() - $instruction->getReversingDepositedAmount())) { throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (PaymentInstruction restriction).', $max)); } if (1 === Number::compare($amount, $payment->getDepositedAmount())) { throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (Payment restriction).', $payment->getDepositedAmount())); } $transaction = $this->buildFinancialTransaction(); $transaction->setTransactionType(FinancialTransactionInterface::TRANSACTION_TYPE_REVERSE_DEPOSIT); $transaction->setState(FinancialTransactionInterface::STATE_PENDING); $transaction->setRequestedAmount($amount); $payment->setReversingDepositedAmount($amount); $instruction->setReversingDepositedAmount($instruction->getReversingDepositedAmount() + $amount); $retry = false; } else { if (FinancialTransactionInterface::TRANSACTION_TYPE_REVERSE_DEPOSIT !== $transaction->getTransactionType()) { throw new InvalidPaymentInstructionException('There is another pending transaction on this payment.'); } if ($payment->getId() !== $transaction->getPayment()->getId()) { throw new InvalidPaymentException('The pending transaction belongs to another Payment.'); } if (1 === Number::compare($amount, $instruction->getReversingDepositedAmount())) { throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (PaymentInstruction restriction).', $instruction->getReversingDepositedAmount())); } if (0 !== Number::compare($amount, $payment->getReversingDepositedAmount())) { throw new \InvalidArgumentException(sprintf('$amount must be equal to %.2f (Payment restriction).', $payment->getReversingDepositedAmount())); } $retry = true; } $plugin = $this->getPlugin($instruction->getPaymentSystemName()); try { $plugin->reverseDeposit($transaction, $retry); $processedAmount = $transaction->getProcessedAmount(); $payment->setReversingDepositedAmount(0.0); $instruction->setReversingDepositedAmount($instruction->getReversingDepositedAmount() - $amount); if (PluginInterface::RESPONSE_CODE_SUCCESS === $transaction->getResponseCode()) { $transaction->setState(FinancialTransactionInterface::STATE_SUCCESS); $payment->setDepositedAmount($payment->getDepositedAmount() - $processedAmount); $instruction->setDepositedAmount($instruction->getDepositedAmount() - $processedAmount); return $this->buildFinancialTransactionResult($transaction, Result::STATUS_SUCCESS, PluginInterface::REASON_CODE_SUCCESS); } else { $transaction->setState(FinancialTransactionInterface::STATE_FAILED); return $this->buildFinancialTransactionResult($transaction, Result::STATUS_FAILED, $transaction->getReasonCode()); } } catch (PluginFinancialException $ex) { $transaction->setState(FinancialTransactionInterface::STATE_FAILED); return $this->buildFinancialTransactionResult($transaction, Result::STATUS_FAILED, $transaction->getReasonCode()); } catch (PluginBlockedException $blocked) { $transaction->setState(FinancialTransactionInterface::STATE_PENDING); if ($blocked instanceof PluginTimeoutException) { $reasonCode = PluginInterface::REASON_CODE_TIMEOUT; } else { if ($blocked instanceof PluginActionRequiredException) { $reasonCode = PluginInterface::REASON_CODE_ACTION_REQUIRED; } else { if (null === ($reasonCode = $transaction->getReasonCode())) { $reasonCode = PluginInterface::REASON_CODE_BLOCKED; } } } $transaction->setReasonCode($reasonCode); $transaction->setResponseCode(PluginInterface::RESPONSE_CODE_PENDING); $result = $this->buildFinancialTransactionResult($transaction, Result::STATUS_PENDING, $reasonCode); $result->setPluginException($blocked); $result->setRecoverable(); return $result; } }
/** * Set state on a given Payment. * * @param PaymentInterface $payment Payment entity * @param integer $state New state */ private function setPaymentState($payment, $state) { $payment->setState($state); $this->em->persist($payment); $this->em->flush(); }