Skip to content
Open
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
},
Expand Down
21 changes: 17 additions & 4 deletions examples/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file added examples/callback.txt
Empty file.
48 changes: 48 additions & 0 deletions examples/checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* An example of using the Redde Api to receive money
*/
use Redde\ReddeApi;

require_once '../vendor/autoload.php';
require_once "common_functions.php";

//Configuration file contains apikey and appid
$config = include '../src/config.php';

// Replace this with your API key and App Id
$apikey = $config['apikey'];
$appid = $config['appid'];

$api = new ReddeApi($apikey, $appid);

/* Note that the clienttransid and clientreference is generated
by the developer. The nickname is your identity name eg. Wigal
*/
$clientreference = generateNumber();
$clienttransid = generateRandomString();

$params = [
"amount" => 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);
32 changes: 32 additions & 0 deletions examples/checkout_status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* An example of using the Redde Api to receive money
*/

use Redde\ReddeApi;
use Redde\Exceptions\ReddeApiException;
use Redde\Exceptions\ReddeException;

require_once '../vendor/autoload.php';
require_once "common_functions.php";

// Configuration file contains apikey and appid
$config = include '../src/config.php';


// Replace this with your API key and App Id
$apikey = $config['apikey'];
$appid = $config['appid'];

$api = new ReddeApi($apikey, $appid);

$transaction_id = ''; // 19XXXX a transaction id from the api

$status_params = [
"apikey" => $apikey,
"appid" => $appid,
];

// To check status of checkout call this function
$api->checkoutStatus($transaction_id, $status_params);
Empty file added examples/gitkeep
Empty file.
19 changes: 19 additions & 0 deletions examples/otpcode-example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

include('otpcode.php');

$response = json_encode([
'status' => '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;
41 changes: 41 additions & 0 deletions examples/otpcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

// Put the functions where you can access them
// Example otpcode.php
// and include it somewhere in your code
// e.g. include('otpcode.php')

/**
* Returns true if $reason
* contains wigal otp code
*
* @param string $reason
* @param string $wigalOtpCode
* @return mixed
*/
function containsOtp($reason, $wigalOtpCode = '*800*')
{
return (bool) strpos($reason, $wigalOtpCode) !== false;
}


function getReason($response)
{
return json_decode($response)->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);
1 change: 1 addition & 0 deletions src/Exceptions/ReddeApiException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace Redde\Exceptions;

/**
* Redde exception returned from the API
*/
Expand Down
131 changes: 122 additions & 9 deletions src/ReddeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class ReddeApi
*/
private $apikey = '';

private $app_id = '';
private $appid = '';

private $lastResponseRaw;

private $lastResponse;

public $url = 'https://api.reddeonline.com/v1/';

const USER_AGENT = 'Redde PHP API SDK 1.0';
const USER_AGENT = 'Redde PHP API SDK 1.2';

/**
* Maximum amount of time in seconds that is allowed to make the connection to the API server
Expand All @@ -39,13 +39,15 @@ class ReddeApi
public $curlTimeout = 20;

/**
* @param string $apikey Redde Store API key
* @throws \Redde\Exceptions\ReddeException if the library failed to initialize
* ReddeApi construct
*
* @param string $apikey Redde API key
* @param string $appid Redd App ID
*/
public function __construct($apikey, $app_id)
public function __construct($apikey, $appid)
{
$this->apikey = $apikey;
$this->app_id = $app_id;
$this->appid = $appid;
}

/**
Expand All @@ -55,7 +57,7 @@ public function __construct($apikey, $app_id)
*/
protected function getBaseUrl()
{
return $this->setBaseUrl();
return $this->setBaseUrl($this->url);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}

}
Loading