Skip to content

Commit

Permalink
PIPRES-329: Use Mollie mapped status for initial subscription order (#…
Browse files Browse the repository at this point in the history
…827)

* PIPRES-329: Use Mollie mapped status for initial subscription order

* moved price compare before validateOrder call
  • Loading branch information
mandan2 authored and JevgenijVisockij committed Jan 16, 2024
1 parent 15e7085 commit 7ef0374
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Utility/NumberUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Utility/OrderStatusUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions subscription/Exception/CouldNotHandleRecurringOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
1 change: 1 addition & 0 deletions subscription/Exception/ExceptionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 16 additions & 9 deletions subscription/Handler/RecurringOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
Expand All @@ -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]);
Expand Down

0 comments on commit 7ef0374

Please sign in to comment.