✏️ 正在编辑: AQBankService.php
路径:
/srv/systems_dir/koruspay/application/Modules/Api/AQBankService.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php namespace Modules\Api; use Carbon\Carbon; use Core_Exception; use Core_Service_AQBank; use Exception; use Model_Container; use Model_Company; use Model_Payers; use Model_Transactions; use String_Refatorar; class AQBankService { public static $_calculateTaxTransaction = true; public function createPayer($payer) { $service = new Core_Service_AQBank($payer['company_id'], $payer['access_key']); $payer = $service->createPayer($payer); return $payer; } public function createInvoice($transaction, $type) { if (empty($transaction['access_key'])) { throw new Exception('Ação não permitida'); } if (in_array($type, ['PIX', 'BANKSLIP'])) { if (empty($transaction['payer_id'])) { throw new Exception('Pagador não informado.'); } if (empty($transaction['id_payer'])) { $transaction['id_payer'] = (new Model_Payers)->createPayer($transaction['payer_id']); } if (empty($transaction['id_payer'])) { throw new Exception('Erro ao cadastrar o pagador.'); } } $service = new Core_Service_AQBank($transaction['company_id'], $transaction['access_key']); if ($type == 'BANKSLIP') { $charge = $service->createInvoiceBankSlip($transaction); } else { $charge = $service->createInvoicePix($transaction); } if ($charge->error) { throw new Exception($charge->error); } if (in_array($type, ['PIX', 'SPLIT'])) { $data = [ "situation" => 'PENDENTE', "invoice_id_pix" => $charge->id, "pix_qr_code" => $charge->brcode, "pix_copy_paste" => $charge->brcode, "invoice_date" => Carbon::now()->format('Y-m-d H:i:s') ]; } elseif ($type == 'BANKSLIP') { $data = [ "situation" => 'PENDENTE', "invoice_id_boleto" => $charge->id, "pix_copy_paste" => $charge->brcode_boleto_pix, "barcode" => $charge->barcode, "digitable_line" => $charge->digitable_line, "invoice_date" => Carbon::now()->format('Y-m-d H:i:s') ]; } return $data ?: []; } public function getInvoiceDetail($transaction, $type) { if (empty($transaction['access_key'])) { throw new Exception('Ação não permitida'); } $service = new Core_Service_AQBank($transaction['company_id'], $transaction['access_key']); if (strtoupper($type) == 'PIX') { $invoice = $service->getInvoicePixDetail($transaction['invoice_id_pix']); } else { $invoice = $service->getInvoiceBankSlipDetail($transaction['invoice_id_boleto']); } if (empty($invoice->status)) { throw new Exception('Fatura não encontrada.'); } $status = [ 'created' => 'PENDENTE', 'pending' => 'PENDENTE', 'paid' => 'RECEBIDO', 'canceled' => 'ERRO' ]; $situation = $status[$invoice->status]; $data = [ 'situation' => $situation, 'proof_url' => $invoice->transaction->proof_of_transaction, 'payment_date' => $situation == 'RECEBIDO' ? $invoice->updated_at : null, 'amount_paid' => $situation == 'RECEBIDO' ? $invoice->amount : null, 'low_date' => !$transaction['low_date'] && $situation == 'RECEBIDO' ? Carbon::now()->format('Y-m-d H:i:s') : $transaction['low_date'], 'pix_qr_code' => $invoice->brcode ?: null, 'pix_copy_paste' => $invoice->brcode ?: null, 'barcode' => $invoice->brcode_boleto_pix ?: null, 'digitable_line' => $invoice->digitable_line ?: null, ]; return array_filter($data); } public function createTransfer($transaction, $type) { if (empty($transaction['access_key'])) { throw new Core_Exception('Ação não permitida'); } $service = new Core_Service_AQBank($transaction['company_id'], $transaction['access_key']); $transfer = $service->createTransfer($transaction, $type == 'TARIFA'); if ($transfer->error) { throw new Core_Exception($transfer->error); } return [ 'id' => $transfer->id, 'status_description' => null ]; } public function getTransferDetail($transaction) { if (empty($transaction['access_key'])) { throw new Exception('Ação não permitida'); } $service = new Core_Service_AQBank($transaction['company_id'], $transaction['access_key']); $transfer = $service->getTransferDetail($transaction['transfer_id']); return $this->transfer($transfer); } public function getTaxDetail($tax) { if (empty($tax['access_key'])) { throw new Exception('Ação não permitida'); } $service = new Core_Service_AQBank($tax['company_id'], $tax['access_key']); $transfer = $service->getTransferDetail($tax['transfer_id']); if ($transfer->status != 'success') { throw new Exception('Saque bloqueado! Tarifas pendentes.'); } return $this->transfer($transfer); } public function getExtract($fields, $company) { if (empty($company['access_key'])) { throw new Exception('Empresa sem permissão de acesso.'); } $serviceApi = new Core_Service_AQBank($company['id'], $company['access_key']); $extract = $serviceApi->getExtract($fields); if ($extract->error) { throw new Exception($extract->error); } return $extract; } public function receipt($invoicePix) { if (empty($invoicePix->id)) { throw new Exception('Identificador do recebimento não informado.'); } if (empty($invoicePix->status) || $invoicePix->status !== 'paid') { throw new Exception('Diferente de pago.'); } $data = [ 'situation' => 'RECEBIDO', 'proof_url' => $invoicePix->transaction->proof_of_transaction, 'payment_date' => $invoicePix->updated_at, 'amount_paid' => $invoicePix->amount, 'low_date' => Carbon::now()->format('Y-m-d H:i:s'), ]; return $data; } public function transfer($transfer) { if (empty($transfer->status)) { throw new Exception('Status não informado.'); } if (empty($transfer->id)) { throw new Exception('Identificador da transferencia não informado.'); } $status = [ 'created' => 'PROCESSANDO', 'prossesing' => 'PROCESSANDO', 'success' => 'CONCLUIDO', 'denied' => 'ERRO', 'canceled' => 'ERRO', 'failed' => 'ERRO', ]; $situation = $status[$transfer->status]; $data = [ 'situation' => $situation, 'status_description' => $transfer->transaction->description, 'proof_url' => $transfer->transaction->proof_of_transaction, 'date_error' => $situation == 'ERRO' ? Carbon::now()->format('Y-m-d') : null, 'amount_paid' => String_Refatorar::parseStringToFloat($transfer->transaction->amount) * -1, 'payment_date' => $situation == 'CONCLUIDO' ? $transfer->created_at : null, 'low_date' => $situation == 'CONCLUIDO' ? $transfer->updated_at : null ]; return $data; } public function getCompareExtract() { $fields = [ "status" => "paid", "initial_date" => Carbon::now()->subDays(3)->format("Y-m-d"), "final_date" => Carbon::now()->format("Y-m-d") ]; $table = Model_Container::getCompany(); $companys = $table->fetchAll(['deleted = 0']); $model = new Model_Transactions(); $inAll = []; $outAll = []; foreach ($companys as $company) { $fields['status'] = 'paid'; $in = $model->getExtract($fields, $company); if (is_array($in)) { $inAll = array_merge($inAll, $in); } elseif (!empty($in)) { $inAll = array_merge($inAll, (array) $in); } $fields['status'] = 'success'; $out = $model->getExtract($fields, $company); if (is_array($out)) { $outAll = array_merge($outAll, $out); } elseif (!empty($out)) { $outAll = array_merge($outAll, (array) $out); } } $in = $inAll; $out = $outAll; $extract = array_merge($in, $out); foreach ($extract as $item) { $company = Model_Company::getByDocNumber($item->client->taxpayer_id); if (empty($company)) { continue; } if ($item->type == 'in') { $type = 'RECEBIMENTO'; $description = $item->transaction->descripition . ' - Pagador: ' . $item->payer->name . ', N DOC: ' . $item->payer->taxpayer_id; } if ($item->type == 'out' && $item->transaction->type == 'p2p') { $type = 'TARIFA'; $description = 'Desconto de tarifa'; } if ($item->type == 'out' && $item->transaction->type == 'pix') { $type = 'PAGAMENTO'; $description = $item->transaction->descripition . ' - Recebedor: ' . $item->transfer_part->part_name . ', N DOC: ' . $item->transfer_part->taxpayer_id . ', Banco: ' . $item->transfer_part->bank_name . ', Agência: ' . $item->transfer_part->branch_code . ' - Conta: ' . $item->transfer_part->account_number; } $data = [ 'created_at' => $item->created_at, 'payment_date' => $item->updated_at, 'low_date' => $item->updated_at, 'company_id' => $company->id, 'invoice_id_pix' => $item->resource == 'invoice-pix' ? $item->transaction->invoice_id : null, 'invoice_id_boleto' => $item->resource == 'invoice_boleto' ? $item->transaction->invoice_id : null, 'transfer_id' => $item->resource == 'transfer' ? $item->transfer_part->id : null, 'description' => $description, 'amount' => $item->amount, 'amount_paid' => $item->amount < 0 ? $item->amount * -1 : $item->amount, 'situation' => $item->status == 'success' ? 'CONCLUIDO' : 'RECEBIDO', 'type' => $type, ]; $id = $item->type == 'in' ? $item->transaction->invoice_id : $item->transfer_part->id; if (!empty($id)) { $model->updateOrCreateTransactionByExtract($id, $data); } } } public function getBalance($company) { return (new Core_Service_AQBank($company['id'], $company['access_key']))->getBalance(); } }
💾 保存文件
← 返回文件管理器