Initial import
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
<?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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\epay;
|
||||
|
||||
class EPayPayment
|
||||
{
|
||||
|
||||
/**
|
||||
* This is int.
|
||||
* Cast it to string when sending data to EPay!
|
||||
* @var int
|
||||
*/
|
||||
private $invoiceNumber;
|
||||
|
||||
|
||||
private $orderId;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $amount;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $currency;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $encoding = 'utf-8';
|
||||
|
||||
/**
|
||||
* URL where the client should be redirected
|
||||
* when payment is processed successfully.
|
||||
* @var string
|
||||
*/
|
||||
private $urlOk;
|
||||
|
||||
/**
|
||||
* URL where the client should be redirected
|
||||
* in case they rejected payment.
|
||||
* @var string
|
||||
*/
|
||||
private $urlCancel;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getInvoiceNumber()
|
||||
{
|
||||
return $this->invoiceNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $invoiceNumber
|
||||
*/
|
||||
public function setInvoiceNumber($invoiceNumber)
|
||||
{
|
||||
$this->invoiceNumber = $invoiceNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getAmount()
|
||||
{
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
*/
|
||||
public function setAmount($amount)
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrency()
|
||||
{
|
||||
return $this->currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $currency
|
||||
*/
|
||||
public function setCurrency($currency)
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*/
|
||||
public function setDescription($description)
|
||||
{
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncoding()
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $encoding
|
||||
*/
|
||||
public function setEncoding($encoding)
|
||||
{
|
||||
$this->encoding = $encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlOk()
|
||||
{
|
||||
return $this->urlOk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $urlOk
|
||||
*/
|
||||
public function setUrlOk($urlOk)
|
||||
{
|
||||
$this->urlOk = $urlOk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlCancel()
|
||||
{
|
||||
return $this->urlCancel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $urlCancel
|
||||
*/
|
||||
public function setUrlCancel($urlCancel)
|
||||
{
|
||||
$this->urlCancel = $urlCancel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOrderId()
|
||||
{
|
||||
return $this->orderId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $orderId
|
||||
*/
|
||||
public function setOrderId($orderId)
|
||||
{
|
||||
$this->orderId = $orderId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user