Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignaciocunado feat/get reimbursable amount for unused time #256

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,28 @@ public function getCurrencyAttribute()
return optional($this->plan())->amount()->getCurrency()->getCode();
}

/**
* Gets the amount to be refunded for the subscription's unused time.
Naoray marked this conversation as resolved.
Show resolved Hide resolved
*
* @param \Carbon\Carbon|null $now
* @return \Money\Money
*/
public function getReimburseAmountForUnusedTime(?Carbon $now = null): ?Money
Naoray marked this conversation as resolved.
Show resolved Hide resolved
{
$now = $now ?: now();

if ($this->onTrial()) {
return null;
Naoray marked this conversation as resolved.
Show resolved Hide resolved
}
if (round($this->getCycleLeftAttribute($now), 5) == 0) {
return null;
}

return $this->reimbursableAmount()
->negative()
->multiply(sprintf('%.8F', $this->getCycleLeftAttribute($now)));
}

/**
* Handle a failed payment.
*
Expand Down Expand Up @@ -639,7 +661,7 @@ public static function handlePaymentPaid(OrderItem $item)

if ($subscription->ends_at !== null) {
DB::transaction(function () use ($item, $subscription) {
if (! $subscription->scheduled_order_item_id) {
if (!$subscription->scheduled_order_item_id) {
$item = $subscription->scheduleNewOrderItemAt($subscription->ends_at);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Database/Factories/OrderItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ public function USD()
'currency' => 'USD',
]);
}

public function withOrder(): self
{
return $this->state(fn () => [
'order_id' => OrderFactory::new(),
]);
}
}
49 changes: 49 additions & 0 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -589,4 +590,52 @@ public function canQueryRecurringSubscriptions()
$this->assertEquals(1, Subscription::whereRecurring()->count());
$this->assertEquals(2, Subscription::whereNotRecurring()->count());
}

/** @test */
public function halfWayThroughSubscriptionReturnsPositiveReimburesmentAmount()
Naoray marked this conversation as resolved.
Show resolved Hide resolved
{
$this->withConfiguredPlans();

$subscriptionHalfWayThrough = SubscriptionFactory::new()
->has(
OrderItemFactory::new(['unit_price' => 100])
->processed()
->withOrder()
->EUR(),
'scheduledOrderItem'
)
->create([
'cycle_started_at' => now()->subDays(20),
'cycle_ends_at' => now()->addDays(20),
]);

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

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

$nonReimbursable = SubscriptionFactory::new()
->has(
OrderItemFactory::new(['unit_price' => 100])
->processed()
->withOrder()
->EUR(),
'scheduledOrderItem'
)
->create([
'cycle_started_at' => now()->subMonth(),
'cycle_ends_at' => now()->subDay(),
]);

$this->assertEquals(
null,
$nonReimbursable->getReimburseAmountForUnusedTime()
);
}
}
Loading