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 = ''; $form .= '
'; foreach ($this->postFields as $key => $value) { $form .= ''; } $form .= '
'; $form .= ''; 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; } }