domain = $domain; $this->callback = $domain . $this->callback; } /** * @var Guzzle the guzzle client */ protected $_client; /** * @var array the guzzle service client options */ protected $_options = ["curl" => [CURLOPT_CONNECTTIMEOUT => 10]]; /** * Get total package cost * @param array $params * @return array|mixed|null */ public function getTotal($credit, $price_credit,$comma) { $initial_total = bcmul($credit, $price_credit, $comma); $feeValue = bcmul($initial_total, $this->getFee(), $comma); $fee = bcdiv($feeValue, 100, $comma); return bcadd($initial_total, $fee, $comma); } /** * @param string $credit * @param string $price_credit * @param string $comma * @return string */ public function getTransactionFee($credit, $price_credit,$comma){ $initial_total = bcmul($credit, $price_credit, $comma); $feeValue = bcmul($initial_total, $this->getFee(), $comma); return bcdiv($feeValue, 100, $comma); } /** * @param string $credit * @param string $price_credit * @param string $comma * @return string */ public function getPackageCost($credit, $price_credit, $comma){ return bcmul($credit, $price_credit, $comma); } /** * @param string $username * @param string $password * @return array */ public function authenticate(){ $username = Yii::$app->params['authUsername']; $password = Yii::$app->params['authPassword']; $response = $this->request('POST', $this->$uriToken, [ "grant_type" => "password", "client_secret" => Yii::$app->params['authSecretKey'], "scope" => "", "username" => $username, "password" => $password, "client_id" => 6 ]); if (isset($response)){ return $response; } return []; } public function request($method, $uri, $params = []){ try { $client = $this->getClient(); $response = $client->request($method, $uri,['form_params'=>$params]); $body = $response->getBody(); return json_decode($body, true); } catch (RequestException $e) { if ($e->hasResponse()) { $statusCode = $e->getResponse()->getStatusCode(); if(in_array($statusCode, [500])){ return [ 'errors' => [ 'message' => $e->getMessage() ] ]; } if(in_array($statusCode, [404])){ return ['errors', ['message'=>'Not found']]; } $response = $e->getResponse(); $body = $response->getBody(true); $decoded = json_decode($body, true); if ($decoded === null){ return [ 'errors' => [ 'message' => $e->getMessage() ] ]; } return $decoded; } } } /** * @param Guzzle $client */ public function setClient($client) { $this->_client = $client; } /** * @return Guzzle */ public function getClient() { if ($this->_client === null) { $this->_client = new Guzzle([ 'base_uri' => $this->baseUrl, "config" => $this->_options, 'headers'=> [ 'X-WebsiteId' => $this->websiteId, 'X-webShopToken'=>$this->webShopToken ] ]); } return $this->_client; } public function getFee(){ return 0.8; } /** * @param string external id (AUCT) * @return object body */ public function createCryptoPayment($tocken, $order){ $params = [ 'external_id' => 'AUCT' . $order['id'], 'required_amount' => $order['price'], 'callback_url' => $this->callback ]; $currencyUri = $this->getCurrencyUri($order['id_currency']); return $this->sendRequest($this->method, $tocken, $this->baseUrl, $currencyUri, $params); } /** * @param currency id * @return currency Uri */ private function getCurrencyUri($id){ if ($id == 4) { return $this->btcUri; } elseif ($id == 5) { return $this->ethUri; } elseif ($id == 6){ return $this->xUri; } } /** * @param string external id (AUCT) * @return object body */ public function checkcCryptoPayment($external_id, $token){ $params = [ 'external_id' => $external_id ]; return $this->sendRequest($this->method, $token, $this->baseUrl, $this->btcUriCheckOrder, $params); } /** * @param int currency id * @return array currency settings and details */ public function getCurrencySettings($id){ $currencySettings = Yii::$app->cache->get('currency'); if ($currencySettings === false) { $settings = Yii::$app->dbExt->getCurrency($id); $currencySettings = Yii::$app->cache->set('currency', $settings, 600); } return $currencySettings; } /** * @param string method, token, url, uri , array params * @return object body */ public function sendRequest($method, $token, $url, $uri, $params){ try { $client = $this->getClient(); $request = $client->request($method, $url . $uri, [ 'headers' => [ 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' .$token ], 'form_params' => $params ]); return json_decode($request->getBody(), true); } catch (RequestException $e) { if ($e->hasResponse()) { $statusCode = $e->getResponse()->getStatusCode(); if(in_array($statusCode, [500])){ return [ 'errors' => [ 'message' => $e->getMessage() ] ]; } if(in_array($statusCode, [404])){ return ['errors', ['message'=>'Not found']]; } $response = $e->getResponse(); $body = $response->getBody(true); $decoded = json_decode($body, true); if ($decoded === null){ return [ 'errors' => [ 'message' => $e->getMessage() ] ]; } return $decoded; } } } }