diff --git a/src/Helpers/helpers.php b/src/Helpers/helpers.php index a833c222..17e8d140 100644 --- a/src/Helpers/helpers.php +++ b/src/Helpers/helpers.php @@ -6,7 +6,7 @@ use Money\Money; use Money\Parser\DecimalMoneyParser; -if (! function_exists('object_to_array_recursive')) { +if (!function_exists('object_to_array_recursive')) { /** * Recursively cast an object into an array. * @@ -23,7 +23,7 @@ function object_to_array_recursive($object) } } -if (! function_exists('money')) { +if (!function_exists('money')) { /** * Create a Money object from a Mollie Amount array. * @@ -37,7 +37,7 @@ function money($value, string $currency) } } -if (! function_exists('decimal_to_money')) { +if (!function_exists('decimal_to_money')) { /** * Create a Money object from a decimal string / currency pair. * @@ -53,7 +53,7 @@ function decimal_to_money(string $value, string $currency) } } -if (! function_exists('mollie_array_to_money')) { +if (!function_exists('mollie_array_to_money')) { /** * Create a Money object from a Mollie Amount array. * @@ -66,7 +66,7 @@ function mollie_array_to_money(array $array) } } -if (! function_exists('money_to_mollie_array')) { +if (!function_exists('money_to_mollie_array')) { /** * Create a Mollie Amount array from a Money object. * @@ -84,7 +84,7 @@ function money_to_mollie_array(Money $money) } } -if (! function_exists('mollie_object_to_money')) { +if (!function_exists('mollie_object_to_money')) { /** * Create a Money object from a Mollie Amount object. * @@ -97,7 +97,7 @@ function mollie_object_to_money(object $object) } } -if (! function_exists('money_to_decimal')) { +if (!function_exists('money_to_decimal')) { /** * Format the money as basic decimal diff --git a/src/Subscription.php b/src/Subscription.php index 5397b43a..cb279b73 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -613,20 +613,22 @@ public function getCurrencyAttribute() } /** - * Gets the amount to be refunded for the subscription's unused time. + * Gets the amount to be reimbursed for the subscription's unused time. + * + * Result range value: (-X up to 0) * * @param \Carbon\Carbon|null $now * @return \Money\Money */ - public function getReimburseAmountForUnusedTime(?Carbon $now = null): ?Money + public function getReimbursableAmountForUnusedTime(?Carbon $now = null): Money { $now = $now ?: now(); if ($this->onTrial()) { - return null; + return $this->zero(); } if (round($this->getCycleLeftAttribute($now), 5) == 0) { - return null; + return $this->zero(); } return $this->reimbursableAmount() @@ -792,12 +794,10 @@ protected function reimburse(Money $amount, array $overrides = []) */ protected function reimbursableAmount() { - $zeroAmount = new Money('0.00', new Currency($this->currency)); - // Determine base amount eligible to reimburse $latestProcessedOrderItem = $this->latestProcessedOrderItem(); if (!$latestProcessedOrderItem) { - return $zeroAmount; + return $this->zero(); } $reimbursableAmount = $latestProcessedOrderItem->getTotal() @@ -831,7 +831,7 @@ protected function reimbursableAmount() // Guard against a negative value if ($reimbursableAmount->isNegative()) { - return $zeroAmount; + return $this->zero(); } return $reimbursableAmount; @@ -971,4 +971,9 @@ public function latestProcessedOrderItem() { return $this->orderItems()->processed()->orderByDesc('process_at')->first(); } + + private function zero(): Money + { + return new Money('0.00', new Currency($this->currency)); + } } diff --git a/tests/SubscriptionTest.php b/tests/SubscriptionTest.php index c1691c25..add5c51b 100644 --- a/tests/SubscriptionTest.php +++ b/tests/SubscriptionTest.php @@ -8,7 +8,6 @@ use Laravel\Cashier\Cashier; use Laravel\Cashier\Events\SubscriptionResumed; use Laravel\Cashier\Subscription; -use Laravel\Cashier\Tests\Database\Factories\OrderFactory; use Laravel\Cashier\Tests\Database\Factories\OrderItemFactory; use Laravel\Cashier\Tests\Database\Factories\SubscriptionFactory; use Laravel\Cashier\Tests\Fixtures\User; @@ -592,7 +591,7 @@ public function canQueryRecurringSubscriptions() } /** @test */ - public function halfWayThroughSubscriptionReturnsPositiveReimburesmentAmount() + public function halfwayThroughSubscriptionReturnsPositiveReimbursementAmount() { $this->withConfiguredPlans(); @@ -611,7 +610,7 @@ public function halfWayThroughSubscriptionReturnsPositiveReimburesmentAmount() $this->assertEquals( money('-50', 'EUR'), - $subscriptionHalfWayThrough->getReimburseAmountForUnusedTime() + $subscriptionHalfWayThrough->getReimbursableAmountForUnusedTime() ); } @@ -634,8 +633,8 @@ public function nonReimbursableSubscriptionReturnsNoReimbursementAmount() ]); $this->assertEquals( - null, - $nonReimbursable->getReimburseAmountForUnusedTime() + money(0, 'EUR'), + $nonReimbursable->getReimbursableAmountForUnusedTime() ); } }