✏️ 正在编辑: Partner.php
路径:
/srv/systems_dir/koruspay/library/Core/Service/Partner.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_Service_Partner { private $_companyId; private $_url; private $_apiKey; private $_webhookAuthorization; public function __construct($companyId = null) { if (empty($companyId)) { throw new Core_Exception("Empresa não informada." ); } $tableCompany = Model_Container::getCompany(); $tableCredential = Model_Container::getCredential(); $company = $tableCompany->findById($companyId); $credential = $tableCredential->findByCompanyId($companyId); if (empty($company) || empty($credential)) { throw new Core_Exception("Empresa não possui integração." ); } $this->_companyId = $company->id; $this->_url = $company->webhook; $this->_apiKey = $credential->api_key; $this->_webhookAuthorization = $company->webhook_authorization; } protected function send($params) { $signature = $this->_webhookAuthorization ? ["Signature: {$this->_webhookAuthorization}"] : []; $options = [ CURLOPT_URL => $this->_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 => array_merge([ "Accept: application/json", "ApiKey: {$this->_apiKey}", "Content-type: application/json" ], $signature), ]; $curl = curl_init(); curl_setopt_array($curl, $options); $json = curl_exec($curl); $response = json_decode($json); $info = curl_getinfo($curl); $codeErros = [400, 401, 403, 429]; curl_close($curl); Yuppie_Log::write("Send Partner, parâmetros enviados para url {$this->_url}:" . print_r($params, true), $this); Yuppie_Log::write("Retorno do Send Partner: Code HTTP - " . $info['http_code'] . ' - ' . print_r($response, true), $this); Model_RequestsHistory::saveRequest(json_encode($params), 'output', $this->_companyId, $this->_url); Model_RequestsHistory::saveRequest($json, 'input', $this->_companyId, $this->_url); if (in_array($info['http_code'], $codeErros)) { $retorno = new stdClass(); $retorno->erro = $info['http_code']; return $retorno; } return $response; } public function returnInvoices(array $ids) { if (count($ids) == 0) { return false; } $dbTable = new Model_DbTable_Transactions(); $invoices = []; foreach ($ids as $id) { $transaction = $dbTable->findTransaction($id); if (empty($transaction['invoice_date'])) { continue; } $invoices['invoices'][] = [ 'koruspay_charge_id' => Core_Crypt::Encrypt('koruspay_id=' . $transaction['id']), 'id' => $transaction['id'], 'id_integration' => $transaction['id_integration'], 'status' => $transaction['situation'], 'status_description' => $transaction['status_description'], 'invoice_date' => $transaction['invoice_date'], 'description' => $transaction['description'], 'amount' => $transaction['amount'], 'discount' => $transaction['discount'], 'penalty' => $transaction['penalty'], 'due_date' => $transaction['due_date'], 'digitable_line' => $transaction['digitable_line'], 'barcode' => $transaction['barcode'], 'pix_qr_code' => $transaction['pix_qr_code'], 'pix_copy_paste' => $transaction['pix_copy_paste'], 'low_date' => $transaction['low_date'], 'amount_paid' => $transaction['amount_paid'], 'payment_date' => $transaction['payment_date'], 'amount_tax' => $transaction['amount_tax'], 'payer' => [ 'name' => $transaction['payer'], 'doc_number' => $transaction['doc_number'], 'email' => $transaction['payer_email'], 'phone' => $transaction['payer_cell'] ] ]; } if (count($invoices) == 0) { return; } $response = $this->send($invoices); if ($response->erro) { Yuppie_Log::write('Houve um erro no envio do webhook. ' . print_r($response, true), $this); } Yuppie_Log::write("Retorno APIKorus invoices => " . print_r($invoices, true)); return $invoices; } public function returnError(array $charges) { if (count($charges) == 0) { return false; } $response = $this->send(['errors' => $charges]); if ($response->erro) { throw new Exception('Houve um erro no envio.'); } return $response; } public function sendNFSe(int $docketId) { $company = Model_Container::getCompany()->findById($this->_companyId); if (!$company->automatic_nfse) { return; } $serviceNFSe = new Core_Service_NFSe(); $serviceNFSe->send($docketId); } public function sendProofPayment($transaction) { if (!$transaction || !$transaction->proof_url) { return; } $company = Model_Container::getCompany()->findById($this->_companyId); if (!$company->automatic_proof_payment || !$company->email) { return; } try { $view = new \Zend_View(); $view->setScriptPath(APPLICATION_PATH . '/views/scripts'); $view->company = $company; $view->transaction = $transaction; $body = $view->render('transactions/proof-payment.phtml'); $email = new Core_Mail(); $email->enviarMensagemHtml( 'KorusPay', 'no-reply@koruspay.com.br', $company->reason, $company->email, 'Comprovante de pagamento', $body ); } catch (Throwable $e) { Yuppie_Log::write("Erro ao enviar comprovante de pagamento para parceiro: " . $e->getMessage(), $this); } } }
💾 保存文件
← 返回文件管理器