157 lines
4.8 KiB
PHP
157 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries;
|
|
|
|
use CodeIgniter\HTTP\CURLRequest;
|
|
|
|
class WablasService
|
|
{
|
|
protected string $token;
|
|
protected string $secretKey;
|
|
protected string $baseUrl = 'https://tegal.wablas.com/api/v2';
|
|
protected $externalApi;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->externalApi = new \Config\ExternalApi();
|
|
$this->token = env('WABLAS_TOKEN') ?: $this->externalApi->wablasToken;
|
|
$this->secretKey = env('WABLAS_SECRET_KEY') ?: $this->externalApi->WablasSecretKey;
|
|
|
|
if (empty($this->token) || empty($this->secretKey)) {
|
|
throw new \Exception('Unauthorized');
|
|
}
|
|
}
|
|
|
|
// Kirim pesan teks biasa (single)
|
|
public function sendText(string $phone, string $message, bool $isGroup = false): array
|
|
{
|
|
return $this->sendBulkText([['phone' => $phone, 'message' => $message, 'isGroup' => $isGroup ? 'true' : 'false']]);
|
|
}
|
|
|
|
// Kirim pesan teks massal (multiple)
|
|
public function sendBulkText(array $messages): array
|
|
{
|
|
$formatted = [];
|
|
foreach ($messages as $msg) {
|
|
if (!isset($msg['phone']) || !isset($msg['message'])) {
|
|
throw new \InvalidArgumentException('Setiap pesan harus memiliki "phone" dan "message"');
|
|
}
|
|
$formatted[] = [
|
|
'phone' => $this->sanitizePhone($msg['phone']),
|
|
'message' => $msg['message'],
|
|
'isGroup' => $msg['isGroup'] ?? 'false'
|
|
];
|
|
}
|
|
|
|
return $this->makeRequest('/send-message', ['data' => $formatted]);
|
|
}
|
|
|
|
// Kirim OTP atau template message
|
|
public function sendTemplate(string $phone, string $content, string $title = 'Verification Code', string $footer = 'Supported by Wablas', string $code = ''): array
|
|
{
|
|
$message = [
|
|
'title' => [
|
|
'type' => 'text',
|
|
'content' => $title
|
|
],
|
|
'content' => $content,
|
|
'footer' => $footer
|
|
];
|
|
|
|
// Hanya tambahkan buttons jika code ada
|
|
if (!empty($code)) {
|
|
$message['buttons'] = [
|
|
'url' => [
|
|
'display' => 'Copy',
|
|
'link' => "https://www.whatsapp.com/otp/copy/" . $code
|
|
]
|
|
];
|
|
}
|
|
|
|
$payload = [
|
|
'data' => [
|
|
[
|
|
'phone' => $this->sanitizePhone($phone),
|
|
'message' => $message
|
|
]
|
|
]
|
|
];
|
|
|
|
return $this->makeRequest('/send-template', $payload);
|
|
}
|
|
|
|
// Kirim OTP sederhana - PERSIS SEPERTI DOKUMENTASI
|
|
public function sendOTP(string $phone, string $code, string $title = 'Verification Code'): array
|
|
{
|
|
$phone = $this->sanitizePhone($phone);
|
|
|
|
$message = [
|
|
'title' => [
|
|
'type' => 'text',
|
|
'content' => $title,
|
|
],
|
|
'buttons' => [
|
|
'url' => [
|
|
'display' => 'Copy',
|
|
'link' => "https://www.whatsapp.com/otp/copy/" . $code,
|
|
],
|
|
],
|
|
'content' => "Your verification code : $code",
|
|
'footer' => 'Supported by Wablas',
|
|
];
|
|
|
|
$payload = [
|
|
'data' => [
|
|
[
|
|
'phone' => $phone,
|
|
'message' => $message
|
|
]
|
|
]
|
|
];
|
|
|
|
return $this->makeRequest('/send-message', $payload);
|
|
}
|
|
|
|
// Fungsi internal untuk eksekusi API
|
|
protected function makeRequest(string $endpoint, array $payload): array
|
|
{
|
|
$curl = \Config\Services::curlrequest();
|
|
|
|
try {
|
|
$response = $curl->post($this->baseUrl . $endpoint, [
|
|
'headers' => [
|
|
'Authorization' => $this->token . '.' . $this->secretKey,
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
'json' => $payload,
|
|
'verify' => false // Sama seperti CURLOPT_SSL_VERIFYPEER = 0
|
|
]);
|
|
|
|
$result = json_decode($response->getBody(), true);
|
|
$statusCode = $response->getStatusCode();
|
|
|
|
return [
|
|
'success' => ($statusCode === 200 && !empty($result)),
|
|
'data' => $result,
|
|
'http_code' => $statusCode
|
|
];
|
|
} catch (\Exception $e) {
|
|
return [
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
// Bersihkan nomor HP ke format internasional
|
|
protected function sanitizePhone(string $phone): string
|
|
{
|
|
$phone = preg_replace('/[^0-9]/', '', $phone);
|
|
if (substr($phone, 0, 2) === '08') {
|
|
$phone = '62' . substr($phone, 1);
|
|
} elseif (substr($phone, 0, 1) === '8') {
|
|
$phone = '62' . $phone;
|
|
}
|
|
return $phone;
|
|
}
|
|
} |