Fixing Paystack Integration in Laravel Deployment: URI must be a string or UriInterface
Deploying a Laravel application with Paystack integration can sometimes lead to unexpected issues. Recently, I faced an InvalidArgumentException
error during deployment:
InvalidArgumentException: URI must be a string or UriInterface in .../public_html/vendor/guzzlehttp/psr7/src/Utils.php:461
The Issue
The error originated from the guzzlehttp/psr7
package, which is commonly used for handling HTTP requests in Laravel applications. The problem was traced to the Paystack configuration in config/paystack.php
:
<?php
return [
'publicKey' => getenv('PAYSTACK_PUBLIC_KEY'),
'secretKey' => getenv('PAYSTACK_SECRET_KEY'),
'paymentUrl' => getenv('PAYSTACK_PAYMENT_URL'),
'merchantEmail' => getenv('MERCHANT_EMAIL'),
];
The getenv()
function was not retrieving environment variables as expected in the production environment, although it worked fine locally.
The Solution
Replacing getenv()
with Laravel's env()
helper function fixed the issue. Here's the updated config/paystack.php
file:
<?php
return [
'publicKey' => env('PAYSTACK_PUBLIC_KEY'),
'secretKey' => env('PAYSTACK_SECRET_KEY'),
'paymentUrl' => env('PAYSTACK_PAYMENT_URL'),
'merchantEmail' => env('MERCHANT_EMAIL'),
];
Why This Works
The env()
helper function in Laravel retrieves environment variables from the .env
file, ensuring consistent configuration across different environments. In contrast, getenv()
might not work reliably in all server setups.
Project Setup Details
- PHP version: 8.1.29 (cli)
- Laravel version: 10.10
- Composer package:
unicodeveloper/laravel-paystack
Step-by-Step Guide to Implementing the Solution
- Edit the Configuration File: Replace
getenv()
withenv()
inconfig/paystack.php
.
<?php
return [
'publicKey' => env('PAYSTACK_PUBLIC_KEY'),
'secretKey' => env('PAYSTACK_SECRET_KEY'),
'paymentUrl' => env('PAYSTACK_PAYMENT_URL'),
'merchantEmail' => env('MERCHANT_EMAIL'),
];
2. Verify Environment Variables: Ensure your .env
file contains the correct Paystack environment variables.
PAYSTACK_PUBLIC_KEY=your-public-key
PAYSTACK_SECRET_KEY=your-secret-key
PAYSTACK_PAYMENT_URL=https://api.paystack.co
MERCHANT_EMAIL=your-email@example.com
3. Clear Config Cache: Clear the Laravel config cache to ensure the changes take effect.
php artisan config:cache
4. Deploy and Test: Deploy your application and test the Paystack integration to confirm the issue is resolved.
Conclusion
Using the env()
function instead of getenv()
ensures your Laravel application retrieves environment variables reliably in various deployment environments. This simple change resolved the InvalidArgumentException
and ensured smooth integration with Paystack.
Happy coding!