Route SMS voice uploads through Transfer with wallet billing.
Deploy Ladill Transfer / deploy (push) Successful in 28s

Add a service API for the Ladill SMS platform to store voice recordings in Transfer and charge monthly storage from the customer wallet.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-06-20 02:23:18 +00:00
co-authored by Cursor
parent eb9023340f
commit ccffa1433d
8 changed files with 281 additions and 0 deletions
@@ -0,0 +1,81 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\Identity\PublicIdUserResolver;
use App\Services\Transfer\TransferService;
use App\Support\DirectTransferFileUrl;
use App\Support\TransferWalletErrors;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use RuntimeException;
/** Voice SMS audio uploads from the Ladill SMS platform (storage billed via Transfer). */
class SmsVoiceTransferController extends Controller
{
public function __construct(
private PublicIdUserResolver $users,
private TransferService $transfers,
) {}
public function store(Request $request): JsonResponse
{
if ($request->attributes->get('service_caller') !== 'sms') {
return response()->json(['message' => 'Forbidden.'], 403);
}
$maxKb = max(256, (int) config('transfer.sms_voice_max_upload_kb', 5120));
$data = $request->validate([
'user' => ['required', 'string', 'max:64'],
'title' => ['nullable', 'string', 'max:120'],
'voice' => ['required', 'file', 'mimes:mp3,wav', 'max:'.$maxKb],
]);
try {
$user = $this->users->resolve((string) $data['user']);
} catch (RuntimeException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
$voice = $request->file('voice');
$filename = $voice?->getClientOriginalName() ?: 'voice.mp3';
$title = trim((string) ($data['title'] ?? ''));
if ($title === '') {
$title = Str::limit('SMS voice: '.$filename, 120, '');
}
try {
$transfer = $this->transfers->create($user, [
'title' => $title,
], [$voice]);
} catch (RuntimeException $e) {
$payload = ['message' => $e->getMessage()];
if (TransferWalletErrors::isInsufficientBalance($e)) {
$payload['error_code'] = TransferWalletErrors::CODE_INSUFFICIENT_WALLET;
}
return response()->json($payload, 422);
}
$transfer->load(['files', 'qrCode']);
$file = $transfer->files->first();
if ($file === null) {
return response()->json(['message' => 'Voice upload failed.'], 422);
}
return response()->json([
'data' => [
'direct_file_url' => DirectTransferFileUrl::for($transfer, $file),
'public_url' => $transfer->qrCode?->publicUrl(),
'transfer_id' => $transfer->id,
'file_id' => $file->id,
'paid_until' => $transfer->paid_until?->toIso8601String(),
'storage_gb' => $transfer->storageGb(),
'monthly_cost_ghs' => $transfer->monthlyCostGhs(),
],
], 201);
}
}
@@ -0,0 +1,57 @@
<?php
namespace App\Services\Identity;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use RuntimeException;
/** Resolves a Ladill Transfer user mirror from a platform account public_id. */
class PublicIdUserResolver
{
public function resolve(string $publicId): User
{
$publicId = trim($publicId);
if ($publicId === '') {
throw new RuntimeException('User public_id is required.');
}
$existing = User::query()->where('public_id', $publicId)->first();
if ($existing) {
return $existing;
}
$base = rtrim((string) config('identity.api_url'), '/');
$key = (string) config('identity.api_key');
if ($base === '' || $key === '') {
throw new RuntimeException('Identity API is not configured.');
}
$res = Http::withToken($key)
->acceptJson()
->timeout(10)
->get("{$base}/identity/profile", ['user' => $publicId]);
if ($res->failed()) {
throw new RuntimeException('Could not resolve the Ladill account.');
}
$data = $res->json('data');
if (! is_array($data)) {
throw new RuntimeException('Could not resolve the Ladill account.');
}
$email = trim((string) ($data['email'] ?? ''));
if ($email === '') {
$email = 'sms+'.$publicId.'@accounts.ladill.local';
}
return User::updateOrCreate(
['public_id' => $publicId],
[
'name' => trim((string) ($data['name'] ?? '')) ?: 'Ladill user',
'email' => $email,
],
);
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace App\Support;
use App\Models\QrCode;
use App\Models\Transfer;
use App\Models\TransferFile;
use RuntimeException;
class DirectTransferFileUrl
{
public static function for(Transfer $transfer, TransferFile $file): string
{
$shortCode = $transfer->qrCode?->short_code;
if ($shortCode === null || $shortCode === '') {
throw new RuntimeException('Transfer file URL is unavailable.');
}
return QrCode::publicBaseUrl().'/q/'.$shortCode.'/transfer/files/'.$file->id;
}
}