144 lines
6.1 KiB
PHP
144 lines
6.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
class WablasService {
|
|
public static function sendMessage($to, $message) {
|
|
$pdo = db();
|
|
$settings = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'wablas_%'")->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
$token = $settings['wablas_api_token'] ?? '';
|
|
$serverUrl = $settings['wablas_server_url'] ?? '';
|
|
$securityKey = $settings['wablas_security_key'] ?? '';
|
|
|
|
if (empty($token) || empty($serverUrl)) {
|
|
error_log("Wablas settings missing.");
|
|
return ['success' => false, 'error' => 'Settings missing'];
|
|
}
|
|
|
|
$to = prefix_phone($to);
|
|
|
|
$data = ['phone' => $to, 'message' => $message];
|
|
if (!empty($securityKey)) {
|
|
$data['security_key'] = $securityKey;
|
|
$data['secret_key'] = $securityKey;
|
|
}
|
|
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: $token"]);
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
curl_setopt($curl, CURLOPT_URL, rtrim($serverUrl, '/') . "/api/send-message");
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
$result = curl_exec($curl);
|
|
$error = curl_error($curl);
|
|
curl_close($curl);
|
|
|
|
if ($error) {
|
|
error_log("Wablas CURL error: " . $error);
|
|
return ['success' => false, 'error' => $error];
|
|
}
|
|
|
|
$response = json_decode($result, true);
|
|
if (isset($response['status']) && $response['status'] == true) {
|
|
return ['success' => true, 'data' => $response];
|
|
}
|
|
|
|
error_log("Wablas API error: " . $result);
|
|
return ['success' => false, 'error' => $result];
|
|
}
|
|
|
|
public static function getOrgName($lang = 'en') {
|
|
$pdo = db();
|
|
$org = $pdo->query("SELECT name_en, name_ar FROM org_profile LIMIT 1")->fetch();
|
|
return ($lang === 'ar') ? ($org['name_ar'] ?? 'المنظمة') : ($org['name_en'] ?? 'Organization');
|
|
}
|
|
|
|
private static function sendTemplatedMessage($to, $templateName, $vars, $lang = 'en') {
|
|
if (empty($to)) {
|
|
return ['success' => false, 'error' => 'Recipient phone number is missing.'];
|
|
}
|
|
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM whatsapp_templates WHERE template_name = ? AND status = 1");
|
|
$stmt->execute([$templateName]);
|
|
$template = $stmt->fetch();
|
|
|
|
if (!$template) {
|
|
error_log("WhatsApp template '$templateName' not found or is disabled.");
|
|
return ['success' => false, 'error' => "Template '$templateName' not found or disabled."];
|
|
}
|
|
|
|
$messageBody = ($lang === 'ar' && !empty($template['template_body_ar'])) ? $template['template_body_ar'] : $template['template_body_en'];
|
|
|
|
foreach ($vars as $key => $value) {
|
|
$messageBody = str_replace('{'.$key.'}', $value, $messageBody);
|
|
}
|
|
|
|
return self::sendMessage($to, $messageBody);
|
|
}
|
|
|
|
public static function sendThankYou($donation, $lang = 'en') {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT title_en, title_ar FROM cases WHERE id = ?");
|
|
$stmt->execute([$donation['case_id']]);
|
|
$case = $stmt->fetch();
|
|
$caseTitle = ($lang === 'ar' && !empty($case['title_ar'])) ? $case['title_ar'] : $case['title_en'];
|
|
|
|
$vars = [
|
|
'donor_name' => !empty($donation['donor_name']) ? $donation['donor_name'] : (($lang === 'ar') ? 'فاعل خير' : 'Anonymous'),
|
|
'amount' => number_format($donation['amount'], 3),
|
|
'case_title' => $caseTitle,
|
|
'org_name' => self::getOrgName($lang)
|
|
];
|
|
|
|
return self::sendTemplatedMessage($donation['donor_phone'], 'donation_thank_you', $vars, $lang);
|
|
}
|
|
|
|
public static function sendGiftNotification($donation, $lang = 'en') {
|
|
if (empty($donation['is_gift'])) {
|
|
return ['success' => false, 'error' => 'Not a gift'];
|
|
}
|
|
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT title_en, title_ar FROM cases WHERE id = ?");
|
|
$stmt->execute([$donation['case_id']]);
|
|
$case = $stmt->fetch();
|
|
$caseTitle = ($lang === 'ar' && !empty($case['title_ar'])) ? $case['title_ar'] : $case['title_en'];
|
|
|
|
$vars = [
|
|
'recipient_name' => !empty($donation['gift_recipient_name']) ? $donation['gift_recipient_name'] : (($lang === 'ar') ? 'صديق' : 'Friend'),
|
|
'donor_name' => !empty($donation['donor_name']) ? $donation['donor_name'] : (($lang === 'ar') ? 'فاعل خير' : 'A generous donor'),
|
|
'case_title' => $caseTitle,
|
|
'gift_message' => !empty($donation['gift_message']) ? "\n\n" . (($lang === 'ar') ? 'رسالة:' : 'Message:') . " \"" . $donation['gift_message'] . "\"" : "",
|
|
'org_name' => self::getOrgName($lang)
|
|
];
|
|
|
|
return self::sendTemplatedMessage($donation['gift_recipient_phone'], 'donation_gift_notification', $vars, $lang);
|
|
}
|
|
|
|
public static function sendCaseDonationNotification($donation, $lang = 'en') {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT c.title_en, c.title_ar, c.contact_phone FROM cases c WHERE c.id = ?");
|
|
$stmt->execute([$donation['case_id']]);
|
|
$case = $stmt->fetch();
|
|
|
|
if (!$case || empty($case['contact_phone'])) {
|
|
return ['success' => false, 'error' => 'Case contact phone not found'];
|
|
}
|
|
|
|
$caseTitle = ($lang === 'ar' && !empty($case['title_ar'])) ? $case['title_ar'] : $case['title_en'];
|
|
|
|
$vars = [
|
|
'amount' => number_format($donation['amount'], 3),
|
|
'case_title' => $caseTitle,
|
|
'donor_name' => !empty($donation['donor_name']) ? $donation['donor_name'] : (($lang === 'ar') ? 'فاعل خير' : 'Anonymous'),
|
|
'org_name' => self::getOrgName($lang)
|
|
];
|
|
|
|
return self::sendTemplatedMessage($case['contact_phone'], 'case_donation_notification', $vars, $lang);
|
|
}
|
|
}
|