kstmostofa/dtone-php-api
Laravel package that wraps the DT One (Dtone) API, providing a simple interface to authenticate and interact with DT One services from your Laravel app. Includes a publishable config and helper resources (with GIFs in docs).
Installation:
composer require kstmostofa/dtone-php-api
php artisan vendor:publish --provider="Kstmostofa\DtonePhpApi\DtoneServiceProvider" --tag="config"
config/dtone.php and update API credentials (client_id, client_secret, redirect_uri).First Use Case:
use Kstmostofa\DtonePhpApi\Facades\Dtone;
$response = Dtone::otp()->send([
'to' => '+880123456789', // Recipient number
'message' => 'Your OTP is 123456',
]);
success status and otp_id for future reference.Where to Look First:
Kstmostofa\DtonePhpApi\Facades\Dtone (primary entry point).config/dtone.php (API keys, endpoints, and defaults).OTP Management:
Dtone::otp()->send($payload);
Dtone::otp()->verify($otpId, $otpCode);
Dtone::otp()->resend($otpId);
Voice Calls:
Dtone::voice()->call([
'to' => '+880123456789',
'from' => '+1234567890',
'url' => 'https://example.com/voice-callback',
]);
url for server-side callback handling (e.g., call status updates).GIFs (Unique Feature):
Dtone::gif()->send([
'to' => '+880123456789',
'gif_id' => '12345', // From Dtone's GIF library
]);
Error Handling:
try-catch:
try {
$response = Dtone::otp()->send($payload);
} catch (\Exception $e) {
Log::error("Dtone API Error: " . $e->getMessage());
return response()->json(['error' => 'Failed to send OTP'], 500);
}
Service Container Binding:
$this->app->bind('dtone', function () {
return new \Kstmostofa\DtonePhpApi\DtoneClient(config('dtone'));
});
Middleware for Auth:
public function handle($request, Closure $next) {
$response = $next($request);
if ($response->getData()->success === false) {
abort(500, 'Dtone API Error');
}
return $response;
}
Logging:
Dtone::setLogger(function ($message) {
Log::debug($message);
});
Testing:
$this->mock(Dtone::class)->shouldReceive('otp')->andReturnSelf()
->shouldReceive('send')->andReturn((object) ['success' => true]);
API Credentials:
config/dtone.php to version control. Use environment variables:
'client_id' => env('DTONE_CLIENT_ID'),
'client_secret' => env('DTONE_CLIENT_SECRET'),
Rate Limiting:
429 Too Many Requests gracefully:
if ($response->getStatusCode() === 429) {
sleep(2); // Retry after delay
}
OTP Expiry:
if (time() - $otpSentAt > 300) { // 5 minutes
return redirect()->route('resend.otp');
}
GIF Limitations:
Enable Verbose Logging:
debug flag in config/dtone.php:
'debug' => env('APP_DEBUG', false),
Common Errors:
Invalid Credentials: Verify client_id/client_secret in the Dtone portal.Invalid Number: Ensure numbers are in E.164 format (e.g., +880123456789).Missing Parameters: Validate payloads against Dtone’s API specs.Testing Locally:
'base_url' => env('DTONE_SANDBOX_URL', 'https://api.dtone.com'),
Custom Responses:
// app/Providers/AppServiceProvider.php
public function boot() {
Dtone::macro('sendWelcomeOTP', function ($phone) {
return $this->otp()->send([
'to' => $phone,
'message' => 'Welcome! Your OTP is ' . Str::random(6),
]);
});
}
Webhook Handling:
Route::post('/dtone/callback', function (Request $request) {
$payload = $request->json()->all();
// Process call status (e.g., answered, failed)
});
signature header.Batch Processing:
Dtone::otp()->send($payload)->onQueue('dtone-jobs');
Fallback Mechanisms:
try {
Dtone::otp()->send($payload);
} catch (\Exception $e) {
Mail::to($user->email)->send(new OTPFallbackMail($otpCode));
}
How can I help you explore Laravel packages today?