Skip to content

Commit

Permalink
Add dynamic configuration settings
Browse files Browse the repository at this point in the history
  • Loading branch information
seunex17 committed Oct 4, 2024
1 parent 4d9bb03 commit eb34ef0
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 56 deletions.
2 changes: 1 addition & 1 deletion build/.php-cs-fixer.cache

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"require": {
"ext-json": "*",
"php": "^7.4 || ^8"
"php": "^7.4 || ^8",
"codeigniter4/settings": "^2.2"
}
}
60 changes: 58 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

142 changes: 142 additions & 0 deletions src/Commands/FlutterwavePublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);
/**
* Copyright (C) ZubDev Digital Media - All Rights Reserved
*
* File: FlutterwavePublishCommand.php
* Author: Zubayr Ganiyu
* Email: <[email protected]>
* Website: https://zubdev.net
* Date: 10/4/24
* Time: 7:42 AM
*/

namespace Seunex17\FlutterwaveCi4\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Config\Autoload;

class FlutterwavePublishCommand extends BaseCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'Flutterwave';

/**
* The Command's name
*
* @var string
*/
protected $name = 'flutterwave:publish';

/**
* the Command's usage description
*
* @var string
*/
protected $usage = 'flutterwave:publish';

/**
* the Command's short description
*
* @var string
*/
protected $description = 'Publish flutterwave functionality into the current application.';

/**
* the Command's options description
*
* @var array
*/
protected $options = [
'-f' => 'Force overwrite all existing files in destination',
];

protected string $sourcePath;

/**
* {@inheritDoc}
*/
public function run(array $params): void
{
$this->determineSourcePath();

$this->publishConfig();
}

/**
* Publish config auth.
*
* @return mixed
*/
protected function publishConfig()
{
$path = "{$this->sourcePath}/Config/Flutterwave.php";

$content = file_get_contents($path);
$content = str_replace('namespace Seunex17\FlutterwaveCi4\Config', 'namespace Config', $content);
$content = str_replace("use CodeIgniter\\Config\\BaseConfig;\n", '', $content);
$content = str_replace('extends BaseConfig', 'extends \\Seunex17\\FlutterwaveCi4\\Config\\Flutterwave', $content);

$namespace = defined('APP_NAMESPACE') ? APP_NAMESPACE : 'App';

$this->writeFile('Config/Flutterwave.php', $content);
}

/**
* Determines the current source path from which all other files are located.
*
* @return void
*/
protected function determineSourcePath(): void
{
$this->sourcePath = realpath(__DIR__ . '/../');

if ($this->sourcePath === '/' || empty($this->sourcePath)) {
CLI::error('Unable to determine the correct source directory. Bailing.');

exit();
}
}

/**
* Write a file, catching any exceptions and showing a
* nicely formatted error.
*
* @return void
*/
protected function writeFile(string $path, string $content): void
{
$config = new Autoload();
$appPath = $config->psr4[APP_NAMESPACE];

$filename = $appPath . $path;
$directory = dirname($filename);

if (! is_dir($directory)) {
mkdir($directory, 0777, true);
}

if (file_exists($filename)) {
$overwrite = (bool) CLI::getOption('f');

if (! $overwrite && CLI::prompt("File '{$path}' already exists in destination. Overwrite?", ['n', 'y']) === 'n') {
CLI::error("Skipped {$path}. If you wish to overwrite, please use the '-f' option or reply 'y' to the prompt.");

return;
}
}

if (write_file($filename, $content)) {
CLI::write(CLI::color('Created: ', 'green') . $path);
} else {
CLI::error("Error creating {$path}.");
}
}
}
44 changes: 43 additions & 1 deletion src/Config/Flutterwave.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,47 @@

class Flutterwave extends BaseConfig
{
public string $baseUrl = 'https://api.flutterwave.com/v3';
/*
* This allow you to enable dynamic settings
* if change from flase to true your api key will no longer be loaded from env file
* It will now be loaded from this config file
* If you have Codeigniter4 settings install it will be used instead.
*/
public bool $enableDynamicSettings = false;

/*
* Set you flutterwave public key here
* Note: dynamic setting must be enable to use this
*/
public string $publicKey = '';

/*
* Set you flutterwave secrete key here
* Note: dynamic setting must be enable to use this
*/
public string $secretKey = '';

/*
* Set you flutterwave encryption key here
* Note: dynamic setting must be enable to use this
*/
public string $encryptionKey = '';

/*
* Set you flutterwave webhook secret here
* Note: dynamic setting must be enable to use this
*/
public string $webhookSecret = '';

/*
* Set you flutterwave payment title
* Note: dynamic setting must be enable to use this
*/
public string $paymentTitle = '';

/*
* Set you flutterwave logo url here
* Note: dynamic setting must be enable to use this
*/
public string $paymentLogoUrl = '';
}
47 changes: 22 additions & 25 deletions src/Flutterwave/CollectPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
use Config\Services;
use Exception;
use Seunex17\FlutterwaveCi4\Config\Flutterwave;
use Seunex17\FlutterwaveCi4\FlutterwaveConfig;

class CollectPayment
class CollectPayment extends FlutterwaveConfig
{
/**
* @throws Exception
*/
public static function standard(array $data): RedirectResponse
{
$flutterwave = new Flutterwave();
$client = Services::curlrequest();
$client = Services::curlrequest();
$config = new Flutterwave();

$request = $client->request('POST', "{$flutterwave->baseUrl}/payments", [
$request = $client->request('POST', self::BASE_URL . '/payments', [
'headers' => [
'Authorization' => 'Bearer ' . env('FLUTTERWAVE_SECRET_KEY'),
'Authorization' => 'Bearer ' . self::secretKey(),
],
'json' => [
'tx_ref' => (string) $data['tx_ref'] ?? null,
Expand All @@ -36,8 +37,8 @@ public static function standard(array $data): RedirectResponse
'name' => $data['customer_name'] ?? null,
],
'customizations' => [
'title' => env('FLUTTERWAVE_PAYMENT_TITLE'),
'logo' => env('FLUTTERWAVE_PAYMENT_LOGO'),
'title' => self::paymentTitle(),
'logo' => self::paymentLogo(),
],
],
'http_errors' => false,
Expand All @@ -57,12 +58,11 @@ public static function standard(array $data): RedirectResponse
*/
public static function card(array $data)
{
$flutterwave = new Flutterwave();
$client = Services::curlrequest();
$client = Services::curlrequest();

$request = $client->request('POST', "{$flutterwave->baseUrl}/charges?type=card", [
$request = $client->request('POST', self::BASE_URL . '/charges?type=card', [
'headers' => [
'Authorization' => 'Bearer ' . env('FLUTTERWAVE_SECRET_KEY'),
'Authorization' => 'Bearer ' . self::secretKey(),
],
'json' => [
'card_number' => $data['card_number'] ?? null,
Expand Down Expand Up @@ -93,14 +93,13 @@ public static function card(array $data)
*/
public static function bankTransfer(array $data)
{
$flutterwave = new Flutterwave();
$client = Services::curlrequest();
$requests = Services::request();
$client = Services::curlrequest();
$requests = Services::request();
helper('flutterwave');

$request = $client->request('POST', "{$flutterwave->baseUrl}/charges?type=bank_transfer", [
$request = $client->request('POST', self::BASE_URL . '/charges?type=bank_transfer', [
'headers' => [
'Authorization' => 'Bearer ' . env('FLUTTERWAVE_SECRET_KEY'),
'Authorization' => 'Bearer ' . self::secretKey(),
],
'json' => [
'phone_number' => $data['phone_number'] ?? null,
Expand All @@ -127,13 +126,12 @@ public static function bankTransfer(array $data)

public static function tokenizeCharge(array $data)
{
$flutterwave = new Flutterwave();
$client = Services::curlrequest();
$requests = Services::request();
$client = Services::curlrequest();
$requests = Services::request();

$request = $client->request('POST', "{$flutterwave->baseUrl}/tokenized-charges", [
$request = $client->request('POST', self::BASE_URL . '/tokenized-charges', [
'headers' => [
'Authorization' => 'Bearer ' . env('FLUTTERWAVE_SECRET_KEY'),
'Authorization' => 'Bearer ' . self::secretKey(),
],
'json' => [
'token' => $data['token'] ?? null,
Expand Down Expand Up @@ -163,12 +161,11 @@ public static function tokenizeCharge(array $data)
*/
public static function mobileMoneyUganda(array $data): RedirectResponse
{
$flutterwave = new Flutterwave();
$client = Services::curlrequest();
$client = Services::curlrequest();

$request = $client->request('POST', "{$flutterwave->baseUrl}/charges?type=mobile_money_uganda", [
$request = $client->request('POST', self::BASE_URL . '/charges?type=mobile_money_uganda', [
'headers' => [
'Authorization' => 'Bearer ' . env('FLUTTERWAVE_SECRET_KEY'),
'Authorization' => 'Bearer ' . self::secretKey(),
],
'json' => [
'tx_ref' => (string) $data['tx_ref'] ?? null,
Expand Down
Loading

0 comments on commit eb34ef0

Please sign in to comment.