Skip to content

Commit 22ee162

Browse files
author
Prosper Otemuyiwa
authored
Merge pull request #21 from imyque/master
Updated src/Paystack.php
2 parents 27ec34e + acc5273 commit 22ee162

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

src/Paystack.php

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,36 +102,40 @@ private function setRequestOptions()
102102

103103
/**
104104
* Initiate a payment request to Paystack
105+
* Included the option to pass the payload to this method for situations
106+
* when the payload is built on the fly (not passed to the controller from a view)
105107
* @return Paystack
106108
*/
107-
public function makePaymentRequest()
108-
{
109-
$data = [
110-
"amount" => intval(request()->amount),
111-
"reference" => request()->reference,
112-
"email" => request()->email,
113-
"plan" => request()->plan,
114-
"first_name" => request()->first_name,
115-
"last_name" => request()->last_name,
116-
"callback_url" => request()->callback_url,
117-
/*
118-
* to allow use of metadata on Paystack dashboard and a means to return additional data back to redirect url
119-
* form need an input field: <input type="hidden" name="metadata" value="{{ json_encode($array) }}" >
120-
*array must be set up as: $array = [ 'custom_fields' => [
121-
* ['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"],
122-
* ['display_name' => "Sex", "variable_name" => "sex", "value" => "female"],
123-
* .
124-
* .
125-
* .
126-
* ]
127-
*
128-
* ]
129-
*/
130-
'metadata' => request()->metadata
131-
];
132-
133-
// Remove the fields which were not sent (value would be null)
134-
array_filter($data);
109+
public function makePaymentRequest( $data = null)
110+
{
111+
if ( $data == null ) {
112+
$data = [
113+
"amount" => intval(request()->amount),
114+
"reference" => request()->reference,
115+
"email" => request()->email,
116+
"plan" => request()->plan,
117+
"first_name" => request()->first_name,
118+
"last_name" => request()->last_name,
119+
"callback_url" => request()->callback_url,
120+
/*
121+
* to allow use of metadata on Paystack dashboard and a means to return additional data back to redirect url
122+
* form need an input field: <input type="hidden" name="metadata" value="{{ json_encode($array) }}" >
123+
*array must be set up as: $array = [ 'custom_fields' => [
124+
* ['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"],
125+
* ['display_name' => "Sex", "variable_name" => "sex", "value" => "female"],
126+
* .
127+
* .
128+
* .
129+
* ]
130+
*
131+
* ]
132+
*/
133+
'metadata' => request()->metadata
134+
];
135+
136+
// Remove the fields which were not sent (value would be null)
137+
array_filter($data);
138+
}
135139

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

@@ -172,6 +176,21 @@ public function getAuthorizationUrl()
172176

173177
return $this;
174178
}
179+
180+
/**
181+
* Get the authorization callback response
182+
* In situations where Laravel serves as an backend for a detached UI, the api cannot redirect
183+
* and might need to take different actions based on the success or not of the transaction
184+
* @return array
185+
*/
186+
public function getAuthorizationResponse($data)
187+
{
188+
$this->makePaymentRequest($data);
189+
190+
$this->url = $this->getResponse()['data']['authorization_url'];
191+
192+
return $this->getResponse();
193+
}
175194

176195
/**
177196
* Hit Paystack Gateway to Verify that the transaction is valid

0 commit comments

Comments
 (0)