public function run() { if (SmsBalance::count() > 0) { return; } SmsBalance::create(array('balance' => 0)); }
public function fire() { $this->comment('top up '); $balance = SmsBalance::first(); if (!$balance) { $this->error('Sms Balance not found'); return; } $amount = $this->argument('amount'); $balance->TopUp($amount, 'Top Up From Console'); }
public function getView() { $smsBalances = SmsBalance::all(); if ($smsBalances->count() == 0) { App::abort(404, 'SMS not found for site'); } if ($smsBalances->count() > 1) { App::abort(500, 'invalid state'); } $smsBalance = $smsBalances->first(); $totalBalance = $smsBalance->balance; $items = SmsTransactionLog::orderBy('created_at', 'DESC')->paginate(100); $pendingItems = NotificationRequest::with(['toUser', 'fromUser'])->where('sent', false)->paginate(100); $lastTransactionLog = SmsTransactionLog::where('delta', '>', 0)->orderBy('created_at', 'DESC')->first(); $last_reload_time = null; $last_reload_amount = null; if ($lastTransactionLog) { $last_reload_time = $lastTransactionLog->created_at; $last_reload_amount = $lastTransactionLog->delta; } return view('sms-manager::view-sms')->with('total_balance', $totalBalance)->with('list', $items)->with('last_reload_time', $last_reload_time)->with('last_reload_amount', $last_reload_amount)->with('pendingItems', $pendingItems); }
public function send($route, $target_id, \Illuminate\Database\Eloquent\Model $user, \Illuminate\Console\Command $command) { $messageObject = $this->getMessage($route, $target_id, $user, $command); $note = "message:{$messageObject->message} - to:{$messageObject->to} - number:{$messageObject->receiver_number}"; $sms_host = Config::get('sms-manager.host'); $sms_route = Config::get('sms-manager.route'); $sms_username = urlencode(Config::get('sms-manager.username')); $sms_password = urlencode(Config::get('sms-manager.password')); $sms_sender = urlencode(Config::get('sms-manager.sender')); $sms_type = urlencode(Config::get('sms-manager.type')); $message = urlencode($messageObject->message); $url = "http://{$sms_host}/{$sms_route}?username={$sms_username}&password={$sms_password}&message={$message}&mobile={$messageObject->receiver_number}&sender={$sms_sender}&type={$sms_type}"; $command->comment("SMS - {$note} - url:{$url}"); if (Config::get('sms-manager.pretend')) { $command->comment("pretend - {$note} - url:{$url}"); } else { $response = file_get_contents($url); if ($command->option('verbose')) { $command->comment('response:' . $response); } if (preg_match('/^1701/', $response)) { $balance = SmsBalance::first(); $balance->Spend($note); return $response; } elseif (preg_match('/^1704/', $response)) { $command->error('Insufficient Credits from provider'); } elseif (preg_match('/^1705/', $response)) { $command->error('Invalid Mobile Number'); $item = new InvalidPhone(); $item->phone = $messageObject->receiver_number; $item->save(); } else { $command->error('Unmatched Error:' . $response); } } return false; }