src/Bank/Engine/MyRestDriver.php
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Zarinpal\Drivers\DriverInterface;
class Bank_Engine_MyRestDriver implements DriverInterface
{
protected $baseUrl = 'https://www.zarinpal.com/pg/rest/WebGate/';
/**
* request driver.
*
* @param $inputs
*
* @return array
*/
public function request($inputs)
{
$result = $this->restCall('PaymentRequest.json', $inputs);
if ($result['Status'] == 100 || $result['Status'] == 101) {
return ['Authority' => $result['Authority']];
} else {
return ['error' => $result['Status']];
}
}
/**
* requestWithExtra driver.
*
* @param $inputs
*
* @return array
*/
public function requestWithExtra($inputs)
{
$result = $this->restCall('PaymentRequestWithExtra.json', $inputs);
if ($result['Status'] == 100 || $result['Status'] == 101) {
return ['Authority' => $result['Authority']];
} else {
return ['error' => $result['Status']];
}
}
/**
* verify driver.
*
* @param $inputs
*
* @return array
*/
public function verify($inputs)
{
$result = $this->restCall('PaymentVerification.json', $inputs);
if ($result['Status'] == 100 || $result['Status'] == 101) {
return ['Status' => 'success', 'RefID' => $result['RefID']];
} else {
return ['Status' => 'error', 'error' => $result['Status']];
}
}
/**
* verifyWithExtra driver.
*
* @param $inputs
*
* @return array
*/
public function verifyWithExtra($inputs)
{
$result = $this->restCall('PaymentVerificationWithExtra.json', $inputs);
if ($result['Status'] == 100 || $result['Status'] == 101) {
return ['Status' => 'success', 'RefID' => $result['RefID']];
} else {
return ['Status' => 'error', 'error' => $result['Status']];
}
}
/**
* unverifiedTransactions driver.
*
* @param $inputs
*
* @return array
*/
public function unverifiedTransactions($inputs)
{
$result = $this->restCall('UnverifiedTransactions.json', $inputs);
if ($result['Status'] == 100 || $result['Status'] == 101) {
return ['Status' => 'success', 'Authorities' => $result['Authorities']];
} else {
return ['Status' => 'error', 'error' => $result['Status']];
}
}
/**
* refreshAuthority driver.
*
* @param $inputs
*
* @return array
*/
public function refreshAuthority($inputs)
{
$result = $this->restCall('RefreshAuthority.json', $inputs);
if ($result['Status'] == 100 || $result['Status'] == 101) {
return ['Status' => 'success', 'refreshed' => true];
} else {
return ['Status' => 'error', 'error' => $result['Status']];
}
}
/**
* request rest and return the response.
*
* @param $uri
* @param $data
*
* @return mixed
*/
private function restCall($uri, $data)
{
try {
$client = new Client(['base_uri' => $this->baseUrl]);
$response = $client->request('POST', $uri, ['json' => $data]);
$rawBody = $response->getBody()->getContents();
$body = json_decode($rawBody, true);
} catch (RequestException $e) {
$response = $e->getResponse();
$rawBody = is_null($response) ? '{"Status":-98,"message":"http connection error"}' : $response->getBody()->getContents();
$body = json_decode($rawBody, true);
}
if (!isset($result['Status'])) {
$result['Status'] = -99;
}
return $body;
}
/**
* @param mixed $baseUrl
* @return void
*/
public function setAddress($baseUrl)
{
$this->baseUrl = $baseUrl;
}
/**
* activate sandbox mod for dev environment.
*/
public function enableSandbox(){
return false;
}
}