Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactored for psr-2 compliance #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2016
Merged
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 resources/config/paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
*/
'merchantEmail' => getenv('MERCHANT_EMAIL'),

];
];
5 changes: 3 additions & 2 deletions src/Exceptions/PaymentVerificationFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Exception;

class PaymentVerificationFailedException extends Exception {
class PaymentVerificationFailedException extends Exception
{

}
}
5 changes: 3 additions & 2 deletions src/Exceptions/isNullException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Exception;

class IsNullException extends Exception {
class IsNullException extends Exception
{

}
}
12 changes: 6 additions & 6 deletions src/Facades/Paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

use Illuminate\Support\Facades\Facade;

class Paystack extends Facade {
class Paystack extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
* Get the registered name of the component
* @return string
*/
protected static function getFacadeAccessor()
{
return 'laravel-paystack';
}
}
}
96 changes: 56 additions & 40 deletions src/Paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Config;

class Paystack {

class Paystack
{
/**
* Transaction Verification Successful
*/
Expand All @@ -28,13 +28,13 @@ class Paystack {

/**
* Issue Secret Key from your Paystack Dashboard
* @var mixed
* @var string
*/
protected $secretKey;

/**
* Instance of Client
* @var object
* @var Client
*/
protected $client;

Expand Down Expand Up @@ -73,7 +73,6 @@ public function setBaseUrl()

/**
* Get secret key from Paystack config file
* @return void
*/
public function setKey()
{
Expand All @@ -82,22 +81,26 @@ public function setKey()

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

$this->client = new Client(['base_uri' => $this->baseUrl,
'headers' => [
'Authorization' => $authBearer,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
]]);
$this->client = new Client(
[
'base_uri' => $this->baseUrl,
'headers' => [
'Authorization' => $authBearer,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
]
]
);
}

/**
* Initiate a payment request to Paystack
* @return Paystack
*/
public function makePaymentRequest()
{
Expand All @@ -110,7 +113,7 @@ public function makePaymentRequest()
"last_name" => request()->last_name,
"callback_url" => request()->callback_url
];

// Remove the fields which were not sent (value would be null)
array_filter($data);

Expand All @@ -120,18 +123,30 @@ public function makePaymentRequest()
}


/**
* @param string $relativeUrl
* @param string $method
* @param array $body
* @return Paystack
* @throws IsNullException
*/
private function setHttpResponse($relativeUrl, $method, $body = [])
{
if(is_null($method)){
if (is_null($method)) {
throw new IsNullException("Empty method not allowed");
}

$this->response = $this->client->{strtolower($method)}($this->baseUrl . $relativeUrl, ["body" => json_encode($body)]);
$this->response = $this->client->{strtolower($method)}(
$this->baseUrl . $relativeUrl,
["body" => json_encode($body)]
);

return $this;
}

/**
* Get the authorization url from the callback response
* @return Paystack
*/
public function getAuthorizationUrl()
{
Expand All @@ -144,7 +159,6 @@ public function getAuthorizationUrl()

/**
* Hit Paystack Gateway to Verify that the transaction is valid
* @return void
*/
private function verifyTransactionAtGateway()
{
Expand All @@ -165,8 +179,7 @@ public function isTransactionVerificationValid()

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

switch ($result)
{
switch ($result) {
case self::VS:
$validate = true;
break;
Expand Down Expand Up @@ -275,8 +288,8 @@ private function getData()
/**
* Create a plan
*/
public function createPlan(){

public function createPlan()
{
$data = [
"name" => request()->name,
"description" => request()->desc,
Expand All @@ -298,7 +311,8 @@ public function createPlan(){
* @param $plan_code
* @return array
*/
public function fetchPlan($plan_code){
public function fetchPlan($plan_code)
{
$this->setRequestOptions();
return $this->setHttpResponse('/plan/' . $plan_code, 'GET', [])->getResponse();
}
Expand All @@ -308,7 +322,8 @@ public function fetchPlan($plan_code){
* @param $plan_code
* @return array
*/
public function updatePlan($plan_code){
public function updatePlan($plan_code)
{
$data = [
"name" => request()->name,
"description" => request()->desc,
Expand All @@ -325,9 +340,9 @@ public function updatePlan($plan_code){

/**
* Create a customer
* @return array
*/
public function createCustomer(){
public function createCustomer()
{
$data = [
"email" => request()->email,
"first_name" => request()->fname,
Expand Down Expand Up @@ -357,7 +372,8 @@ public function fetchCustomer($customer_id)
* @param $customer_id
* @return array
*/
public function updateCustomer($customer_id){
public function updateCustomer($customer_id)
{
$data = [
"email" => request()->email,
"first_name" => request()->fname,
Expand All @@ -372,10 +388,11 @@ public function updateCustomer($customer_id){
}

/**
* Export tranactions in .CSV
* Export transactions in .CSV
* @return array
*/
public function exportTransactions(){
public function exportTransactions()
{
$data = [
"from" => request()->from,
"to" => request()->to,
Expand All @@ -388,9 +405,9 @@ public function exportTransactions(){

/**
* Create a subscription to a plan from a customer.
* @return array
*/
public function createSubscription(){
public function createSubscription()
{
$data = [
"customer" => request()->customer, //Customer email or code
"plan" => request()->plan,
Expand All @@ -405,7 +422,8 @@ public function createSubscription(){
* Enable a subscription using the subscription code and token
* @return array
*/
public function enableSubscription(){
public function enableSubscription()
{
$data = [
"code" => request()->code,
"token" => request()->token,
Expand All @@ -419,7 +437,8 @@ public function enableSubscription(){
* Disable a subscription using the subscription code and token
* @return array
*/
public function disableSubscription(){
public function disableSubscription()
{
$data = [
"code" => request()->code,
"token" => request()->token,
Expand All @@ -431,7 +450,7 @@ public function disableSubscription(){

/**
* Fetch details about a certain subscription
* @param $subscription_id
* @param mixed $subscription_id
* @return array
*/
public function fetchSubscription($subscription_id)
Expand All @@ -442,9 +461,9 @@ public function fetchSubscription($subscription_id)

/**
* Create pages you can share with users using the returned slug
* @return array
*/
public function createPage(){
public function createPage()
{
$data = [
"name" => request()->name,
"description" => request()->description,
Expand All @@ -467,7 +486,7 @@ public function getAllPages()

/**
* Fetch details about a certain page using its id or slug
* @param $page_id
* @param mixed $page_id
* @return array
*/
public function fetchPage($page_id)
Expand All @@ -481,7 +500,8 @@ public function fetchPage($page_id)
* @param $page_id
* @return array
*/
public function updatePage($page_id){
public function updatePage($page_id)
{
$data = [
"name" => request()->name,
"description" => request()->description,
Expand All @@ -491,8 +511,4 @@ public function updatePage($page_id){
$this->setRequestOptions();
return $this->setHttpResponse('/page/'.$page_id, 'PUT', $data)->getResponse();
}

}



Loading