✏️ 正在编辑: AQBank.php
路径:
/srv/systems_dir/koruspay/library/Core/Service/AQBank.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php use Carbon\Carbon; class Core_Service_AQBank { // private $_url = "https://apipaysandbox.aqbank.com.br/api"; private $_url = "https://apishopaqpago.aqbank.com.br/api"; private $_secretKey; private $_taxDocument; private $_sellerId; private $_token; private $_companyId; private $_extract = []; const NOME_API = 'AQBank'; public function __construct($companyId = null, $accessKey = null) { $config = new Zend_Config_Ini(CLIENT_PATH . "/configs/application.ini"); $aqBank = $config->production->aqbank; if (empty($aqBank)) { throw new Core_Exception("Dados de integração com o AQ Bank não estão configurados." ); } $this->_taxDocument = $aqBank->document; $this->_secretKey = $aqBank->secret_key; $this->_sellerId = $aqBank->seller_id; if ($accessKey) { $seller = $this->getSellerKey($accessKey); $this->_token = null; } $this->_companyId = $companyId; if ($seller->access_key) { $this->_secretKey = $seller->access_key; } } public function token() { $this->newToken(); return $this->_token; } protected function newToken() { $params = [ 'tax_document' => $this->_taxDocument, 'secret_access_key' => $this->_secretKey ]; $curl = curl_init(); $url = $this->_url . "/auth/login"; curl_setopt_array($curl, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode($params), CURLOPT_HTTPHEADER => [ "Accept: application/json", "Content-type: application/json" ], ]); $response = curl_exec($curl); curl_close($curl); $token = json_decode($response); Yuppie_Log::write("Gerado novo token: " . print_r($response, 1), $this); if (empty($token->token)) { Yuppie_Log::write("Erro na geração do novo token: " . print_r($response, 1), $this); throw new Exception('Erro na geração do novo token.'); } $this->_token = $token->token; $expiraEm = Zend_Date::now()->addSecond($token->expira_em); (new Model_DbTable_TokenApi())->saveToken(self::NOME_API, $this->_token, $expiraEm->get('yyyy-MM-dd HH:mm:ss'), $this->_companyId); Yuppie_Log::write("Novo token gerado. Expira em: " . $expiraEm->get('yyyy-MM-dd HH:mm:ss'), $this); } protected function getResponse($endPoint, $params, $method = 'POST') { $url = $this->_url . $endPoint; if ($method == 'GET') { $url .= !empty($params) ? '?' . http_build_query($params) : ''; } $options = [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => [ "Accept: application/json", "Authorization: Bearer {$this->_token}", "Content-type: application/json" ], ]; if ($method == 'POST') { $options[CURLOPT_POSTFIELDS] = json_encode($params); } $curl = curl_init(); curl_setopt_array($curl, $options); $json = curl_exec($curl); $response = json_decode($json); $info = curl_getinfo($curl); $codeError = [400, 422, 429, 499]; curl_close($curl); Yuppie_Log::write("getResponse AQ Bank, parâmetros enviados para url {$url}:" . print_r($params, true), $this); Yuppie_Log::write("Retorno do getResponse AQ Bank: Code HTTP - " . $info['http_code'] . ' - ' . print_r($response, true), $this); Model_RequestsHistory::saveRequest(json_encode($params), 'output', $this->_companyId, $url); Model_RequestsHistory::saveRequest($json, 'input', $this->_companyId, $url); if (in_array($info['http_code'], $codeError)) { $msgErro = ''; if (is_array($response->errors)) { foreach ($response->errors as $erro) { $msgErro .= current($erro) . ' | '; }; } else { $msgErro = $response->errors ?: $response->error; } $retorno = new stdClass(); $retorno->error = $msgErro; $retorno->msg = $msgErro; return $retorno; } return $response; } protected function getTokenApi() { $tokenValido = (new Model_DbTable_TokenApi())->checkTokenValido(self::NOME_API, $this->_companyId); if ($tokenValido) { $this->_token = $tokenValido['token']; return; } $this->newToken(); } public function createSeller(array $company) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/accreditation/business"; $params = [ "ein" => $company['doc_number'], "business_name" => $company['reason'], "business_email" => $company['email'], "business_phone" => $company['phone'], "business_opening_date" => $company['registration_date'], "mcc" => "8999", // Código passado pelo AQ Bank "statement_descriptor" => $company['fantasy'] ]; $params['business_address'] = [ 'zip_code' => $company['zip_code'], 'street' => $company['street'], 'number' => $company['number'], 'city' => $company['city'], 'state' => $company['state'], 'district' => $company['neighborhood'] ]; $name = explode(' ', $company['accountable']); $params['owner'] = [ 'name' => $name[0], 'lastname' => $name[1], 'taxpayer_id' => $company['doc_accountable'], 'email' => $company['email'], 'phone' => $company['cell'], 'birthdate' => $company['birthdate_accountable'], 'mcc' => "8999", // Código passado pelo AQ Bank 'statement_descriptor' => $company['fantasy'] ]; $response = $this->getResponse($endPoint, $params); return $response->body ?: null; } public function getSellers() { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/accreditation"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: null; } public function createSellerKey(string $sellerId, string $sellerIp) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/access/$sellerId/accreditation"; $params = [ "ip" => $sellerIp ]; $response = $this->getResponse($endPoint, $params); return $response->body ?: null; } public function getSellerKey(string $sellerKey) { $key = Core_Crypt::Decrypt($sellerKey); $return = new stdClass(); $return->access_key = $key; return $return; // if (empty($this->_token)) { // $this->getTokenApi(); // } // $endPoint = "/access/$sellerId/accreditation"; // $params = []; // $response = $this->getResponse($endPoint, $params, 'GET'); // return current($response->access) ?: null; } public function createPixKey(string $type, ?string $key = null) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/pix-key"; $params["type"] = $type; if ($key) { $params["key_id"] = $type; } $response = $this->getResponse($endPoint, $params); return $response->data ?: null; } public function getDict(?string $key = null) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/dict/{$key}"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: null; } public function createPayer(array $payer) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/payer-service"; $name = explode(' ', $payer['name']); $params = [ "name" => $name[0], "lastname" => $name[1], "tapayer_id" => $payer['doc_number'], "phone" => $payer['cell'], "email" => $payer['email'] ]; $params['address'] = [ "zipcode" => $payer['zip_code'], "street" => $payer['street'], "number" => $payer['number'], "district" => $payer['neighborhood'], "city" => $payer['city'], "state" => $payer['state'], "complement" => $payer['complement'] ]; $response = $this->getResponse($endPoint, $params); return $response->body ?: null; } public function getPayers() { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/payer-service"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: null; } public function getPayer(string $idPayer) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/payer-service/{$idPayer}"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: null; } public function createInvoicePix(array $invoice) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/invoice-pix"; $params = [ "payer_id" => $invoice['id_payer'], "invoice_name" => $invoice['payer'], "amount" => String_Refatorar::soNumeros($invoice['amount']), // o valor deve ser inteiro, sem casas décimais ex.: R$ 78,52 = 7852 "validate" => $invoice['due_date'] ?: Carbon::now()->format('Y-m-d'), "descripition" => $invoice['description'], "penalty" => $invoice['penalty'] ?: 0.00, "type" => "unique", "method" => "pix", // "expiration" => 8600, "reference_id" => $invoice['id'], "single_payer" => false ]; $response = $this->getResponse($endPoint, $params); return $response->body ?: $response; } public function getInvoicePixDetail(string $id) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/invoice-pix/{$id}"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: $response; } public function createInvoiceBankSlip(array $invoice) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/invoice-boleto"; $params = [ "payer_id" => $invoice['id_payer'], "invoice_name" => $invoice['description'], "amount" => String_Refatorar::soNumeros($invoice['amount']), // o valor deve ser inteiro, sem casas décimais ex.: R$ 78,52 = 7852 "validate" => $invoice['due_date'], "descripition" => $invoice['description'], "penalty" => $invoice['penalty'], "type" => "unique", "method" => "boleto", "discount" => $invoice['discount'], "installments" => 1, "reciver_name" => $invoice['company_reason'], "taxreciver_id" => $invoice['company_doc_number'] ]; $response = $this->getResponse($endPoint, $params); return $response->body ?: $response; } public function getInvoiceBankSlipDetail(string $id) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/invoice-boleto/{$id}"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: $response; } public function createTransferTed(array $transfer) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/v2/transfer"; $params = [ "method" => "manual", "amount" => String_Refatorar::soNumeros($transfer['amount']), // o valor deve ser inteiro, sem casas décimais ex.: R$ 78,52 = 7852 "taxpayer_id" => $transfer['doc_number'], "name" => $transfer['name'], "bank_code" => $transfer['bank_code'], "branch_code" => $transfer['branch_code'], "account_number" => $transfer['account_number'], "account_type" => "checking",//checking", "payment", "savings" ,"salary", "checking", "accredited_reciver_id" => $transfer['seller_id'], "descripition" => $transfer['description'] ]; $response = $this->getResponse($endPoint, $params); return $response->body ?: $response; } public function createTransfer(array $transfer, $tarifa = false) { sleep(10); if (empty($this->_token)) { $this->getTokenApi(); } if (empty($tarifa) && empty($transfer['chave_pix'])) { throw new Exception('Chave Pix é obrigatória para transferência.'); // return $this->createTransferTed($transfer); } $endPoint = "/transfer-ex"; $params = [ "amount" => String_Refatorar::soNumeros($transfer['amount']), // o valor deve ser inteiro, sem casas décimais ex.: R$ 78,52 = 7852 "descripition" => $transfer['description'], "reference_id" => $transfer['id'] ]; if ($tarifa) { $params['method'] = 'p2p'; $params['accredited_reciver_id'] = $this->_sellerId; } else { $params['method'] = 'pix'; $params['dict_key'] = $transfer['chave_pix']; } $response = $this->getResponse($endPoint, $params); return $response->body ?: $response; } public function getTransferDetail(string $id) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/transfer/{$id}"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->data ?: $response; } public function getBalance() { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/balance"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->body ?: null; } public function getExtract(array $filters, $page = 1) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/extract"; $params = [ "page" => $page, "status" => $filters['status'], "initial_date" => $filters['initial_date'], "final_date" => $filters['final_date'], // "transaction_id" => $filters['transaction_id'] ]; $response = $this->getResponse($endPoint, $params, 'GET'); $this->_extract = array_merge($this->_extract, $response->data); if ($response->meta->to < $response->meta->total) { $page += 1; $this->getExtract($filters, $page); } if ($response->erro) { return $response; } return $this->_extract; } public function getExtractDetail(string $id) { if (empty($this->_token)) { $this->getTokenApi(); } $endPoint = "/extract/{$id}"; $response = $this->getResponse($endPoint, [], 'GET'); return $response->body ?: $response; } }
💾 保存文件
← 返回文件管理器