Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reverse deposit check #228

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions PluginController/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ protected function doCreateDependentCredit(PaymentInterface $payment, $amount)
}

$paymentState = $payment->getState();
if (PaymentInterface::STATE_APPROVED !== $paymentState && PaymentInterface::STATE_EXPIRED !== $paymentState) {
if (!in_array($paymentState, [
PaymentInterface::STATE_APPROVED,
PaymentInterface::STATE_EXPIRED,
PaymentInterface::STATE_DEPOSITED,
])) {
throw new InvalidPaymentException('Payment\'s state must be APPROVED, or EXPIRED.');
}

Expand Down Expand Up @@ -470,10 +474,15 @@ protected function doCredit(CreditInterface $credit, $amount)
throw new \InvalidArgumentException(sprintf('$amount cannot be greater than %.2f (Credit restriction).', $credit->getTargetAmount()));
}

$payment = null;
if (false === $credit->isIndependent()) {
$payment = $credit->getPayment();
$paymentState = $payment->getState();
if (PaymentInterface::STATE_APPROVED !== $paymentState && PaymentInterface::STATE_EXPIRED !== $paymentState) {
if (!in_array($paymentState, [
PaymentInterface::STATE_APPROVED,
PaymentInterface::STATE_DEPOSITED,
PaymentInterface::STATE_EXPIRED,
])) {
throw new InvalidPaymentException('Payment\'s state must be APPROVED, or EXPIRED.');
}

Expand Down Expand Up @@ -956,8 +965,11 @@ protected function doReverseDeposit(PaymentInterface $payment, $amount)
throw new InvalidPaymentInstructionException('PaymentInstruction must be in STATE_VALID.');
}

if (PaymentInterface::STATE_APPROVED !== $payment->getState()) {
throw new InvalidPaymentException('Payment must be in STATE_APPROVED.');
if (!in_array($payment->getState(), [
PaymentInterface::STATE_APPROVED,
PaymentInterface::STATE_DEPOSITED,
])) {
throw new InvalidPaymentException('Payment must be in STATE_APPROVED or STATE_DEPOSITED.');
}

$transaction = $instruction->getPendingTransaction();
Expand All @@ -974,6 +986,7 @@ protected function doReverseDeposit(PaymentInterface $payment, $amount)
$transaction->setTransactionType(FinancialTransactionInterface::TRANSACTION_TYPE_REVERSE_DEPOSIT);
$transaction->setState(FinancialTransactionInterface::STATE_PENDING);
$transaction->setRequestedAmount($amount);
$payment->addTransaction($transaction);

$payment->setReversingDepositedAmount($amount);
$instruction->setReversingDepositedAmount($instruction->getReversingDepositedAmount() + $amount);
Expand Down
2 changes: 1 addition & 1 deletion PluginController/PluginControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public function reverseCredit($creditId, $amount);
*
* The implementation will ensure that:
* - PaymentInstruction is in STATE_VALID
* - Payment is in STATE_APPROVED
* - Payment is in STATE_APPROVED or STATE_DEPOSITED
* - any pending transaction is belonging to this payment, and is a reverseDeposit transaction
* - for non-retry transactions: requested amount <= PaymentInstruction.depositedAmount - PaymentInstruction.reversingDepositedAmount
* - for non-retry transactions: requested amount <= Payment.depositedAmount
Expand Down