From 7ef03740b5a0a1f735e3feae5ac1af3487bd7d5c Mon Sep 17 00:00:00 2001 From: mandan2 <61560082+mandan2@users.noreply.github.com> Date: Mon, 16 Oct 2023 14:55:16 +0300 Subject: [PATCH] PIPRES-329: Use Mollie mapped status for initial subscription order (#827) * PIPRES-329: Use Mollie mapped status for initial subscription order * moved price compare before validateOrder call --- src/Utility/NumberUtility.php | 2 +- src/Utility/OrderStatusUtility.php | 8 +++--- .../CouldNotHandleRecurringOrder.php | 8 ++++++ subscription/Exception/ExceptionCode.php | 1 + .../Handler/RecurringOrderHandler.php | 25 ++++++++++++------- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Utility/NumberUtility.php b/src/Utility/NumberUtility.php index f34ea296a..f7c4f5977 100644 --- a/src/Utility/NumberUtility.php +++ b/src/Utility/NumberUtility.php @@ -114,7 +114,7 @@ public static function divide( return (float) $result->toPrecision($precision, $roundingMode); } - public static function isEqual($a, $b) + public static function isEqual(float $a, float $b): bool { $firstNumber = self::getNumber($a); $secondNumber = self::getNumber($b); diff --git a/src/Utility/OrderStatusUtility.php b/src/Utility/OrderStatusUtility.php index 5d07977f0..2ac6fd999 100644 --- a/src/Utility/OrderStatusUtility.php +++ b/src/Utility/OrderStatusUtility.php @@ -56,10 +56,10 @@ public static function transformPaymentStatusToRefunded($transaction) $remainingAmount = $payment->getAmountRemaining(); } } - $amountRefunded = $transaction->amountRefunded->value; - $amountPayed = $transaction->amountCaptured->value; - $isPartiallyRefunded = NumberUtility::isLowerThan($amountRefunded, $amountPayed); - $isFullyRefunded = NumberUtility::isEqual($amountRefunded, $amountPayed); + $amountRefunded = (float) $transaction->amountRefunded->value; + $amountPaid = (float) $transaction->amountCaptured->value; + $isPartiallyRefunded = NumberUtility::isLowerThan($amountRefunded, $amountPaid); + $isFullyRefunded = NumberUtility::isEqual($amountRefunded, $amountPaid); if ($isPartiallyRefunded) { if ($isVoucher && NumberUtility::isEqual(0, $remainingAmount)) { diff --git a/subscription/Exception/CouldNotHandleRecurringOrder.php b/subscription/Exception/CouldNotHandleRecurringOrder.php index 88c00de19..a8aa3f9d5 100644 --- a/subscription/Exception/CouldNotHandleRecurringOrder.php +++ b/subscription/Exception/CouldNotHandleRecurringOrder.php @@ -19,4 +19,12 @@ public static function failedToApplySelectedCarrier(): self ExceptionCode::RECURRING_ORDER_FAILED_TO_APPLY_SELECTED_CARRIER ); } + + public static function cartAndPaidPriceAreNotEqual(): self + { + return new self( + 'Cart and paid price are not equal', + ExceptionCode::RECURRING_ORDER_CART_AND_PAID_PRICE_ARE_NOT_EQUAL + ); + } } diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index 69a0b5e2e..1d2710b49 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -26,4 +26,5 @@ class ExceptionCode public const RECURRING_ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 3001; public const RECURRING_ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 3002; + public const RECURRING_ORDER_CART_AND_PAID_PRICE_ARE_NOT_EQUAL = 3003; } diff --git a/subscription/Handler/RecurringOrderHandler.php b/subscription/Handler/RecurringOrderHandler.php index 56833468a..96cea54d3 100644 --- a/subscription/Handler/RecurringOrderHandler.php +++ b/subscription/Handler/RecurringOrderHandler.php @@ -30,6 +30,7 @@ use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface; use Mollie\Subscription\Repository\RecurringOrdersProductRepositoryInterface; use Mollie\Subscription\Utility\ClockInterface; +use Mollie\Utility\NumberUtility; use Mollie\Utility\SecureKeyUtility; use MolRecurringOrder; use MolRecurringOrdersProduct; @@ -245,11 +246,24 @@ private function createSubscription(Payment $transaction, MolRecurringOrder $rec $methodName = $paymentMethod->method_name ?: Config::$methods[$transaction->method]; + $subscriptionPaidTotal = (float) $subscription->amount->value; + $cartTotal = (float) $newCart->getOrderTotal(true, Cart::BOTH); + + if (!NumberUtility::isEqual($cartTotal, $subscriptionPaidTotal)) { + // TODO when improved logging with context will be implemented, remove this logging + $this->logger->error('Paid price is not equal to the order\'s total', [ + 'Paid price' => $subscriptionPaidTotal, + 'Order price' => $cartTotal, + ]); + + throw CouldNotHandleRecurringOrder::cartAndPaidPriceAreNotEqual(); + } + try { $this->mollie->validateOrder( (int) $newCart->id, - (int) $this->configuration->get(Config::MOLLIE_STATUS_AWAITING), - (float) $subscription->amount->value, + (int) Config::getStatuses()[$transaction->status], + $subscriptionPaidTotal, sprintf('subscription/%s', $methodName), null, ['transaction_id' => $transaction->id], @@ -268,13 +282,6 @@ private function createSubscription(Payment $transaction, MolRecurringOrder $rec $orderId = (int) Order::getIdByCartId((int) $newCart->id); $order = new Order($orderId); - if ((float) $order->total_paid_tax_incl !== (float) $subscription->amount->value) { - $this->logger->error('Paid price is not equal to the order\'s total', [ - 'Paid price' => (float) $subscription->amount->value, - 'Order price' => (float) $order->total_paid_tax_incl, - ]); - } - $this->mollieOrderCreationService->createMolliePayment($transaction, (int) $newCart->id, $order->reference, (int) $orderId, PaymentStatus::STATUS_PAID); $this->orderStatusService->setOrderStatus($orderId, (int) Config::getStatuses()[$transaction->status]);