Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":[],"times":{"Unicodeveloper\\Paystack\\Test\\HelpersTest::it_returns_instance_of_paystack":0.213,"Unicodeveloper\\Paystack\\Test\\PaystackTest::testAllCustomersAreReturned":0.089,"Unicodeveloper\\Paystack\\Test\\PaystackTest::testAllTransactionsAreReturned":0.001,"Unicodeveloper\\Paystack\\Test\\PaystackTest::testAllPlansAreReturned":0.001}}
{"version":2,"defects":[],"times":{"Unicodeveloper\\Paystack\\Test\\TransRefTest::testGeneratesTokenOfCorrectLength":0.001,"Unicodeveloper\\Paystack\\Test\\TransRefTest::testGeneratesDifferentTokens":0,"Unicodeveloper\\Paystack\\Test\\TransRefTest::testUsesDefaultLength":0,"Unicodeveloper\\Paystack\\Test\\PaystackTest::testAllCustomersAreReturned":0.001,"Unicodeveloper\\Paystack\\Test\\PaystackTest::testAllTransactionsAreReturned":0,"Unicodeveloper\\Paystack\\Test\\PaystackTest::testAllPlansAreReturned":0,"Unicodeveloper\\Paystack\\Test\\HelpersTest::testReturnsInstanceOfPaystack":0}}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,20 @@ Expiry Date: any date in the future
CVV: 883
```

## Compatibility

| Laravel Version | PHP Version |
|-----------------|------------|
| 6.x | 7.2+ |
| 7.x | 7.2+ |
| 8.x | 7.2+ |
| 9.x | 8.0+ |
| 10.x | 8.0+ |
| 11.x | 8.1+ |
| 12.x | 8.2+ |

> This package has been tested with Laravel 6 through 12 and PHP 7.2 through 8.4.

## Todo

* Charge Returning Customers
Expand Down
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
"paystack.co",
"laravel 6",
"laravel 7",
"laravel 8"
"laravel 8",
"laravel 9",
"laravel 10",
"laravel 11",
"laravel 12"
],
"license": "MIT",
"authors": [
Expand All @@ -27,12 +31,12 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.2|^8.0|^8.1",
"illuminate/support": "~6|~7|~8|~9|^10.0|^11.0",
"php": "^7.2|^8.0|^8.1|^8.2|^8.3|^8.4",
"illuminate/support": "~6|~7|~8|~9|^10.0|^11.0|^12.0",
"guzzlehttp/guzzle": "~6|~7|~8|~9"
},
"require-dev": {
"phpunit/phpunit": "^8.4|^9.0|^10.5",
"phpunit/phpunit": "^8.4|^9.0|^10.5|^12.3",
"scrutinizer/ocular": "~1.1",
"php-coveralls/php-coveralls": "^2.0",
"mockery/mockery": "^1.3"
Expand Down
63 changes: 31 additions & 32 deletions src/Paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class Paystack
*/
protected $authorizationUrl;

/**
* Authorization URL returned from Paystack after initializing a transaction.
*
* @var string
*/
protected string $url;

public function __construct()
{
$this->setKey();
Expand All @@ -68,23 +75,23 @@ public function __construct()
/**
* Get Base Url from Paystack config file
*/
public function setBaseUrl()
public function setBaseUrl(): void
{
$this->baseUrl = Config::get('paystack.paymentUrl');
}

/**
* Get secret key from Paystack config file
*/
public function setKey()
public function setKey(): void
{
$this->secretKey = Config::get('paystack.secretKey');
}

/**
* Set options for making the Client request
*/
private function setRequestOptions()
private function setRequestOptions(): void
{
$authBearer = 'Bearer ' . $this->secretKey;

Expand All @@ -100,16 +107,13 @@ private function setRequestOptions()
);
}


/**

* Initiate a payment request to Paystack
* Included the option to pass the payload to this method for situations
* when the payload is built on the fly (not passed to the controller from a view)
* @return Paystack
*/

public function makePaymentRequest($data = null)
public function makePaymentRequest(?array $data = null): self
{
if ($data == null) {

Expand Down Expand Up @@ -188,7 +192,7 @@ public function makePaymentRequest($data = null)
* @return Paystack
* @throws IsNullException
*/
private function setHttpResponse($relativeUrl, $method, $body = [])
private function setHttpResponse(string $relativeUrl, string $method, array $body = []): self
{
if (is_null($method)) {
throw new IsNullException("Empty method not allowed");
Expand All @@ -206,12 +210,10 @@ private function setHttpResponse($relativeUrl, $method, $body = [])
* Get the authorization url from the callback response
* @return Paystack
*/
public function getAuthorizationUrl($data = null)
public function getAuthorizationUrl(?array $data = null): self
{
$this->makePaymentRequest($data);

$this->url = $this->getResponse()['data']['authorization_url'];

return $this;
}

Expand All @@ -221,37 +223,31 @@ public function getAuthorizationUrl($data = null)
* and might need to take different actions based on the success or not of the transaction
* @return array
*/
public function getAuthorizationResponse($data)
public function getAuthorizationResponse(?array $data): array
{
$this->makePaymentRequest($data);

$this->url = $this->getResponse()['data']['authorization_url'];

return $this->getResponse();
}

/**
* Hit Paystack Gateway to Verify that the transaction is valid
*/
private function verifyTransactionAtGateway($transaction_id = null)
private function verifyTransactionAtGateway(?string $transaction_id = null): void
{
$transactionRef = $transaction_id ?? request()->query('trxref');

$relativeUrl = "/transaction/verify/{$transactionRef}";

$this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
}

/**
* True or false condition whether the transaction is verified
* @return boolean
*/
public function isTransactionVerificationValid($transaction_id = null)
public function isTransactionVerificationValid(?string $transaction_id = null): bool
{
$this->verifyTransactionAtGateway($transaction_id);

$result = $this->getResponse()['message'];

switch ($result) {
case self::VS:
$validate = true;
Expand Down Expand Up @@ -283,8 +279,10 @@ public function getPaymentData()

/**
* Fluent method to redirect to Paystack Payment Page
*
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectNow()
public function redirectNow(): \Illuminate\Http\RedirectResponse
{
return redirect($this->url);
}
Expand All @@ -293,7 +291,7 @@ public function redirectNow()
* Get Access code from transaction callback respose
* @return string
*/
public function getAccessCode()
public function getAccessCode(): string
{
return $this->getResponse()['data']['access_code'];
}
Expand All @@ -302,7 +300,7 @@ public function getAccessCode()
* Generate a Unique Transaction Reference
* @return string
*/
public function genTranxRef()
public function genTranxRef(): string
{
return TransRef::getHashedToken();
}
Expand All @@ -311,7 +309,7 @@ public function genTranxRef()
* Get all the customers that have made transactions on your platform
* @return array
*/
public function getAllCustomers()
public function getAllCustomers(): array
{
$this->setRequestOptions();

Expand All @@ -322,7 +320,7 @@ public function getAllCustomers()
* Get all the plans that you have on Paystack
* @return array
*/
public function getAllPlans()
public function getAllPlans(): array
{
$this->setRequestOptions();

Expand All @@ -333,7 +331,7 @@ public function getAllPlans()
* Get all the transactions that have happened overtime
* @return array
*/
public function getAllTransactions()
public function getAllTransactions(): array
{
$this->setRequestOptions();

Expand All @@ -342,9 +340,10 @@ public function getAllTransactions()

/**
* Get the whole response from a get operation
* @return array
*
* @return array|null
*/
private function getResponse()
private function getResponse(): ?array
{
return json_decode($this->response->getBody(), true);
}
Expand All @@ -353,7 +352,7 @@ private function getResponse()
* Get the data response from a get operation
* @return array
*/
private function getData()
private function getData(): array
{
return $this->getResponse()['data'];
}
Expand Down Expand Up @@ -426,7 +425,7 @@ public function createCustomer($data = null)

];
}

$this->setRequestOptions();
return $this->setHttpResponse('/customer', 'POST', $data)->getResponse();
}
Expand Down Expand Up @@ -699,10 +698,10 @@ public function updateSubAccount($subaccount_code)
return $this->setHttpResponse("/subaccount/{$subaccount_code}", "PUT", array_filter($data))->getResponse();
}


/**
* Get a list of all supported banks and their properties
* @param $country - The country from which to obtain the list of supported banks, $per_page - Specifies how many records to retrieve per page ,
* @param $country - The country from which to obtain the list of supported banks, $per_page - Specifies how many records to retrieve per page ,
* $use_cursor - Flag to enable cursor pagination on the endpoint
* @return array
*/
Expand Down
24 changes: 11 additions & 13 deletions src/PaystackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,32 @@ class PaystackServiceProvider extends ServiceProvider
protected $defer = false;

/**
* Publishes all the config file this package needs to function
*/
public function boot()
* Publishes all the config file this package needs to function
*/
public function boot(): void
{
$config = realpath(__DIR__.'/../resources/config/paystack.php');
$config = realpath(__DIR__ . '/../resources/config/paystack.php');

$this->publishes([
$config => config_path('paystack.php')
]);
}

/**
* Register the application services.
*/
public function register()
* Register the application services.
*/
public function register(): void
{
$this->app->bind('laravel-paystack', function () {

return new Paystack;

});
}

/**
* Get the services provided by the provider
* @return array
*/
public function provides()
* Get the services provided by the provider
* @return array
*/
public function provides(): array
{
return ['laravel-paystack'];
}
Expand Down
3 changes: 1 addition & 2 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
if (! function_exists("paystack"))
{
function paystack() {

return app()->make('laravel-paystack');
}
}
}
Loading