コード例 #1
0
 protected function doReverseApproval(PaymentInterface $payment, $amount)
 {
     $instruction = $payment->getPaymentInstruction();
     if (PaymentInstructionInterface::STATE_VALID !== $instruction->getState()) {
         throw new InvaliPaymentInstructionException('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 (null === $transaction) {
         if (1 === Number::compare($amount, $max = $instruction->getApprovedAmount() - $instruction->getReversingApprovedAmount())) {
             throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (PaymentInstruction restriction).', $max));
         }
         if (1 === Number::compare($amount, $payment->getApprovedAmount())) {
             throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (Payment restriction).', $payment->getApprovedAmount()));
         }
         $transaction = $this->buildFinancialTransaction();
         $transaction->setTransactionType(FinancialTransactionInterface::TRANSACTION_TYPE_REVERSE_APPROVAL);
         $transaction->setRequestedAmount($amount);
         $payment->addTransaction($transaction);
         $payment->setReversingApprovedAmount($amount);
         $instruction->setReversingApprovedAmount($instruction->getReversingApprovedAmount() + $amount);
         $retry = false;
     } else {
         if (FinancialTransactionInterface::TRANSACTION_TYPE_REVERSE_APPROVAL !== $transaction->getState()) {
             throw new InvalidPaymentInstructionException('PaymentInstruction has another pending transaction.');
         }
         if ($payment->getId() !== $transaction->getPayment()->getId()) {
             throw new \RuntimeException('Pending transaction belongs to another Payment.');
         }
         if (1 === Number::compare($amount, $instruction->getReversingApprovedAmount())) {
             throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (PaymentInstruction restriction).', $instruction->getReversingApprovedAmount()));
         }
         if (0 !== Number::compare($amount, $payment->getReversingApprovedAmount())) {
             throw new \InvalidArgumentException(sprintf('$amount must be equal to %.2f (Payment restriction).', $payment->getReversingApprovedAmount()));
         }
         $retry = true;
     }
     $plugin = $this->getPlugin($instruction->getPaymentSystemName());
     try {
         $plugin->reverseApproval($transaction, $retry);
         $processedAmount = $transaction->getProcessedAmount();
         if (PluginInterface::RESPONSE_CODE_SUCCESS === $transaction->getResponseCode()) {
             $transaction->setState(FinancialTransactionInterface::STATE_SUCCESS);
             $payment->setReversingApprovedAmount(0.0);
             $instruction->setReversingApprovedAmount($instruction->getReversingApprovedAmount() - $amount);
             $payment->setApprovedAmount($payment->getApprovedAmount() - $processedAmount);
             $instruction->setApprovedAmount($instruction->getApprovedAmount() - $processedAmount);
             return $this->buildFinancialTransactionResult($transaction, Result::STATUS_SUCCESS, PluginInterface::REASON_CODE_SUCCESS);
         } else {
             $transaction->setState(FinancialTransactionInterface::STATE_FAILED);
             $payment->setReversingApprovedAmount(0.0);
             $instruction->setReversingApprovedAmount($instruction->getReversingApprovedAmount() - $amount);
             return $this->buildFinancialTransactionResult($transaction, Result::STATUS_FAILED, $transaction->getReasonCode());
         }
     } catch (PluginfinancialException $ex) {
         $transaction->setState(FinancialTransactionInterface::STATE_FAILED);
         $payment->setReversingApprovedAmount(0.0);
         $instruction->setReversingApprovedAmount($instruction->getReversingApprovedAmount() - $amount);
         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;
     }
 }