'https://api.imoje.pl/v1', Util::ENVIRONMENT_SANDBOX => 'https://sandbox.api.imoje.pl/v1', ]; /** * @var string */ private $authorizationToken; /** * @var string */ private $merchantId; /** * @var string */ private $serviceId; /** * @var string */ private $environment; /** * Api constructor. * * @param string $authorizationToken * @param string $merchantId * @param string $serviceId * @param string $environment */ public function __construct($authorizationToken, $merchantId, $serviceId, $environment = '') { $this->authorizationToken = $authorizationToken; $this->merchantId = $merchantId; $this->serviceId = $serviceId; if (!$environment) { $environment = Util::ENVIRONMENT_PRODUCTION; } $this->environment = $environment; } /** * @param string $firstName * @param string $lastName * @param string $street * @param string $city * @param string $region * @param string $postalCode * @param string $countryCodeAlpha2 * * @return array */ public static function prepareAddressData( $firstName, $lastName, $street, $city, $region, $postalCode, $countryCodeAlpha2 ) { $array = [ 'firstName' => $firstName, 'lastName' => $lastName, 'street' => $street, 'city' => $city, 'postalCode' => $postalCode, 'countryCodeAlpha2' => $countryCodeAlpha2, ]; if ($region) { $array['region'] = $region; } return $array; } /** * @param array $transaction * * @return array */ public static function parseStringToArray($transaction) { $array = []; if ($transaction['action']['method'] === Util::METHOD_REQUEST_POST) { parse_str($transaction['action']['contentBodyRaw'], $array); } if ($transaction['action']['method'] === Util::METHOD_REQUEST_GET) { $urlParsed = parse_url($transaction['action']['url']); if (isset($urlParsed['query']) && $urlParsed['query']) { parse_str($urlParsed['query'], $array); } } return $array; } /** * @param string $blikProfileId * @param int $amount * @param string $currency * @param string $orderId * @param string $title * @param string $clientIp * @param string $blikKey * @param string $blikCode * * @return string */ public function prepareBlikOneclickData( $blikProfileId, $amount, $currency, $orderId, $title, $clientIp, $blikKey = '', $blikCode = '' ) { $array = [ 'serviceId' => $this->serviceId, 'blikProfileId' => $blikProfileId, 'amount' => Util::convertAmountToFractional($amount), 'currency' => $currency, 'orderId' => $orderId, 'title' => $title, 'clientIp' => $clientIp, ]; if ($blikKey) { $array['blikKey'] = $blikKey; } if ($blikCode) { $array['blikCode'] = $blikCode; } return json_encode($array); } /** * @param string $id * * @return array */ public function getTransaction($id) { return $this->call( $this->getTransactionUrl($id), Util::METHOD_REQUEST_GET ); } /** * @param int $amount * @param string $currency * @param string $orderId * @param string $paymentMethod * @param string $paymentMethodCode * @param string $successReturnUrl * @param string $failureReturnUrl * @param string $customerFirstName * @param string $customerLastName * @param string $customerEmail * @param string $type * @param string $clientIp * @param string $blikCode * @param array $address * @param string $cid * * @return string */ public function prepareData( $amount, $currency, $orderId, $paymentMethod, $paymentMethodCode, $successReturnUrl, $failureReturnUrl, $customerFirstName, $customerLastName, $customerEmail, $type = 'sale', $clientIp = '', $blikCode = '', $address = [], $cid = '' ) { if (!$clientIp) { $clientIp = $_SERVER['REMOTE_ADDR']; } $array = [ 'type' => $type, 'serviceId' => $this->serviceId, 'amount' => Util::convertAmountToFractional($amount), 'currency' => $currency, 'orderId' => (string) $orderId, 'title' => (string) $orderId, 'paymentMethod' => $paymentMethod, 'paymentMethodCode' => $paymentMethodCode, 'successReturnUrl' => $successReturnUrl, 'failureReturnUrl' => $failureReturnUrl, 'clientIp' => $clientIp, 'customer' => [ 'firstName' => $customerFirstName, 'lastName' => $customerLastName, 'email' => $customerEmail, ], ]; if ($blikCode) { $array['blikCode'] = $blikCode; } if (isset($address['billing']) && $address['billing']) { $array['billing'] = $address['billing']; } if (isset($address['shipping']) && $address['shipping']) { $array['shipping'] = $address['shipping']; } if ($cid) { $array['customer']['cid'] = (string) $cid; } return json_encode($array); } /** * @param int $amount * * @return string */ public function prepareRefundData( $amount ) { return json_encode([ 'amount' => $amount, 'serviceId' => $this->serviceId, 'type' => self::TRANSACTION_TYPE_REFUND, ]); } // endregion /** * @param string $body * * @return array */ public function createTransaction($body, $url = '', $method = '') { if (!$url) { $url = $this->getTransactionCreateUrl(); } if (!$method) { $method = Util::METHOD_REQUEST_POST; } return $this->call( $url, $method, $body ); } /** * @return array */ public function getServiceInfo() { return $this->call( $this->getServiceInfoUrl(), Util::METHOD_REQUEST_GET ); } /** * @param string $cid * * @return array */ public function getBlikProfileList($cid) { return $this->call( $this->getProfileBlikUrl($cid), Util::METHOD_REQUEST_GET ); } /** * @param string $body * * @return array */ public function createRefund($body, $transactionUuid) { return $this->call( $this->getRefundCreateUrl($transactionUuid), Util::METHOD_REQUEST_POST, $body ); } /** * Creates full form with order data. * * @param array $transaction * @param string $submitValue * @param string $submitClass * @param string $submitStyle * * @return string * @throws Exception */ public function buildOrderForm($transaction, $submitValue = '', $submitClass = '', $submitStyle = '') { if (!isset($transaction['body']['action'])) { throw new Exception(json_encode([ 'action' => 'apiBuildOrderForm', 'error' => 'Doesnt action exist', ] )); } return Util::createOrderForm( self::parseStringToArray($transaction['body']), $transaction['body']['action']['url'], $transaction['body']['action']['method'], $submitValue, $submitClass, $submitStyle ); } /** * @return string */ public function getDebitBlikProfileUrl() { $baseUrl = self::getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::TRANSACTION . '/' . self::PROFILE . '/blik'; } return ''; } /** * @return string */ public function getDeactivateBlikProfileUrl() { $baseUrl = self::getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::PROFILE . '/deactivate/blik'; } return ''; } /** * @param string $url * @param string $methodRequest * @param string $body * * @return array */ private function call($url, $methodRequest, $body = '') { $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $methodRequest); curl_setopt($curl, CURLOPT_POSTFIELDS, $body); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->authorizationToken, ]); $resultCurl = json_decode(curl_exec($curl), true); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if (($httpCode !== 200) || !$resultCurl) { $array = [ 'success' => false, 'data' => [ 'httpCode' => $httpCode, 'error' => curl_error($curl), 'body' => '', ], ]; if (isset($resultCurl['apiErrorResponse']['message']) && $resultCurl['apiErrorResponse']['message']) { $array['data']['body'] = $resultCurl['apiErrorResponse']['message']; } return $array; } if (isset($resultCurl['transaction']['statusCode']) && $resultCurl['transaction']['statusCode']) { if ($this->check_retype_code($resultCurl['transaction']['statusCode'])) { $resultCurl['code'] = 1; } if ($this->check_new_param_alias($resultCurl['transaction']['statusCode'])) { $resultCurl['newParamAlias'] = 1; } } return [ 'success' => true, 'body' => $resultCurl, ]; } /** * Verify that error code needs to retype BLIK code in frontend * * @param string $code * * @return bool */ private function check_retype_code($code) { $array = [ 'BLK-ERROR-210002', 'BLK-ERROR-210003', 'BLK-ERROR-210004', 'BLK-ERROR-210005', 'BLK-ERROR-210006', 'BLK-ERROR-210007', 'BLK-ERROR-210008', 'BLK-ERROR-210009', 'BLK-ERROR-210010', 'BLK-ERROR-210011', 'BLK-ERROR-210012', 'BLK-ERROR-210013', 'BLK-ERROR-210014', 'BLK-ERROR-210015', 'BLK-ERROR-210016', 'BLK-ERROR-210017', 'BLK-ERROR-210018', 'BLK-ERROR-210019', 'BLK-ERROR-210020', 'BLK-ERROR-210021', 'BLK-ERROR-210022', ]; return in_array($code, $array); } /** * Verify that error code needs to create new paramAlias * * @param string $code * * @return bool */ private function check_new_param_alias($code) { $array = [ 'BLK-ERROR-210002', 'BLK-ERROR-210003', 'BLK-ERROR-210004', ]; return in_array($code, $array); } /** * @return string */ private function getTransactionUrl($id) { $baseUrl = self::getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::TRANSACTION . '/' . $id; } return ''; } /** * @return string */ private function getServiceUrl() { if (isset(self::$serviceUrls[$this->environment])) { return self::$serviceUrls[$this->environment]; } return ''; } /** * @return string */ private function getTransactionCreateUrl() { $baseUrl = self::getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::TRANSACTION; } return ''; } /** * @return string */ private function getServiceInfoUrl() { $baseUrl = self::getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::SERVICE . '/' . $this->serviceId; } return ''; } /** * @param string $cid * * @return string */ private function getProfileBlikUrl($cid) { $baseUrl = $this->getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::PROFILE . '/' . self::SERVICE . '/' . $this->serviceId . '/cid/' . $cid . '/blik'; } return ''; } /** * @param string $transactionUuid * * @return string */ private function getRefundCreateUrl($transactionUuid) { $baseUrl = $this->getServiceUrl(); if ($baseUrl) { return $baseUrl . '/' . self::MERCHANT . '/' . $this->merchantId . '/' . self::TRANSACTION . '/' . $transactionUuid . '/refund'; } return ''; } }