From acc5273253eb460ec0b0dd2caf327407c42f7c73 Mon Sep 17 00:00:00 2001 From: imyque Date: Thu, 24 Aug 2017 17:35:38 +0100 Subject: [PATCH] Updated Paystack.php 1. UPDATE: The makePaymentRequest function builds the payload from the request, assuming the request as the only source of the payload. In a situation when Laravel is used strictly as an API backend, there are situations where the payload needs to be built dynamically, say from within the controller and not from a view or request. 2. NEW: Added getAuthorizationResponse function for a Laravel API scenario, where the controller may want take a different course of action if the initialization succeeded or not --- src/Paystack.php | 75 ++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/src/Paystack.php b/src/Paystack.php index 2208a39..162e2a3 100644 --- a/src/Paystack.php +++ b/src/Paystack.php @@ -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: - *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 ) { + $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: + *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); @@ -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