Initial Ladill Queue release — enterprise QMS standalone app.
Deploy Ladill Queue / deploy (push) Successful in 56s
Deploy Ladill Queue / deploy (push) Successful in 56s
Phases 1–6: tickets, counters, displays, appointments, workflows, rules, analytics, reports, feedback, admin, device API, and Gitea deploy workflow for queue.ladill.com. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Billing;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
/**
|
||||
* Client for the platform Billing HTTP API — the one UserWallet lives on the
|
||||
* platform; CRM only consumes it. Amounts are integer minor units; the user is
|
||||
* identified by public_id. Authenticates with the per-consumer config('billing.api_key').
|
||||
*/
|
||||
class BillingClient
|
||||
{
|
||||
private function base(): string
|
||||
{
|
||||
return rtrim((string) config('billing.api_url'), '/');
|
||||
}
|
||||
|
||||
private function token(): string
|
||||
{
|
||||
return (string) (config('billing.api_key') ?? '');
|
||||
}
|
||||
|
||||
public function balanceMinor(string $publicId): int
|
||||
{
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(8)
|
||||
->get($this->base().'/balance', ['user' => $publicId]);
|
||||
$res->throw();
|
||||
|
||||
return (int) ($res->json('balance_minor') ?? 0);
|
||||
}
|
||||
|
||||
public function canAfford(string $publicId, int $amountMinor): bool
|
||||
{
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(8)
|
||||
->get($this->base().'/can-afford', ['user' => $publicId, 'amount_minor' => $amountMinor]);
|
||||
$res->throw();
|
||||
|
||||
return (bool) ($res->json('affordable') ?? false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Debit the wallet. Returns true on success, false on insufficient balance
|
||||
* (HTTP 402). Idempotent by $reference.
|
||||
*/
|
||||
public function debit(string $publicId, int $amountMinor, string $source, string $reference, ?string $description = null): bool
|
||||
{
|
||||
$res = Http::withToken($this->token())->acceptJson()->timeout(10)->post($this->base().'/debit', array_filter([
|
||||
'user' => $publicId,
|
||||
'amount_minor' => $amountMinor,
|
||||
'service' => (string) config('billing.service', 'crm'),
|
||||
'source' => $source,
|
||||
'reference' => $reference,
|
||||
'description' => $description,
|
||||
], static fn ($v) => $v !== null));
|
||||
|
||||
if ($res->status() === 402) {
|
||||
return false;
|
||||
}
|
||||
$res->throw();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Billing;
|
||||
|
||||
use App\Models\CrmPurchase;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class OneTimePurchaseService
|
||||
{
|
||||
public function __construct(private readonly BillingClient $billing)
|
||||
{
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|null */
|
||||
public function product(string $productKey): ?array
|
||||
{
|
||||
$product = config('crm_products.'.$productKey);
|
||||
|
||||
return is_array($product) ? $product : null;
|
||||
}
|
||||
|
||||
public function hasPurchased(string $ownerRef, string $productKey): bool
|
||||
{
|
||||
return CrmPurchase::has($ownerRef, $productKey);
|
||||
}
|
||||
|
||||
public function priceMinor(string $productKey): int
|
||||
{
|
||||
return (int) ($this->product($productKey)['price_minor'] ?? 0);
|
||||
}
|
||||
|
||||
public function purchase(string $ownerRef, string $productKey): bool
|
||||
{
|
||||
if ($this->hasPurchased($ownerRef, $productKey)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$product = $this->product($productKey);
|
||||
if (! $product) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$costMinor = (int) ($product['price_minor'] ?? 0);
|
||||
|
||||
if ($costMinor > 0) {
|
||||
try {
|
||||
if (! $this->billing->canAfford($ownerRef, $costMinor)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$charged = $this->billing->debit(
|
||||
$ownerRef,
|
||||
$costMinor,
|
||||
'purchase',
|
||||
'crm-purchase-'.$productKey.'-'.$ownerRef,
|
||||
'CRM: '.($product['name'] ?? $productKey),
|
||||
);
|
||||
|
||||
if (! $charged) {
|
||||
return false;
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
CrmPurchase::create([
|
||||
'owner_ref' => $ownerRef,
|
||||
'product_key' => $productKey,
|
||||
'cost_minor' => $costMinor,
|
||||
'purchased_at' => now(),
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user