Deploy: 2026-05-23 01:57:22
This commit is contained in:
@@ -13,7 +13,60 @@ use App\Models\CompanySubscriptionUsage;
|
|||||||
class OTPController extends BaseController
|
class OTPController extends BaseController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Send OTP verification code via WhatsApp (Text or Voice Note)
|
* Generate an image with the OTP code and return its public URL.
|
||||||
|
*/
|
||||||
|
private function generateOtpImage(string $code): string
|
||||||
|
{
|
||||||
|
$dir = __DIR__ . '/../../public/otp';
|
||||||
|
if (!is_dir($dir)) {
|
||||||
|
mkdir($dir, 0755, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = 'otp_' . time() . '_' . rand(1000, 9999) . '.png';
|
||||||
|
$filepath = $dir . '/' . $filename;
|
||||||
|
|
||||||
|
if (function_exists('imagecreate')) {
|
||||||
|
// Base image
|
||||||
|
$img = imagecreatetruecolor(400, 200);
|
||||||
|
$bg = imagecolorallocate($img, 240, 248, 255); // Alice blue background
|
||||||
|
$textColor = imagecolorallocate($img, 15, 23, 42); // Dark text
|
||||||
|
$lineColor = imagecolorallocate($img, 203, 213, 225); // Subtle lines
|
||||||
|
|
||||||
|
imagefill($img, 0, 0, $bg);
|
||||||
|
|
||||||
|
// Add noise lines to make it look like a CAPTCHA and prevent automated scraping
|
||||||
|
for ($i = 0; $i < 15; $i++) {
|
||||||
|
imageline($img, rand(0, 400), rand(0, 200), rand(0, 400), rand(0, 200), $lineColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a temporary small image for the text (since built-in font 5 is small)
|
||||||
|
$tmp = imagecreatetruecolor(100, 40);
|
||||||
|
$tmpBg = imagecolorallocate($tmp, 240, 248, 255);
|
||||||
|
$tmpText = imagecolorallocate($tmp, 15, 23, 42);
|
||||||
|
imagefill($tmp, 0, 0, $tmpBg);
|
||||||
|
|
||||||
|
$spacedCode = implode(' ', str_split($code));
|
||||||
|
imagestring($tmp, 5, 10, 12, $spacedCode, $tmpText);
|
||||||
|
|
||||||
|
// Scale up the text image to the main image
|
||||||
|
imagecopyresized($img, $tmp, 0, 0, 0, 0, 400, 200, 100, 40);
|
||||||
|
|
||||||
|
imagepng($img, $filepath);
|
||||||
|
imagedestroy($img);
|
||||||
|
imagedestroy($tmp);
|
||||||
|
} else {
|
||||||
|
// Fallback to placehold.co if PHP GD extension is missing
|
||||||
|
$url = "https://placehold.co/400x200/e0f2fe/0f172a/png?text=" . $code . "&font=oswald";
|
||||||
|
$content = file_get_contents($url);
|
||||||
|
file_put_contents($filepath, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
$appUrl = rtrim(getenv('APP_URL') ?: 'https://nabeh.intaleqapp.com', '/');
|
||||||
|
return $appUrl . '/otp/' . $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send OTP verification code via WhatsApp (Text, Voice Note, or Image)
|
||||||
* POST /api/otp/send
|
* POST /api/otp/send
|
||||||
*/
|
*/
|
||||||
public function send(Request $request, Response $response): void
|
public function send(Request $request, Response $response): void
|
||||||
@@ -22,7 +75,7 @@ class OTPController extends BaseController
|
|||||||
$body = $request->getBody();
|
$body = $request->getBody();
|
||||||
|
|
||||||
$phone = $body['phone'] ?? '';
|
$phone = $body['phone'] ?? '';
|
||||||
$type = $body['type'] ?? 'text'; // 'text' or 'voice'
|
$type = $body['type'] ?? 'text'; // 'text', 'voice', or 'image'
|
||||||
$sessionId = $body['session_id'] ?? null;
|
$sessionId = $body['session_id'] ?? null;
|
||||||
$customCode = $body['code'] ?? null;
|
$customCode = $body['code'] ?? null;
|
||||||
|
|
||||||
@@ -142,6 +195,11 @@ class OTPController extends BaseController
|
|||||||
|
|
||||||
// Send voice note
|
// Send voice note
|
||||||
ConversationFlowEngine::sendReply($session, $phone, '', null, $audioBase64, $mimeType);
|
ConversationFlowEngine::sendReply($session, $phone, '', null, $audioBase64, $mimeType);
|
||||||
|
} else if ($type === 'image') {
|
||||||
|
// Generate OTP Image
|
||||||
|
$imageUrl = $this->generateOtpImage($code);
|
||||||
|
$captionMsg = "مرحباً! هذا هو رمز التحقق الخاص بك لمتجر نابه. الرجاء عدم مشاركته.";
|
||||||
|
ConversationFlowEngine::sendReply($session, $phone, $captionMsg, $imageUrl);
|
||||||
} else {
|
} else {
|
||||||
// Send text
|
// Send text
|
||||||
$textMsg = "رمز التحقق الخاص بك لمتجر نابه هو: *{$code}* \n الرجاء عدم مشاركته مع أي شخص.";
|
$textMsg = "رمز التحقق الخاص بك لمتجر نابه هو: *{$code}* \n الرجاء عدم مشاركته مع أي شخص.";
|
||||||
|
|||||||
Reference in New Issue
Block a user