<?php
namespace App\Services; //modify the namespace accroding to your
use Exception;
use Illuminate\Support\Facades\Http;
class PortPos
{
private $_appKey = "";
private $_secretKey = "";
public $mode = "sandbox";
private $config_data = [
'sandbox' => [
'invoice_endpoint' => 'https://api-sandbox.portpos.com/payment/v2/invoice/',
'ipn_endpoint' => 'https://api-sandbox.portpos.com/payment/v2/invoice/ipn/',
],
'production' => [
'invoice_endpoint' => 'https://api.portpos.com/payment/v2/invoice/',
'ipn_endpoint' => 'https://api.portpos.com/payment/v2/invoice/ipn/',
],
];
protected $_params = array();
public function __construct($appKey = null, $secretKey = null, $mode = 'sandbox')
{
if (!empty($appKey)) {
$this->_setAppKey($appKey);
}
if (!empty($secretKey)) {
$this->_setSecretKey($secretKey);
}
$this->setMode($mode);
}
public function generateInvoice($data)
{
// $data = json_encode($data);
$this->_params = $data;
$url = $this->config_data[$this->mode]['invoice_endpoint'];
return $this->_process("post", $url);
}
public function ipnValidate($invoiceId,$amount)
{
$this->_params = [];
$url = $this->config_data[$this->mode]['ipn_endpoint'] . $invoiceId . '/' . $amount;
return $this->_process("post", $url);
}
public function getInvoice($invoiceId)
{
$url = $this->config_data[$this->mode]['invoice_endpoint'] . $invoiceId;
return $this->_process("get", $url);
}
public function setMode($mode)
{
$this->mode = $mode;
}
protected function _setAppKey($appKey)
{
$this->_appKey = $appKey;
}
protected function _setSecretKey($secretKey)
{
$this->_secretKey = $secretKey;
}
private function _process($method = "get", $url = null)
{
$headers['Authorization'] = 'Bearer ' . base64_encode($this->_appKey . ":" . md5($this->_secretKey . time()));
$headers['Content-Type'] = 'application/json';
return $this->_request($method, $headers, $url);
}
private function _request($method, $headers = [], $url = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($method == "post") {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->_params));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// dump($response);
$info = curl_getinfo($ch);
curl_close($ch);