unioil-mobile-api/app/Libraries/RNRPHLibrary.php

134 lines
3.2 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: root
* Date: 10/18/18
* Time: 10:45 AM
*/
namespace App\Libraries;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Support\Facades\Config;
class RNRPHLibrary
{
public function __construct() {
$this->url = Config::get('app.rnrph');
$this->paymaya_url = Config::get('app.paymayaenv');
$this->paymaya_api_key = Config::get('app.paymayaapikey');
$this->paymaya_secret_key = Config::get('app.paymayasecretkey');
$this->uri = new Uri($this->url);
$this->payamaya_uri = new Uri($this->paymaya_url);
}
protected $uri;
protected $url;
protected $paymaya_uri;
protected $paymaya_url;
public $response;
public function getAllProvinces() {
$this->create_request("provinces/mobile/provinces");
}
public function getAllBranches() {
$this->create_request("branches/mobile/branches");
}
public function getAllStations() {
$this->create_request("stations/mobile/stations");
}
public function getAllFuels() {
$this->create_request("fuels/mobile/fuels");
}
public function getStationDetails($station_code) {
$this->create_request("stations/mobile/stations/".$station_code);
}
public function getPaymayaCustomerDetails($customer_id) {
$this->create_paymaya_request("payments/v1/customers/".$customer_id."/cards");
}
public function create_request($params)
{
$client = new Client();
$request = $client->request('GET', $this->url.$params);
switch ($params) {
case '':
$indata = true;
break;
default:
$indata = false;
break;
}
$this->setResponse($request,$indata);
}
public function create_paymaya_request($params)
{
$client = new Client();
$token = base64_encode($this->paymaya_secret_key);
$request = $client->request('GET', $this->paymaya_url.$params,[
'headers' => [
'Authorization' => 'Basic '.$token,
'Accept' => 'application/json',
]
]);
switch ($params) {
case '':
$indata = true;
break;
default:
$indata = false;
break;
}
$this->setResponse($request,$indata);
}
/**
* @return $response
*/
public function getResponse()
{
return $this->response;
}
/**
* @param $response
*/
function setResponse($response , $indata)
{
$result = $response->getStatusCode() != 200 ? null : json_decode($response->getBody()->getContents());
$return = [];
$return['data'] = $result;
if ($indata){
$return['data'] = $result[0];
}
// if (count($result) == 1){
// $return['data'] = $result[0];
// }
$return['status_code'] = $response->getStatusCode();
$this->response = $return;
if ($indata){
$return['data'] = $result[0][0];
$this->response = $return;
}
}
}