diff --git a/composer.json b/composer.json index a28f013..ed72b8e 100644 --- a/composer.json +++ b/composer.json @@ -2,18 +2,18 @@ "name": "redde/php-api-sdk", "description": "Redde API wrapper", "type": "library", - "keywords": ["online payment","payment integration","wigal","wigal solutions", "mobile money"], + "keywords": ["redde","redde payments","online payment","payment integration","wigal","wigal solutions", "wigal vision", "mobile money", "card payments"], "homepage": "https://www.reddeonline.com/", "license": "MIT", "authors": [ { "name": "Oteng Kwame Appiah-Nti", - "email": "koteng@wigalsolutions.com", + "email": "koteng@wigal.com.gh", "role": "Software Engineer" } ], "require": { - "php": ">=5.5.0", + "php": "^7.4", "ext-curl": "*", "ext-json": "*" }, diff --git a/examples/callback.php b/examples/callback.php index c3a524a..9a17bb6 100755 --- a/examples/callback.php +++ b/examples/callback.php @@ -3,13 +3,26 @@ * A simple implementation of callback * on Redde Api */ -require_once '../vendor/autoload.php'; use Redde\Exceptions\ReddeApiException; use Redde\Exceptions\ReddeException; use Redde\Webhooks\WebHookStatus; -$status = new WebHookStatus(); //instantiate an object +require_once '../vendor/autoload.php'; + +$status = new WebHookStatus(); // instantiate an object + +$data = $status->callback(); // get callback and set it to a variable + +echo $data->reason; // output any data e.g. status|reason| etc + +/** + * Another implementation can be seen below + * This is to show how to log in a text file + */ + +// $data = $status->getRawResponse(); // get raw response and use it how you want -$data = $status->callback(); //get callback and set it to a variable +// if($data != null) { +// file_put_contents('callback.txt', "Log: ".date('d-m-Y')." => " . $params, FILE_APPEND | LOCK_EX); +// } -echo $data->reason; //output any data e.g. status|reason| etc diff --git a/examples/callback.txt b/examples/callback.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/checkout.php b/examples/checkout.php new file mode 100644 index 0000000..e0bcf66 --- /dev/null +++ b/examples/checkout.php @@ -0,0 +1,48 @@ + 1, // amount to receive + "apikey" => $apikey, // your api key + "appid" => $appid, // your app id + "description" => "A description for your transaction", // A description for the transaction + "merchantname" => "Some Company Limited", // Merchantname (Your Company or A name of your application) + "logolink" => "https://example.com/some-path-to-image-logo.jpg", // Url to fetch for your logo + "clienttransid" => $clientreference, // client reference from your end + "successcallback" => "https://example.com/", // A success callback url from your end + "failurecallback" => "https://example.com/failure", // A failure callback url from your end +]; + +/** +* Call checkOut function and pass +* the payload as parameter. This will +* take you to the Redde Checkout page +* when successful or throw an error +* when something goes wrong +* +* On success the success callbackurl will be called +* On failure the failure callbackurl will be called +*/ +$api->checkOut($params); diff --git a/examples/checkout_status.php b/examples/checkout_status.php new file mode 100644 index 0000000..33a3e7b --- /dev/null +++ b/examples/checkout_status.php @@ -0,0 +1,32 @@ + $apikey, + "appid" => $appid, +]; + +// To check status of checkout call this function +$api->checkoutStatus($transaction_id, $status_params); \ No newline at end of file diff --git a/examples/gitkeep b/examples/gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/examples/otpcode-example.php b/examples/otpcode-example.php new file mode 100644 index 0000000..d5c9a3e --- /dev/null +++ b/examples/otpcode-example.php @@ -0,0 +1,19 @@ + 'OK', + 'reason' => 'To Receive Payment Prompt Dial *800*0*3072#', + 'transactionid' => 2553918, + 'clienttransid' => 345655, + 'clientreference' => 754876, + 'statusdate' => '2022-03-02 11:59:17.779', + 'brandtransid' => null +]); + +$reason = getReason($response); + +$output = getOtpUssd($reason); + +echo $otpCode = $output->notice .' '.$output->otp_code; diff --git a/examples/otpcode.php b/examples/otpcode.php new file mode 100644 index 0000000..28bac0a --- /dev/null +++ b/examples/otpcode.php @@ -0,0 +1,41 @@ +reason; +} + +function getOtpUssd($string) +{ + $strpos = strpos($string, '*'); + $first_star = substr($string, 0, $strpos); + $otp_code = substr($string, ($strpos)); + + return (object) [ + 'notice' => $first_star, + 'otp_code' => $otp_code + ]; +} + +// Put this code where you want to display otpcode to user $reason variable +$reason = getReason($response); +$output = getOtpUssd($reason); diff --git a/src/Exceptions/ReddeApiException.php b/src/Exceptions/ReddeApiException.php index 42d74df..4521666 100644 --- a/src/Exceptions/ReddeApiException.php +++ b/src/Exceptions/ReddeApiException.php @@ -1,5 +1,6 @@ apikey = $apikey; - $this->app_id = $app_id; + $this->appid = $appid; } /** @@ -55,7 +57,7 @@ public function __construct($apikey, $app_id) */ protected function getBaseUrl() { - return $this->setBaseUrl(); + return $this->setBaseUrl($this->url); } /** @@ -141,7 +143,7 @@ private function request($method, $path, array $params = [], $data = null) curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'apikey: ' . $this->apikey, - 'appid: ' . $this->app_id, + 'appid: ' . $this->appid, ]); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); @@ -193,7 +195,6 @@ private function request($method, $path, array $params = [], $data = null) */ public function receiveMoney($params) { - //$this->api = new ReddeApi($this->apikey, $this->app_id); try { // Receive payment from Customer $transaction = $this->post('receive', $params); @@ -231,5 +232,117 @@ public function sendMoney($params) echo $this->getLastResponseRaw(); } } + + /** + * Perform a status request to the API + * To check the status of a transaction api initiated through sendMoney or receiveMoney + * @param $transaction_id int|string + * @return mixed API response + * @throws \Redde\Exceptions\ReddeApiException If the API call status code is not in the 2xx range + * @throws ReddeException If the API call has failed or the response is invalid + */ + public function transactionStatus($transaction_id) + { + + $status_id = $transaction_id; //use the exact transaction_id here + + try { + // Checkout Status from Customer + $status = $this->get('status/' . $status_id); + echo $status; + } catch (ReddeApiException $e) { + // API response status code was not successful + echo 'Redde API Exception: ' . $e->getCode() . ' ' . $e->getMessage(); + } catch (ReddeException $e) { + // API call failed + echo 'Redde Exception: ' . $e->getMessage(); + echo $this->getLastResponseRaw(); + } + } + + /** + * Perform a checkout request to the API + * To receive money from customer through Checkout + * @param array $checkout_params + * @return mixed API response + * + * If the API call status code is not in the 2xx range + * and API call fails it will return to failure callback url + */ + public function checkOut($checkout_params) { + + $checkouturl = ''; + + try { + + // Checkout from Customer + $transaction = $this->post('checkout', $checkout_params); + $transaction = json_decode($transaction); + $checkouturl = $transaction->checkouturl; + + if ( + defined('LARAVEL_START') + && function_exists('redirect') + ) { + + return @redirect()->to($checkouturl); + } + + header("Location: " . $checkouturl); + + } catch (ReddeApiException $e) { + + // API response status code was not successful + if ( + defined('LARAVEL_START') + && function_exists('redirect') + ) { + return @redirect()->to($checkout_params['failurecallback']); + } else { + // die(var_dump($e)); + // echo 'Redde API Exception: ' . $e->getCode() . ' ' . $e->getMessage(); + header("Location: " . $checkout_params['failurecallback']); + } + } catch (ReddeException $e) { + + // API call failed + if ( + defined('LARAVEL_START') + && function_exists('redirect') + ) { + // die(var_dump($e)); + return @redirect()->to($checkout_params['failurecallback']); + } else { + header("Location: " . $checkout_params['failurecallback']); + } + } + } + + /** + * Perform a checkout status request to the API + * To check the status of a checkout api initiated through Checkout + * @param $transaction_id int|string + * @return mixed API response + * @throws \Redde\Exceptions\ReddeApiException If the API call status code is not in the 2xx range + * @throws ReddeException If the API call has failed or the response is invalid + */ + public function checkoutStatus($transaction_id) + { + + $checkout_status_id = $transaction_id; //use the exact transaction_id here + + try { + // Checkout Status from Customer + $status = $this->get('checkoutstatus/' . $checkout_status_id); + echo $status; + } catch (ReddeApiException $e) { + //API response status code was not successful + echo 'Redde API Exception: ' . $e->getCode() . ' ' . $e->getMessage(); + } catch (ReddeException $e) { + //API call failed + echo 'Redde Exception: ' . $e->getMessage(); + echo $this->getLastResponseRaw(); + } + } } \ No newline at end of file diff --git a/src/Webhooks/WebHookStatus.php b/src/Webhooks/WebHookStatus.php index a4361f1..d943ea9 100644 --- a/src/Webhooks/WebHookStatus.php +++ b/src/Webhooks/WebHookStatus.php @@ -1,8 +1,6 @@ setResponse($data); + $this->setResponse($data); return $this->getResponse(); } /** * The callback function prepares and retrieve - * final status of api call - * @return json + * response as object of final api call + * @return object */ public function callback() { - return json_decode($this->getRawResponse()); + $this->getRawResponse(); + return json_decode($this->response); } - + } diff --git a/tests/gitkeep b/tests/gitkeep new file mode 100644 index 0000000..e69de29