Add POS Pro subscription billing and free-tier limits.
Deploy Ladill POS / deploy (push) Successful in 43s
Deploy Ladill POS / deploy (push) Successful in 43s
Wallet-backed Pro unlocks unlimited products, restaurant mode, catalog imports, and ecosystem sync features. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pos;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class BillingClient
|
||||
{
|
||||
public function configured(): bool
|
||||
{
|
||||
return (string) config('billing.api_url', '') !== '' && (string) config('billing.api_key', '') !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{ok: bool, insufficient: bool, balance_minor: ?int, error: ?string}
|
||||
*/
|
||||
public function charge(User $user, int $amountMinor, string $reference, string $description): array
|
||||
{
|
||||
if (! $this->configured()) {
|
||||
return ['ok' => false, 'insufficient' => false, 'balance_minor' => null, 'error' => 'Billing is not configured.'];
|
||||
}
|
||||
|
||||
try {
|
||||
$res = Http::withToken((string) config('billing.api_key'))
|
||||
->acceptJson()->timeout(20)
|
||||
->post(rtrim((string) config('billing.api_url'), '/').'/debit', [
|
||||
'user' => $user->public_id,
|
||||
'amount_minor' => $amountMinor,
|
||||
'service' => 'pos',
|
||||
'source' => 'subscription',
|
||||
'reference' => $reference,
|
||||
'description' => $description,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('POS BillingClient: charge failed', ['user' => $user->public_id, 'error' => $e->getMessage()]);
|
||||
|
||||
return ['ok' => false, 'insufficient' => false, 'balance_minor' => null, 'error' => 'Could not reach the billing service.'];
|
||||
}
|
||||
|
||||
if ($res->status() === 402) {
|
||||
return ['ok' => false, 'insufficient' => true, 'balance_minor' => (int) $res->json('balance_minor', 0), 'error' => 'Insufficient wallet balance.'];
|
||||
}
|
||||
|
||||
if ($res->failed()) {
|
||||
return ['ok' => false, 'insufficient' => false, 'balance_minor' => null, 'error' => 'Billing error ('.$res->status().').'];
|
||||
}
|
||||
|
||||
return ['ok' => true, 'insufficient' => false, 'balance_minor' => null, 'error' => null];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user