80 lines
3.0 KiB
PHP
80 lines
3.0 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);
|
|
|
|
// 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'];
|
|
}
|
|
}
|