public static function smsBalance() { $smsApi = new SmsApi(); // Set API Credentials before calling getSmsBalance() $smsApi->setUsername(getenv('SMS_USERNAME')); $smsApi->setPassword(getenv('SMS_PASSWORD')); $smsApi->setUrl('http://98.102.204.231/smsapi/GetCreditBalance.aspx?'); // Cache result and return value if (Cache::has('sms_balance')) { return Cache::get('sms_balance', 0); } else { $balance = $smsApi->getSmsBalance(); if ($balance) { // Cache for 1 hour before checking for balance $time = Carbon::now()->addMinutes(360); // Cache for 6 Hours Cache::add('sms_balance', $balance, $time); return Cache::get('sms_balance'); } return false; } }
public static function sendSms($data) { $destinationNos = $data['destination_nos']; $senderId = $data['sender_id']; $messageBody = $data['sms_message']; $longSms = isset($data['long_sms']) ? 1 : 0; $smsApi = new SmsApi(); // Set API Credentials before calling getSmsBalance() $smsApi->setUsername(getenv('SMS_USERNAME')); $smsApi->setPassword(getenv('SMS_PASSWORD')); $smsApi->setUrl('http://98.102.204.231/smsapi/Send.aspx?'); $smsApi->setDestinationNos($destinationNos); $smsApi->setLongSms($longSms); $smsApi->setSmsMessage($messageBody); $smsApi->setSenderId($senderId); $response = $smsApi->send(); // Persist Sent SMS to DB if (!$response) { return false; } else { static::create(['destinationnos' => $destinationNos, 'senderno' => $senderId, 'messagebody' => $messageBody, 'long_sms' => $longSms]); // Cache result and return value $smsApi->setUrl('http://98.102.204.231/smsapi/GetCreditBalance.aspx?'); $balance = $smsApi->getSmsBalance(); if ($balance) { $time = Carbon::now()->addMinutes(360); // Cache for 6 hours if (Cache::has('sms_balance')) { // Force a Refresh Cache::forget('sms_balance'); // Add new balance to Cache Cache::put('sms_balance', $balance, $time); } else { // Add new balance to Cache Cache::add('sms_balance', $balance, $time); } } return $response; } }