218 lines
6.0 KiB
PHP
218 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace app\services\epay;
|
|
|
|
class EPay
|
|
{
|
|
|
|
const API_MODE_DEMO = 'demo';
|
|
const API_MODE_LIVE = 'live';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $apiMode;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $ePayDemoEnvironmentUrl;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $ePayLiveEnvironmentUrl;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $apiClientEmail;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $authenticationDemo;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $authenticationLive;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $postFields = [];
|
|
|
|
/**
|
|
* EPayHelper set method.
|
|
* @param string $apiMode
|
|
* @param string $ePayDemoEnvironmentUrl
|
|
* @param string $ePayLiveEnvironmentUrl
|
|
* @param string $apiClientEmail
|
|
* @param array $authenticationDemo
|
|
* @param array $authenticationLive
|
|
*/
|
|
public function setData($apiMode, $ePayDemoEnvironmentUrl, $ePayLiveEnvironmentUrl, $apiClientEmail, array $authenticationDemo, array $authenticationLive)
|
|
{
|
|
$this->apiMode = $apiMode;
|
|
$this->ePayDemoEnvironmentUrl = $ePayDemoEnvironmentUrl;
|
|
$this->ePayLiveEnvironmentUrl = $ePayLiveEnvironmentUrl;
|
|
$this->apiClientEmail = $apiClientEmail;
|
|
$this->authenticationDemo = $authenticationDemo;
|
|
$this->authenticationLive = $authenticationLive;
|
|
}
|
|
|
|
public function getEPayUrl()
|
|
{
|
|
if ($this->apiMode == self::API_MODE_DEMO) {
|
|
return $this->ePayDemoEnvironmentUrl;
|
|
} else {
|
|
return $this->ePayLiveEnvironmentUrl;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param EPayPayment $ePayPayment
|
|
* @param bool $directCard
|
|
* @param array $order
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public function prepareEPayPaymentFormFields($ePayPayment, $directCard = false)
|
|
{
|
|
$now = new \DateTime("+5 min");
|
|
|
|
$dataFields = [];
|
|
$postFields = [];
|
|
|
|
$dataFields['MIN'] = $this->getApiClientId();
|
|
$dataFields['INVOICE'] = (string)$ePayPayment->getInvoiceNumber();
|
|
|
|
$amount = (float)$ePayPayment->getAmount();
|
|
$paymentAmount = $amount;
|
|
$dataFields['AMOUNT'] = number_format($paymentAmount, 2);
|
|
$dataFields['CURRENCY'] = 'BGN';
|
|
$dataFields['EXP_TIME'] = $now->format('d.m.Y H:i:s');
|
|
$dataFields['DESCR'] = $ePayPayment->getDescription();
|
|
$dataFields['ENCODING'] = 'utf-8';
|
|
$dataFields['ORDERID'] = $ePayPayment->getOrderId();
|
|
|
|
$data = $this->getDataFieldsAsString($dataFields);
|
|
$encodedData = base64_encode($data);
|
|
//$checksum = $this->getHash($encodedData);
|
|
$checksum = $this->generateChecksum($encodedData);
|
|
|
|
if (!$directCard) {
|
|
$postFields['PAGE'] = 'paylogin';
|
|
} else {
|
|
$postFields['PAGE'] = 'credit_paydirect';
|
|
}
|
|
if($directCard) {
|
|
$postFields['LANG'] = 'bg';
|
|
}
|
|
$postFields['ENCODED'] = $encodedData;
|
|
$postFields['CHECKSUM'] = $checksum;
|
|
$postFields['URL_OK'] = $ePayPayment->getUrlOk();
|
|
$postFields['URL_CANCEL'] = $ePayPayment->getUrlCancel();
|
|
|
|
$this->postFields = $postFields;
|
|
}
|
|
|
|
public function postSubmit()
|
|
{
|
|
//echo json_encode($this->postFields);
|
|
//exit;
|
|
if (sizeof($this->postFields) > 0) {
|
|
$form = '<html><body onload="document.forms[\'ePay\'].submit()">';
|
|
$form .= '<form action="' . $this->getEPayUrl() . '" name="ePay" method="post">';
|
|
foreach ($this->postFields as $key => $value) {
|
|
$form .= '<input type="hidden" name="' . $key . '" value="' . $value . '">';
|
|
}
|
|
$form .= '</form>';
|
|
$form .= '</body></html>';
|
|
echo $form;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $dataFields
|
|
* @return string
|
|
*/
|
|
private function getEncodedDataFieldsAsString($dataFields)
|
|
{
|
|
return JWT::encode($dataFields, JWT::SECRET_KEY);
|
|
}
|
|
|
|
private function getDataFieldsAsString($dataFields)
|
|
{
|
|
$data = '';
|
|
foreach ($dataFields as $key => $value) {
|
|
$data .= "$key=$value" . PHP_EOL;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param string $data
|
|
* @return string
|
|
*/
|
|
private function generateChecksum($data)
|
|
{
|
|
$algorithm = 'sha1';
|
|
$password = $this->getApiClientSecret();
|
|
|
|
$p = ['md5' => 'H32', 'sha1' => 'H40'];
|
|
if (strlen($password) > 64) {
|
|
$password = pack($p[$algorithm], $algorithm($password));
|
|
}
|
|
if (strlen($password) < 64) {
|
|
$password = str_pad($password, 64, chr(0));
|
|
}
|
|
|
|
$iPad = substr($password, 0, 64) ^ str_repeat(chr(0x36), 64);
|
|
$oPad = substr($password, 0, 64) ^ str_repeat(chr(0x5C), 64);
|
|
|
|
return ($algorithm($oPad . pack($p[$algorithm], $algorithm($iPad . $data))));
|
|
}
|
|
|
|
private function getHash($data) {
|
|
$algorithm = 'sha1';
|
|
$password = $this->getApiClientSecret();
|
|
return hash_hmac($algorithm, $data, $password);
|
|
}
|
|
|
|
private function getApiClientSecret()
|
|
{
|
|
if ($this->apiMode == self::API_MODE_DEMO) {
|
|
return $this->authenticationDemo['client_secret'];
|
|
} else {
|
|
return $this->authenticationLive['client_secret'];
|
|
}
|
|
}
|
|
|
|
private function getApiClientId()
|
|
{
|
|
if ($this->apiMode == self::API_MODE_DEMO) {
|
|
return $this->authenticationDemo['client_identifier'];
|
|
} else {
|
|
return $this->authenticationLive['client_identifier'];
|
|
}
|
|
}
|
|
|
|
public function getOrder($encoded)
|
|
{
|
|
$decodeArray = explode(':', base64_decode($encoded));
|
|
$responseData = [];
|
|
foreach ($decodeArray as $keyValueStr) {
|
|
$keyValueArray = explode('=', $keyValueStr);
|
|
if (sizeof($keyValueArray) == 2)
|
|
$responseData[$keyValueArray[0]] = $keyValueArray[1];
|
|
}
|
|
if (isset($responseData['INVOICE']) && isset($responseData['STATUS'])) {
|
|
return $responseData;
|
|
}
|
|
return null;
|
|
|
|
}
|
|
}
|