106 lines
4.3 KiB
PHP
106 lines
4.3 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'];
|
|
}
|
|
|
|
// Clean phone number (remove +, spaces, etc)
|
|
$to = preg_replace('/[^0-9]/', '', $to);
|
|
|
|
// Add 968 prefix if missing and it's an 8-digit Omani number
|
|
if (strlen($to) === 8) {
|
|
$to = '968' . $to;
|
|
}
|
|
|
|
// Prepare data
|
|
$data = [
|
|
'phone' => $to,
|
|
'message' => $message,
|
|
];
|
|
|
|
// Add security key if provided (using both common keys to ensure compatibility)
|
|
if (!empty($securityKey)) {
|
|
$data['security_key'] = $securityKey;
|
|
$data['secret_key'] = $securityKey;
|
|
}
|
|
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, array("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 sendThankYou($donation) {
|
|
$name = !empty($donation['donor_name']) ? $donation['donor_name'] : 'Donor';
|
|
$amount = number_format($donation['amount'], 3);
|
|
|
|
// Fetch case title
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT title_en FROM cases WHERE id = ?");
|
|
$stmt->execute([$donation['case_id']]);
|
|
$case = $stmt->fetch();
|
|
$caseTitle = $case ? $case['title_en'] : 'a cause';
|
|
|
|
$message = "Dear $name,\n\nThank you for your generous donation of OMR $amount to \"$caseTitle\".\n\nYour support makes a real difference! ❤️\n\nCharityHub Team";
|
|
|
|
if (!empty($donation['donor_phone'])) {
|
|
return self::sendMessage($donation['donor_phone'], $message);
|
|
}
|
|
|
|
return ['success' => false, 'error' => 'No phone number provided'];
|
|
}
|
|
|
|
public static function sendGiftNotification($donation) {
|
|
if (empty($donation['is_gift']) || empty($donation['gift_recipient_phone'])) {
|
|
return ['success' => false, 'error' => 'Not a gift or no recipient phone'];
|
|
}
|
|
|
|
$donorName = !empty($donation['donor_name']) ? $donation['donor_name'] : 'A generous donor';
|
|
$recipientName = !empty($donation['gift_recipient_name']) ? $donation['gift_recipient_name'] : 'Friend';
|
|
$giftMessage = !empty($donation['gift_message']) ? "\n\nMessage: \"" . $donation['gift_message'] . "\"" : "";
|
|
|
|
// Fetch case title
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT title_en FROM cases WHERE id = ?");
|
|
$stmt->execute([$donation['case_id']]);
|
|
$case = $stmt->fetch();
|
|
$caseTitle = $case ? $case['title_en'] : 'a charitable cause';
|
|
|
|
$message = "Hello $recipientName! ✨\n\n$donorName has made a donation to \"$caseTitle\" in your name as a special gift.$giftMessage\n\nMay this kindness bring joy to your day! ❤️\n\nCharityHub Team";
|
|
|
|
return self::sendMessage($donation['gift_recipient_phone'], $message);
|
|
}
|
|
}
|