From ba6ea1767245a6e50a1d8dfa013f5be105ee2e3a Mon Sep 17 00:00:00 2001 From: Israel Birhane Date: Tue, 7 Jun 2022 23:12:54 -0600 Subject: [PATCH] Add Payment Verification and Fix the Structure --- .gitignore | 3 +- composer.json | 13 +-- src/Chapa.php | 79 +++++++++++++++++++ src/ChapaLaravel.php | 59 -------------- ...eProvider.php => ChapaServiceProvider.php} | 12 +-- .../Chapa.php} | 8 +- 6 files changed, 99 insertions(+), 75 deletions(-) create mode 100644 src/Chapa.php delete mode 100644 src/ChapaLaravel.php rename src/{ChapaLaravelServiceProvider.php => ChapaServiceProvider.php} (74%) rename src/{ChapaLaravelFacade.php => Facades/Chapa.php} (58%) diff --git a/.gitignore b/.gitignore index 49ce3c1..bd9ddaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/vendor \ No newline at end of file +/vendor +.DS_Store \ No newline at end of file diff --git a/composer.json b/composer.json index 7a084dc..bd52dc2 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "chapa/chapa-laravel", - "description": "", + "description": "A Laravel Package to Accept Payment through Chapa", "keywords": [ "chapa", "chapa-laravel" @@ -25,13 +25,16 @@ "phpunit/phpunit": "^9.0" }, "autoload": { + "classmap": [ + "src/Chapa.php" + ], "psr-4": { - "Chapa\\ChapaLaravel\\": "src" + "Chapa\\Chapa\\": "src/" } }, "autoload-dev": { "psr-4": { - "Chapa\\ChapaLaravel\\Tests\\": "tests" + "Chapa\\Chapa\\Tests\\": "tests" } }, "scripts": { @@ -45,10 +48,10 @@ "extra": { "laravel": { "providers": [ - "Chapa\\ChapaLaravel\\ChapaLaravelServiceProvider" + "Chapa\\Chapa\\ChapaServiceProvider" ], "aliases": { - "ChapaLaravel": "Chapa\\ChapaLaravel\\ChapaLaravelFacade" + "Chapa": "Chapa\\Chapa\\Facades\\Chapa" } } } diff --git a/src/Chapa.php b/src/Chapa.php new file mode 100644 index 0000000..cc2b3c9 --- /dev/null +++ b/src/Chapa.php @@ -0,0 +1,79 @@ +publicKey = env('CHAPA_PUBLIC_KEY'); + $this->secretKey = env('CHAPA_SECRET_KEY'); + $this->secretHash = env('CHAPA_WEBHOOK_SECRET'); + $this->baseUrl = 'https://api.chapa.co/v1'; + } + + public static function generateReference(String $transactionPrefix = NULL) + { + if ($transactionPrefix) { + return $transactionPrefix . '_' . uniqid(time()); + } + return 'chapa_' . uniqid(time()); + } + + /** + * Reaches out to Chapa to initialize a payment + * @param $data + * @return object + */ + public static function initializePayment(array $data) + { + + + $payment = Http::withToken($this->secretKey)->post( + $this->baseUrl . '/transaction/initialize', + $data + )->json(); + + return $payment; + } + + /** + * Reaches out to Chapa to verify a transaction + * @param $id + * @return object + */ + public function verifyTransaction($id) + { + $data = Http::withToken($this->secretKey)->get($this->baseUrl . "/transaction/" . 'verify/'. $id )->json(); + return $data; + } + + public function getTransactionIDFromCallback() + { + $transactionID = request()->tx_ref; + + if (!$transactionID) { + $transactionID = json_decode(request()->resp)->data->tx_ref; + } + + return $transactionID; + } + +} diff --git a/src/ChapaLaravel.php b/src/ChapaLaravel.php deleted file mode 100644 index 7577a72..0000000 --- a/src/ChapaLaravel.php +++ /dev/null @@ -1,59 +0,0 @@ -post( - $baseUrl . '/transaction/initialize', - $data - )->json(); - - return $payment; - } - - /** - * Reaches out to Chapa to verify a transaction - * @param $id - * @return object - */ - public function verifyTransaction($id) - { - - } - - public static function event() - { - - } - -} diff --git a/src/ChapaLaravelServiceProvider.php b/src/ChapaServiceProvider.php similarity index 74% rename from src/ChapaLaravelServiceProvider.php rename to src/ChapaServiceProvider.php index 4b2885c..830c494 100644 --- a/src/ChapaLaravelServiceProvider.php +++ b/src/ChapaServiceProvider.php @@ -1,10 +1,10 @@ mergeConfigFrom(__DIR__.'/../config/config.php', 'chapa-laravel'); + $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravelchapa'); // Register the main class to use with the facade - $this->app->singleton('chapa-laravel', function () { - return new ChapaLaravel; + $this->app->singleton('laravelchapa', function () { + return new Chapa; }); - $this->app->alias('laravelchapa', "Chapa\ChapaLaravel\ChapaLaravel"); + $this->app->alias('laravelchapa', "Chapa\Chapa\Chapa"); } diff --git a/src/ChapaLaravelFacade.php b/src/Facades/Chapa.php similarity index 58% rename from src/ChapaLaravelFacade.php rename to src/Facades/Chapa.php index e1e70b6..8beb230 100644 --- a/src/ChapaLaravelFacade.php +++ b/src/Facades/Chapa.php @@ -1,13 +1,13 @@