diff --git a/src/Subscription.php b/src/Subscription.php index 116e68c..8715c08 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -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. + * + * @param \Carbon\Carbon|null $now + * @return \Money\Money + */ + public function getReimburseAmountForUnusedTime(?Carbon $now = null): ?Money + { + $now = $now ?: now(); + + if ($this->onTrial()) { + return null; + } + if (round($this->getCycleLeftAttribute($now), 5) == 0) { + return null; + } + + return $this->reimbursableAmount() + ->negative() + ->multiply(sprintf('%.8F', $this->getCycleLeftAttribute($now))); + } + /** * Handle a failed payment. *