John Davidson

php - Can we change stripes keys in cashier dynamically in laravel 8?

0 comments
Message:


I am using stripe with Laravel cashier. I want to make stripe keys dynamic instead of saving keys in .env file. I saved keys in database and now want to use these keys in cashier. Cashier get these keys from .env but i want to get these from database.



this is laravel/cashier/config/casher.php file where it access keys and i want to set my values there. I can't use eloquent in this file.



My Answer:
To dynamically change the Stripe keys in Laravel Cashier, you can create a custom service provider and use it to set the Stripe keys from the database. Here's how you can do it:

1. Create a custom service provider by running the following command:
bash
php artisan make:provider StripeServiceProvider


2. In the `StripeServiceProvider` class, you can override the `register` method to set the Stripe keys from the database. Here's an example of how you can do it:

php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Stripe\Stripe;

class StripeServiceProvider extends ServiceProvider
{
public function register()
{
$stripeKey = // Get the Stripe key from the database
$stripeSecret = // Get the Stripe secret from the database

Stripe::setApiKey($stripeSecret);
}
}


3. Register the custom service provider in your `config/app.php` file:

php
'providers' => [
// Other service providers
App\Providers\StripeServiceProvider::class,
],


4. Make sure to update your database values for the Stripe keys whenever they change.

By following these steps, you can dynamically change the Stripe keys in Laravel Cashier by setting them from the database instead of the `.env` file.

Rate this post

5 of 5 based on 9391 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us