Deploy Ladill Frontdesk / deploy (push) Successful in 44s
Host alerts use email/phone on the host record without requiring a Ladill account link; SMS and over-quota emails debit the org wallet at platform rates. Co-authored-by: Cursor <cursoragent@cursor.com>
111 lines
3.0 KiB
PHP
111 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Frontdesk;
|
|
|
|
use App\Models\Organization;
|
|
use App\Services\Billing\BillingClient;
|
|
use Illuminate\Support\Str;
|
|
|
|
class NotificationBillingService
|
|
{
|
|
public function __construct(
|
|
protected BillingClient $billing,
|
|
protected NotificationPricingService $pricing,
|
|
protected NotificationUsageService $usage,
|
|
protected PlanService $plans,
|
|
) {}
|
|
|
|
public function emailCostMinor(Organization $organization): int
|
|
{
|
|
$allowance = $this->plans->freeEmailsPerMonth($organization);
|
|
if ($allowance === null || $this->usage->emailCountThisMonth($organization) < $allowance) {
|
|
return 0;
|
|
}
|
|
|
|
return $this->pricing->emailCostMinor();
|
|
}
|
|
|
|
public function canAffordEmail(Organization $organization): bool
|
|
{
|
|
$cost = $this->emailCostMinor($organization);
|
|
if ($cost <= 0) {
|
|
return true;
|
|
}
|
|
|
|
return $this->canAfford($organization->owner_ref, $cost);
|
|
}
|
|
|
|
public function canAffordSms(Organization $organization, string $message): bool
|
|
{
|
|
$cost = $this->pricing->smsCostMinor($message);
|
|
if ($cost <= 0) {
|
|
return true;
|
|
}
|
|
|
|
return $this->canAfford($organization->owner_ref, $cost);
|
|
}
|
|
|
|
/**
|
|
* Debit after a successful send. Returns false if the wallet could not be charged.
|
|
*/
|
|
public function chargeEmail(Organization $organization, string $reference, string $description): bool
|
|
{
|
|
$cost = $this->emailCostMinor($organization);
|
|
if ($cost <= 0) {
|
|
$this->usage->recordEmail($organization, 0);
|
|
|
|
return true;
|
|
}
|
|
|
|
if (! $this->debit($organization->owner_ref, $cost, 'email', $reference, $description)) {
|
|
return false;
|
|
}
|
|
|
|
$this->usage->recordEmail($organization, $cost);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function chargeSms(Organization $organization, string $message, string $reference, string $description): bool
|
|
{
|
|
$cost = $this->pricing->smsCostMinor($message);
|
|
if ($cost <= 0) {
|
|
$this->usage->recordSms($organization, 0);
|
|
|
|
return true;
|
|
}
|
|
|
|
if (! $this->debit($organization->owner_ref, $cost, 'sms', $reference, $description)) {
|
|
return false;
|
|
}
|
|
|
|
$this->usage->recordSms($organization, $cost);
|
|
|
|
return true;
|
|
}
|
|
|
|
private function canAfford(string $ownerRef, int $costMinor): bool
|
|
{
|
|
try {
|
|
return $this->billing->canAfford($ownerRef, $costMinor);
|
|
} catch (\Throwable) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private function debit(string $ownerRef, int $costMinor, string $channel, string $reference, string $description): bool
|
|
{
|
|
try {
|
|
return $this->billing->debit(
|
|
$ownerRef,
|
|
$costMinor,
|
|
'notification_'.$channel,
|
|
'frontdesk-notify-'.$reference.'-'.Str::uuid(),
|
|
$description,
|
|
);
|
|
} catch (\Throwable) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|