70 lines
2.4 KiB
PHP
70 lines
2.4 KiB
PHP
<?php
|
|
// Headersa
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Methods: POST');
|
|
// header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
|
|
|
|
include_once 'models/Auth.php';
|
|
include_once 'config/Database.php';
|
|
include_once 'models/Disbursement.php';
|
|
|
|
$database = new Database();
|
|
$db = $database->connect();
|
|
|
|
$auth = new Auth($db);
|
|
|
|
$retval = $auth->read_api_auth();
|
|
|
|
$data = json_decode(file_get_contents("php://input") , true);
|
|
|
|
@file_put_contents("logs/" . date("Y_m_d_") . "contact_centre_electricity_purchase_requests.txt", json_encode($data) . PHP_EOL, FILE_APPEND);
|
|
|
|
|
|
if ($retval == false) {
|
|
http_response_code(401);
|
|
echo json_encode(["status" => "fail", "message" => "Unauthorised Access"]);
|
|
exit();
|
|
}
|
|
$requested_keys = ['transaction_id', 'reference_id', 'mobile_number', 'amount'];
|
|
$missing = [];
|
|
|
|
$missing = array_diff_key(array_flip($requested_keys), $data);
|
|
if (count($missing) > 0) {
|
|
$missing_string = implode(", ", array_flip($missing));
|
|
http_response_code(400);
|
|
echo json_encode(["status" => "fail", "message" => "Required parameter(s) missing : $missing_string"]);
|
|
exit();
|
|
}
|
|
|
|
// var_dump($missing);
|
|
// var_dump($data); die;
|
|
|
|
// TODO: check if transaction ID matches our record in DB
|
|
|
|
$result = varifyTransaction($data['transaction_id'], $data['mobile_number']);
|
|
// var_dump($result);
|
|
if ($result == false) {
|
|
http_response_code(200);
|
|
echo json_encode(["status" => "fail", "message" => "Transaction ID not found"]);
|
|
exit();
|
|
}
|
|
|
|
if($result !== false) {
|
|
// TODO: send request to ultima to purchase electricty
|
|
$result = processDisbursement($data['transaction_id'], $data['mobile_number'], $data['amount'], $data['reference_id']);
|
|
if ($electricity_token !== false) {
|
|
http_response_code(200);
|
|
echo json_encode(["status" => "success", "reference_id" => $data['reference_id']]);
|
|
exit();
|
|
}
|
|
http_response_code(200);
|
|
echo json_encode(["status" => "fail", "reference_id" => $data['reference_id'], "message" => "Disbursement could not be processed at this time"]);
|
|
exit();
|
|
}
|
|
else {
|
|
http_response_code(200);
|
|
echo json_encode(["status" => "fail", "reference_id" => $data['reference_id'], "message" => "Disbursement failed"]);
|
|
exit();
|
|
}
|