diff --git a/Entity/Payment.php b/Entity/Payment.php index cf3ce8f0..44e85508 100644 --- a/Entity/Payment.php +++ b/Entity/Payment.php @@ -296,4 +296,9 @@ public function getUpdatedAt() { return $this->updatedAt; } + + public function isRefundAllowed() + { + return $this->getState() === PaymentInterface::STATE_DEPOSITED; + } } diff --git a/Model/PaymentInterface.php b/Model/PaymentInterface.php index 58b2c851..36bf8328 100644 --- a/Model/PaymentInterface.php +++ b/Model/PaymentInterface.php @@ -64,4 +64,5 @@ function setReversingApprovedAmount($amount); function setReversingCreditedAmount($amount); function setReversingDepositedAmount($amount); function setState($state); + function isRefundAllowed(); } \ No newline at end of file diff --git a/PluginController/PluginController.php b/PluginController/PluginController.php index 8aae937f..c30fcf2c 100644 --- a/PluginController/PluginController.php +++ b/PluginController/PluginController.php @@ -3,6 +3,7 @@ namespace JMS\Payment\CoreBundle\PluginController; use JMS\Payment\CoreBundle\PluginController\Event\PaymentStateChangeEvent; +use JMS\Payment\CoreBundle\Entity\Payment; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -420,8 +421,8 @@ protected function doCreateDependentCredit(PaymentInterface $payment, $amount) } $paymentState = $payment->getState(); - if (PaymentInterface::STATE_APPROVED !== $paymentState && PaymentInterface::STATE_EXPIRED !== $paymentState) { - throw new InvalidPaymentException('Payment\'s state must be APPROVED, or EXPIRED.'); + if (!$payment->isRefundAllowed()) { + throw new InvalidPaymentException('Payment\'s state must be DEPOSITED.'); } $credit = $this->buildCredit($instruction, $amount); @@ -462,10 +463,11 @@ protected function doCredit(CreditInterface $credit, $amount) } if (false === $credit->isIndependent()) { + /** @var PaymentInterface $payment */ $payment = $credit->getPayment(); $paymentState = $payment->getState(); - if (PaymentInterface::STATE_APPROVED !== $paymentState && PaymentInterface::STATE_EXPIRED !== $paymentState) { - throw new InvalidPaymentException('Payment\'s state must be APPROVED, or EXPIRED.'); + if (!$payment->isRefundAllowed()) { + throw new InvalidPaymentException('Payment\'s state must be DEPOSITED.'); } if (1 === Number::compare($amount, $max = $payment->getDepositedAmount() - $payment->getReversingDepositedAmount() - $payment->getCreditingAmount() - $payment->getCreditedAmount())) { @@ -495,10 +497,10 @@ protected function doCredit(CreditInterface $credit, $amount) } if (false === $credit->isIndependent()) { + /** @var Payment $payment */ $payment = $credit->getPayment(); - $paymentState = $payment->getState(); - if (PaymentInterface::STATE_APPROVED !== $paymentState && PaymentInterface::STATE_EXPIRED !== $paymentState) { - throw new InvalidPaymentException('Payment\'s state must be APPROVED, or EXPIRED.'); + if (!$payment->isRefundAllowed()) { + throw new InvalidPaymentException('Payment\'s state must be DEPOSITED.'); } if (1 === Number::compare($amount, $payment->getCreditingAmount())) { @@ -516,6 +518,7 @@ protected function doCredit(CreditInterface $credit, $amount) $plugin = $this->getPlugin($instruction->getPaymentSystemName()); try { + $transaction->setPayment($credit->getPayment()); $plugin->credit($transaction, $retry); $processedAmount = $transaction->getProcessedAmount();