Skip to content

Commit

Permalink
Refactor OTPIQ API integration to improve error handling and update c…
Browse files Browse the repository at this point in the history
…onfiguration
  • Loading branch information
codenashwan committed Jan 31, 2025
1 parent 17cd18d commit da80977
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
35 changes: 22 additions & 13 deletions app/Http/Controllers/Welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Illuminate\Support\Facades\Cache;
use Livewire\Component;
use Rstacode\Otpiq\DTOs\SmsData;
use Rstacode\Otpiq\Facades\Otpiq;
use WireUi\Traits\WireUiActions;

Expand All @@ -19,12 +18,18 @@ public function setApiKey()
$this->validate([
'apiKey' => 'required|string|min:20',
]);

if (strpos($this->apiKey, 'sk_live_') !== 0) {
$this->addError('apiKey', 'API Key is invalid!');
return;
}
Cache::put('otpiq_api_key', $this->apiKey, now()->addHours(24));
$this->reset('apiKey');
$this->dispatch('notify', [
'message' => 'API Key set successfully!',
'type' => 'success',
]);

}

public function removeApiKey()
Expand Down Expand Up @@ -59,12 +64,13 @@ public function save()
{
$this->callApiKey();
try {
$response = Otpiq::sendSms(new SmsData(
phoneNumber: $this->phoneNumber,
smsType: 'verification',
provider: $this->provider,
verificationCode: $this->verificationCode
));
$response = Otpiq::sendSms([
'phoneNumber' => $this->phoneNumber,
'smsType' => 'verification',
'verificationCode' => $this->verificationCode,
'provider' => $this->provider,
]);

return dd($response);
} catch (\Exception $exception) {
return dd($exception->getMessage());
Expand All @@ -74,12 +80,15 @@ public function save2()
{
$this->callApiKey();
try {
$response = Otpiq::sendSms(new SmsData(
phoneNumber: $this->phoneNumber,
smsType: 'custom',
customMessage: $this->customMessage,
senderId: $this->senderId,
));
$response = Otpiq::sendSms([
'phoneNumber' => $this->phoneNumber,
'smsType' => 'custom',
'customMessage' => $this->customMessage,
'senderId' => $this->senderId,
'provider' => 'sms', // Required for custom messages
]);

return dd($response);
} catch (\Exception $exception) {
return dd($exception->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"laravel/framework": "^11.31",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.5",
"rstacode/otpiq": "^1.0",
"rstacode/otpiq": "^1.2",
"wireui/wireui": "^2.2"
},
"require-dev": {
Expand Down
31 changes: 17 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions config/otpiq.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| OTPIQ API Configuration
|--------------------------------------------------------------------------
|
| Here you can specify your OTPIQ API credentials and other configuration
| options for the OTPIQ SMS service integration.
|
*/

'api_key' => env('OTPIQ_API_KEY'),
'api_key' => env('OTPIQ_API_KEY', ''),
'base_url' => env('OTPIQ_BASE_URL', 'https://api.otpiq.com/api/'),
];

0 comments on commit da80977

Please sign in to comment.