Skip to content

Updated src/Paystack.php #21

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
Aug 24, 2017
Merged
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
75 changes: 47 additions & 28 deletions src/Paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,40 @@ 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 = [
"amount" => intval(request()->amount),
"reference" => request()->reference,
"email" => request()->email,
"plan" => request()->plan,
"first_name" => request()->first_name,
"last_name" => request()->last_name,
"callback_url" => request()->callback_url,
/*
* to allow use of metadata on Paystack dashboard and a means to return additional data back to redirect url
* form need an input field: <input type="hidden" name="metadata" value="{{ json_encode($array) }}" >
*array must be set up as: $array = [ 'custom_fields' => [
* ['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"],
* ['display_name' => "Sex", "variable_name" => "sex", "value" => "female"],
* .
* .
* .
* ]
*
* ]
*/
'metadata' => request()->metadata
];

// Remove the fields which were not sent (value would be null)
array_filter($data);
public function makePaymentRequest( $data = null)
{
if ( $data == null ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make use of the is_null method here like:

if (is_null($data)){...

$data = [
"amount" => intval(request()->amount),
"reference" => request()->reference,
"email" => request()->email,
"plan" => request()->plan,
"first_name" => request()->first_name,
"last_name" => request()->last_name,
"callback_url" => request()->callback_url,
/*
* to allow use of metadata on Paystack dashboard and a means to return additional data back to redirect url
* form need an input field: <input type="hidden" name="metadata" value="{{ json_encode($array) }}" >
*array must be set up as: $array = [ 'custom_fields' => [
* ['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"],
* ['display_name' => "Sex", "variable_name" => "sex", "value" => "female"],
* .
* .
* .
* ]
*
* ]
*/
'metadata' => request()->metadata
];

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

$this->setHttpResponse('/transaction/initialize', 'POST', $data);

Expand Down Expand Up @@ -172,6 +176,21 @@ public function getAuthorizationUrl()

return $this;
}

/**
* Get the authorization callback response
* In situations where Laravel serves as an backend for a detached UI, the api cannot redirect
* and might need to take different actions based on the success or not of the transaction
* @return array
*/
public function getAuthorizationResponse($data)
{
$this->makePaymentRequest($data);

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

return $this->getResponse();
}

/**
* Hit Paystack Gateway to Verify that the transaction is valid
Expand Down