diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 49330929b0..691e84ffd9 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -429,10 +429,6 @@ class BaseDriver extends AbstractPaymentDriver return; } - // $confirmed_fee_count = collect($invoice->line_items) - // ->where('type_id', '4') - // ->count(); - $unconfirmed_fee_count = collect($invoice->line_items) ->where('type_id', '3') ->count(); diff --git a/app/PaymentDrivers/MolliePaymentDriver.php b/app/PaymentDrivers/MolliePaymentDriver.php index 7ef0db39f1..204970dc92 100644 --- a/app/PaymentDrivers/MolliePaymentDriver.php +++ b/app/PaymentDrivers/MolliePaymentDriver.php @@ -357,7 +357,14 @@ class MolliePaymentDriver extends BaseDriver if ($record) { if (in_array($payment->status, ['canceled', 'expired', 'failed'])) { - $record->service()->deletePayment(false); //sometimes mollie does not return but we still decrement the paid to date, this is incorrect. + + if(property_exists($payment->metadata, 'hash') && $payment->metadata->hash){ + $payment_hash = PaymentHash::where('hash', $payment->metadata->hash)->first(); + $this->handlePendingGatewayFeeRemoval($payment_hash); + } + + $record->service()->deletePayment(false); + } $record->status_id = $codes[$payment->status]; @@ -380,6 +387,28 @@ class MolliePaymentDriver extends BaseDriver } } + private function handlePendingGatewayFeeRemoval(PaymentHash $payment_hash) + { + $invoice = $payment_hash->fee_invoice; + + if($invoice){ + $line_items = $invoice->line_items; + + $line_items = collect($line_items)->filter(function($line_item, $key) use ($line_items) { + $last_type_4_key = collect($line_items)->filter(function($item) { + return $item->type_id == '4'; + })->keys()->last(); + + return $key != $last_type_4_key; + })->toArray(); + + $invoice->line_items = array_values($line_items); + + $invoice = $invoice->calc()->getInvoice(); + + } + } + public function process3dsConfirmation(Mollie3dsRequest $request) { $this->init(); diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index 4612659ca2..982def5b46 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -141,9 +141,9 @@ class DeletePayment ->updateBalanceAndPaidToDate($net_deletable, ($net_deletable * -1) > 0 ? 0 : ($net_deletable * -1)) // if negative, set to 0, the paid to date will be reduced further down. ->save(); - if ($paymentable_invoice->balance == $paymentable_invoice->amount) { + if (abs(floatval($paymentable_invoice->balance) - floatval($paymentable_invoice->amount)) < 0.005) { $paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save(); - } elseif ($paymentable_invoice->balance == 0) { + } elseif (floatval($paymentable_invoice->balance) == 0) { $paymentable_invoice->service()->setStatus(Invoice::STATUS_PAID)->save(); } else { $paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();