Skip to content

Commit

Permalink
Add Payment Verification and Fix the Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Israel Birhane authored and Israel Birhane committed Jun 8, 2022
1 parent a60e82a commit ba6ea17
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor
/vendor
.DS_Store
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chapa/chapa-laravel",
"description": "",
"description": "A Laravel Package to Accept Payment through Chapa",
"keywords": [
"chapa",
"chapa-laravel"
Expand All @@ -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": {
Expand All @@ -45,10 +48,10 @@
"extra": {
"laravel": {
"providers": [
"Chapa\\ChapaLaravel\\ChapaLaravelServiceProvider"
"Chapa\\Chapa\\ChapaServiceProvider"
],
"aliases": {
"ChapaLaravel": "Chapa\\ChapaLaravel\\ChapaLaravelFacade"
"Chapa": "Chapa\\Chapa\\Facades\\Chapa"
}
}
}
Expand Down
79 changes: 79 additions & 0 deletions src/Chapa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Chapa\Chapa;

use Illuminate\Support\Facades\Http;

class Chapa
{

/**
* Generates a unique reference
* @param $transactionPrefix
* @return string
*/



protected $publicKey;
protected $secretKey;
protected $baseUrl;


function __construct()
{

$this->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;
}

}
59 changes: 0 additions & 59 deletions src/ChapaLaravel.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Chapa\ChapaLaravel;
namespace Chapa\Chapa;

use Illuminate\Support\ServiceProvider;

class ChapaLaravelServiceProvider extends ServiceProvider
class ChapaServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
Expand All @@ -27,14 +27,14 @@ public function boot()
public function register()
{
// Automatically apply the package configuration
$this->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");

}

Expand Down
8 changes: 4 additions & 4 deletions src/ChapaLaravelFacade.php → src/Facades/Chapa.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Chapa\ChapaLaravel;
namespace Chapa\Chapa\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Chapa\ChapaLaravel\Skeleton\SkeletonClass
* @see \Chapa\Chapa\Skeleton\SkeletonClass
*/
class ChapaLaravelFacade extends Facade
class Chapa extends Facade
{
/**
* Get the registered name of the component.
Expand All @@ -16,6 +16,6 @@ class ChapaLaravelFacade extends Facade
*/
protected static function getFacadeAccessor()
{
return 'chapa-laravel';
return 'laravelchapa';
}
}

0 comments on commit ba6ea17

Please sign in to comment.