Skip to content

Commit

Permalink
Fix: compatibility for laravel 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghostscypher committed Aug 27, 2024
1 parent c0c887f commit 095903d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
laravel: ['8.*', '9.*', '10.*', '11.*']
php: [7.4, 8.1, 8.2, 8.3]
laravel: ['7.*', 8.*', '9.*', '10.*', '11.*']
stability: [prefer-stable]
include:
- laravel: 10.*
Expand Down
11 changes: 6 additions & 5 deletions src/Concerns/MpesaAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function generateToken(bool $force = false): ?string
if (! $force && ($token = app(config('mpesa.models.token'))->latest()->first())) {
$this->token = $token->token;

$this->http_client->replaceHeaders([
self::updateHttpClient([
'Authorization' => 'Bearer '.$this->token,
]);

Expand All @@ -58,10 +58,11 @@ public function generateToken(bool $force = false): ?string
}

// If the token is not valid, generate a new one
$response = $this->http_client
->replaceHeaders([
self::updateHttpClient([
'Authorization' => 'Basic '.$this->getCredentials(),
])
]);

$response = $this->http_client
->get('/oauth/v1/generate', [
'grant_type' => 'client_credentials',
]);
Expand Down Expand Up @@ -102,7 +103,7 @@ public function generateToken(bool $force = false): ?string
$this->addRequestTraceId();

// Update the headers
$this->http_client->replaceHeaders([
self::updateHttpClient([
'Authorization' => 'Bearer '.$this->token,
]);

Expand Down
22 changes: 21 additions & 1 deletion src/Concerns/MpesaGlobalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ public function __construct()
$this->refreshEnv();
}

protected function updateHttpClient(array $headers = [], array $options = []): PendingRequest
{
$this->addRequestTraceId();

// Get the original headers
$original_headers = $this->http_client->getHeaders();

// Merge the headers
$headers = array_merge($original_headers, $headers);

// Update the headers
$this->http_client->withHeaders($headers);

// Update the options
$original_options = $this->http_client->getOptions();
$this->http_client->withOptions(array_merge($original_options, $options));

return $this->http_client;
}

/**
* Refresh the environment variables, this prevents errors between requests
* during the app lifecycle
Expand Down Expand Up @@ -56,7 +76,7 @@ protected function refreshEnv()
*/
protected function addRequestTraceId()
{
$this->http_client->replaceHeaders([
self::updateHttpClient([
'X-Ghostscypher-Laravel-Mpesa-Request-ID' => uniqid(microtime(true), true),
'X-Ghostscypher-Laravel-Mpesa-Request-Timestamp' => now()->format('Y-m-d H:i:s'),
]);
Expand Down
3 changes: 1 addition & 2 deletions src/Mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function rawRequest(string $method, string $url, array $data = [], array
$this->generateToken();
}

$response = $this->http_client
->replaceHeaders($headers)
$response = self::getHttpClient($headers)
->send($method, $url, $data);

return $response;
Expand Down

0 comments on commit 095903d

Please sign in to comment.