laravel-xero icon indicating copy to clipboard operation
laravel-xero copied to clipboard

Data not inserting in xero_tokens table

Open TareqImam opened this issue 7 months ago • 0 comments

Initially, when I integrated the package, it worked perfectly and stored data in the xero_tokens table. I successfully integrated APIs like contact, invoice, item, etc. However, after running some migration changes with migrate:fresh, the tokens are no longer being stored in the xero_tokens table. As a result, I am experiencing unauthorized access issues when attempting to interact with contacts, invoices, and other data.

Here is my xeroConnect method:

          public function xeroConnect()
          {
              return Xero::connect();
          }

And here is the connect method from the package:

          public function connect(): RedirectResponse|Application|Redirector
          {
              if (request()->has('code')) {
                  try {
                      $params = [
                          'grant_type'   => 'authorization_code',
                          'code'         => request('code'),
                          'redirect_uri' => config('xero.redirectUri')
                      ];
          
                      $result = $this->sendPost(self::$tokenUrl, $params);
          
                      try {
                          $response = Http::withHeaders([
                              'Authorization' => 'Bearer ' . $result['access_token'],
                          ])
                              ->acceptJson()
                              ->get(self::$connectionUrl)
                              ->throw()
                              ->json();
          
                          foreach ($response as $tenant) {
                              $tenantData = [
                                  'auth_event_id'    => $tenant['authEventId'],
                                  'tenant_id'        => $tenant['tenantId'],
                                  'tenant_type'      => $tenant['tenantType'],
                                  'tenant_name'      => $tenant['tenantName'],
                                  'created_date_utc' => $tenant['createdDateUtc'],
                                  'updated_date_utc' => $tenant['updatedDateUtc']
                              ];
          
                              $this->storeToken($result, $tenantData);
                          }
                      } catch (Exception $e) {
                          throw new Exception('Error getting tenant: ' . $e->getMessage());
                      }
          
                      return redirect(config('xero.landingUri'));
                  } catch (Exception $e) {
                      throw new Exception($e->getMessage());
                  }
              }
          
              $url = self::$authorizeUrl . '?' . http_build_query([
                  'response_type' => 'code',
                  'client_id'     => config('xero.clientId'),
                  'redirect_uri'  => config('xero.redirectUri'),
                  'scope'         => config('xero.scopes')
              ]);
          
              return redirect()->away($url);
          }

After some debugging, I discovered that the request('code') is empty, which is why the condition if (request()->has('code')) is never met. Consequently, the token and tenant data are not being stored in the database. I'm fairly inexperienced, and it's possible that I missed some steps or made mistakes along the way. I would appreciate any help or suggestions on how to resolve this issue. Thank you!

TareqImam avatar Jul 09 '24 06:07 TareqImam