Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jul 17, 2024
1 parent 9353c9c commit 617df4b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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
Expand Down
21 changes: 13 additions & 8 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -831,7 +831,7 @@ protected function reimbursableAmount()

// Guard against a negative value
if ($reimbursableAmount->isNegative()) {
return $zeroAmount;
return $this->zero();
}

return $reimbursableAmount;
Expand Down Expand Up @@ -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));
}
}
9 changes: 4 additions & 5 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -592,7 +591,7 @@ public function canQueryRecurringSubscriptions()
}

/** @test */
public function halfWayThroughSubscriptionReturnsPositiveReimburesmentAmount()
public function halfwayThroughSubscriptionReturnsPositiveReimbursementAmount()
{
$this->withConfiguredPlans();

Expand All @@ -611,7 +610,7 @@ public function halfWayThroughSubscriptionReturnsPositiveReimburesmentAmount()

$this->assertEquals(
money('-50', 'EUR'),
$subscriptionHalfWayThrough->getReimburseAmountForUnusedTime()
$subscriptionHalfWayThrough->getReimbursableAmountForUnusedTime()
);
}

Expand All @@ -634,8 +633,8 @@ public function nonReimbursableSubscriptionReturnsNoReimbursementAmount()
]);

$this->assertEquals(
null,
$nonReimbursable->getReimburseAmountForUnusedTime()
money(0, 'EUR'),
$nonReimbursable->getReimbursableAmountForUnusedTime()
);
}
}

0 comments on commit 617df4b

Please sign in to comment.