-1

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?

  • The principle behind a Webhook is that _their_ servers make a POST request to yours. The client browser, that your user is sitting in front of, is not involved in that request at all. So where did you expect the session _id_, that would be necessary to pick up the user's session, to come from now in this scenario ...? – CBroe Aug 17 '23 at 13:32
  • In the webhook method I use this if if($productId === 217150) { $cpf = $session->get('cpf'); log_message('debug', '---------------------- TEST -------------------'); log_message('debug', 'billId: ' . $cpf); log_message('debug', 'billStatus: ' . $billStatus); } but it is returning null the CPF that is stored in the session – Anderson Santos Aug 17 '23 at 13:42
  • and at that moment there is already a CPF in the stored session – Anderson Santos Aug 17 '23 at 13:48
  • _"and at that moment there is already a CPF in the stored session"_ - in _some_ session. But how do you expect to _access_ that session again now, without the session ID? The webhook request does not pass any session ID, and so your system will simply create a new, empty session at that point. – CBroe Aug 17 '23 at 14:07
  • ok i think i understand. Do you have any other suggestion to store the CPF and use it when the webhook makes the request for my method ? – Anderson Santos Aug 17 '23 at 14:15
  • If that service you are using there doesn't allow you to pass any identifying information that the webhook will send back to you, then you will have to find another way to associate whatever data it returns, with your customer. – CBroe Aug 18 '23 at 06:24

0 Answers0