Viewing file: Rent.php (4.01 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Controllers;
use App\Libraries\RentLibrary;
class Rent extends BaseController
{
public function home()
{
if (!user()) {
return redirect()->to('/login');
}
$data['page'] = createMeta('Numara Kirala');
return view(currentTheme() . '/Panel/rent', $data);
}
public function numbers()
{
if (!user()) {
return redirect()->to('/login');
}
$data['page'] = createMeta('Kiraladığım Numaralar');
$data['numbers'] = db('rent')->where('user', user()['id'])->get()->getResultArray();
return view(currentTheme() . '/Panel/rent_numbers', $data);
}
public function getCountries()
{
if (!user()) {
return redirect()->to('/login');
}
$api = new RentLibrary();
if (!$response = cache('RENT_COUNTRIES')) {
$response = $api->getCountries();
cache()->save('RENT_COUNTRIES', $response, 60 * 60 * 24);
}
return $this->response->setJSON($response);
}
public function getServicesByCountry($country, $time)
{
if (!user()) {
return redirect()->to('/login');
}
$api = new RentLibrary();
if (!$response = cache("RENT_{$country}_{$time}")) {
$response = $api->getServicesByCountry($country, $time);
cache()->save("RENT_{$country}_{$time}", $response, 60 * 30);
}
return $this->response->setJSON($response);
}
public function buyNumber($service, $country, $time)
{
if (!user()) {
return redirect()->to('/login');
}
$api = new RentLibrary();
if (!$services = cache("RENT_{$country}_{$time}")) {
$services = $api->getServicesByCountry($country, $time);
cache()->save("RENT_{$country}_{$time}", $services, 60 * 30);
}
$serv = [];
foreach ($services as $ser){
if ($ser['id'] == $service){
$serv = $ser;
break;
}
}
if (empty($serv)){
return $this->response->setJSON([
'status' => false,
'title' => 'Bilinmeyen servis.'
]);
}
if(user()['balance'] < $serv["cost"]) {
return $this->response->setJSON([
'status' => false,
'title' => "Yetersiz bakiye."
]);
}
$response = $api->buyNumber($service, $country, $time);
if ($response['status']) {
$phone = $response['result']['phone'];
db('users')->where('id', user()['id'])->update([
'balance' => floatval(user()['balance']) - $serv["cost"]
]);
db('rent')->insert([
'user' => user()['id'],
'service' => $service,
'phone' => $phone['number'],
'nid' => $phone['id'],
'messages' => '[]',
'end_date' => $phone['endDate'],
'status' => 0
]);
}
return $this->response->setJSON([
'title' => $response['title'],
'status' => $response['status']
]);
}
public function readMessages($id)
{
if (!user()) {
return redirect()->to('/login');
}
$process = db('rent')->where('id', $id)->get()->getFirstRow();
if (!$process) return $this->response->setJSON([]);
$messages = $process->messages;
if ($process->status == '0') {
$api = new RentLibrary();
$response = $api->getStatus($process->nid);
if ($response['status'] == 'error') {
db('rent')->where('id', $id)->update(['status' => 1]);
} else {
$messages = [];
foreach ($response['values'] as $_ => $val) {
$messages[] = $val;
}
db('rent')->where('id', $id)->update(['messages' => $messages]);
}
}
return $this->response->setJSON($messages);
}
}
|