I have a problem with the session in codeigniter 4.3 I have an application, and in this application I need to login, to login I use the CPF and PASSWORD when I log in I store the CPF in the session to use it in other parts of the application so far so good, the CPF is stored correctly. In this application I have this controller
class PointsController extends BaseController
{
public function bills()
{
$client = \Config\Services::curlrequest();
$cpf = session()->get('cpf')
$cpfEncontrado = false;
try {
$resquest = $client->setHeader('accept', 'application/json')
->setHeader('content-type', 'application/json')
->get("https:URL/$cpf");
$listUsers = json_decode($resquest->getBody(), true);
$dataCpf['users'] = array();
foreach ($listUsers['customers'] as $user) {
$id = $user['id'];
$registry_code = $user['registry_code'];
$users[] = array(
'id' => $id,
'registry_code' => $registry_code
);
if ($cpf == $registry_code) {
$cpfEncontrado = true;
break;
}
}
} catch (\Exception $e) {
echo $e->getMessage();
};
...............
}
}
The bill method is just for querying an api with the customer's CPF, if the customer's CPF exists on the platform where I make the query I can pay the invoice, and this method is triggered when I send a form that I created there in HTML and information is sent to that route.
$routes->post('bill', 'PointsController::bills');
So far ok.
The problem is now, there is an external webhook that sends a request to my other method whenever an invoice is paid.
public function webhook()
{
$session = session();
$payload = json_decode($this->request->getBody());
$billId = null;
$billStatus = null;
$productId = null;
if ($payload && isset($payload->event) && isset($payload->event->data) && isset($payload->event->data->bill)) {
$bill = $payload->event->data->bill;
$billId = $bill->id;
$billStatus = $bill->status;
$billItems = $bill->bill_items;
if (!empty($billItems) && isset($billItems[0]->product)) {
$productId = $billItems[0]->product->id;
if($productId === 217150) {
$cpf = $session->get('cpf');
log_message('debug', '---------------------- TEST -------------------');
log_message('debug', 'billId: ' . $cpf);
log_message('debug', 'billStatus: ' . $billStatus);
}
}
} else {
log_message('debug', 'Error');
}
}
At the moment the external webhook sends a request to my method, I try to get the CPF that is stored in the session, but the CPF is returning NULL. And these two methods are in the same controller, how can one method be able to get the CPF that is stored in the session, and the other not? Can anyone tell me what it could be?