crtlmota / banco-sicredi-connector
抽象REST通信与Sicredi银行API v3.3的接口,仅支持curl
1.0.0
2024-07-08 15:18 UTC
Requires
- php: >=7.4
Requires (Dev)
- fakerphp/faker: ^1.19
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.1
- scrutinizer/ocular: ^1.3
- squizlabs/php_codesniffer: ^3.5
- vlucas/phpdotenv: ^5.4
README
连接Sicredi银行到您的PHP项目
抽象REST通信与Sicredi银行提供的API v3.3接口(748)
最初仅开发了收账款范围(支票 + webhook)的CRUD功能
如何使用
1. 安装
通过composer使用此库
稳定版本
composer require "crtlmota/banco-sicredi-connector"
2. 请求访问SICREDI的沙箱环境
3. 配置凭证
以下示例使用默认测试数据,如果您有权访问沙箱/测试环境,只需执行即可见证魔法发生
$banco = new SicrediCobranca( new CredencialsManager( '123456789', // USERNAME 'teste123', // PASSWORD 'API_KEY_0000000000000', // api key fornecida pela sicredi 'cobranca', // scope '6789', // COOPERATIVA '03', // POSTO '12345', // CODIGO_BENEFICIARIO //(opcional) Também é possível adicionar cache de token embutido para diminuir o tempo de processamento eliminando logins desnecessários function (string $tokenJson) { //new token callback if ($tokenFile = fopen('sicredi-oauth-token.txt', 'w')) { fwrite($tokenFile, $tokenJson); fclose($tokenFile); } }, function () { //get token callback $oAuthTokenData = null; // uso do @ para evitar o warning se o arquivo não existe if (($tokenFile = @fopen('sicredi-oauth-token.txt', 'r')) !== false) { // se tiver arquivo com token, carrega ele e retorna $tokenJson = fread($tokenFile, 8192); $oAuthTokenData = json_decode($tokenJson, true); fclose($tokenFile); return $oAuthTokenData; } else { // retorno "falso" força a emissão de novo token return false; } } ), "https://api-parceiro.sicredi.com.br/sb" // URL do sandbox );
- 无token缓存
root@f660e935d737:/app# php vendor/bin/phpunit tests Time: 00:00.553, Memory: 8.00 MB OK (5 tests, 5 assertions)
- 有token缓存
root@f660e935d737:/app# php vendor/bin/phpunit tests Time: 00:00.249, Memory: 8.00 MB OK (5 tests, 5 assertions)
4. 注册支票
$fakerB = \Faker\Factory::create(); $fakerB->addProvider(new \Faker\Provider\pt_BR\Person($fakerB)); $fakerP = \Faker\Factory::create(); $fakerP->addProvider(new \Faker\Provider\pt_BR\Person($fakerP)); $beneficiario = (new Beneficiario()) ->setTipoPessoa(Pessoa::TIPO_PESSOA_FISICA) ->setDocumento($fakerB->cpf(false)) ->setNome($fakerB->name) ->setLogradouro($fakerB->streetName) ->setNumeroEndereco($fakerB->numberBetween(10, 999)) ->setCidade($fakerB->city) ->setUf($fakerB->stateAbbr()) ->setCep($fakerB->numerify("########")); $pagador = (new Pagador()) ->setTipoPessoa(Pessoa::TIPO_PESSOA_FISICA) ->setDocumento($fakerP->cpf(false)) ->setNome($fakerP->name) ->setLogradouro($fakerP->streetName) ->setNumeroEndereco($fakerP->numberBetween(10, 999)) ->setCidade($fakerP->city) ->setUf($fakerP->stateAbbr()) ->setCep($fakerP->numerify("########")); $dataVencimento = (new DateTime())->modify("+30 day"); $boleto = new Boleto(); $boleto->setPagador($pagador); $boleto->setBeneficiario($beneficiario); $boleto->setSeuNumero("TESTE"); $boleto->setValor(500.00); $boleto->setDataVencimento($dataVencimento->format("Y-m-d")); $boleto->setDesconto1((clone $dataVencimento)->modify('-20 days')->format("Y-m-d"), 200.00); $boleto->setDesconto2((clone $dataVencimento)->modify('-10 days')->format("Y-m-d"), 150.00); $boleto->setDesconto3((clone $dataVencimento)->modify('-5 days')->format("Y-m-d"), 100.00); $boleto->setJuros(1); $boleto->setMulta(12.00); $boleto->addMensagem('APÓS O VENCIMENTO, COBRAR MULTA DE ' . $boleto->getMulta() . '%'); $boleto->addMensagem('APÓS O VENCIMENTO, COBRAR JUROS DE ' . $boleto->getJuros() . '% AO MÊS'); $boleto->setTipoCobranca('HIBRIDO'); $boletoResponse = $banco->createBoleto($boleto); // OU defina false no segundo parametro para não aplicar a pré validação dos dados //Pode-se aplicar a pŕe validação localmente de forma manual, para executar um job de envio em massa mais tarde $boleto->validarBoleto(); $job[] = $boleto; /* exemplo de body esperado $boletoResponse: { TODOS OS TIPOS DE COBRANÇA CONTÉM "linhaDigitavel": "74891125110061420512803153351030188640000009990", "codigoBarras": "74891886400000099901125100614205120315335103", "cooperativa": "0512", "posto": "03", "nossoNumero": "251006142" IF (tipoCobranca:HIBRIDO) "txid": "f69d2a0076fb4ea2bddd7babd1200525", "qrCode": "00020101021226930014br.gov.bcb.pix2571pix-qrcodeh.sicredi.com.br/qr/v2/cobv/6946459e4b6e4c19ab5c9689fe0df30a520400005303986540599.905802BR5921OLIVEIRA MULTI MARCAS6008BRASILIA62070503***6304E5E1", IF (tipoCobranca:SPLIT) "splitBoleto": { "repasseAutomaticoSplit": "SIM", "tipoValorRateio": "PERCENTUAL", "regraRateio": "VALOR_COBRADO", "destinatarios": [ { "codigoBanco": "237", "codigoAgencia": "0434", "numeroContaCorrente": "2323232323", "numeroCpfCnpj": "02738306004", "nomeDestinatario": "DECIO OLIVEIRA", "parcelaRateio": "1", "valorPercentualRateio": 24.22, "floatSplit": 20 } ] } } */
还提供其他方法
$findedBoleto = $banco->getBoleto($boletoResponse['nossoNumero']); //'busca Boleto pelo nossoNumero' $findedBoletoPdfTempFilepath = $banco->getBoletoPdf($boletoResponse['linhaDigitavel']); $findedBoletoEncodedPdf = $banco->getBoletoEncoded($boletoResponse['linhaDigitavel']); $updatedBoleto = $banco->updateVencimento($boletoResponse['nossoNumero'], $dataVencimento->modify("+5 days")->format("Y-m-d")); $boletosPagos = $banco->listBoletosPagosByDay($dataVencimento->modify("+5 days")); $banco->baixaBoleto($boletoResponse['nossoNumero']);
5. 迁移到生产环境
更改凭证到生产环境,您的应用程序即可集成到银行。
文档
-
收账款处理 HandleBoleto.php
-
webhook合约 ContratarWebhook.php
为使用这些类提供基本示例。
执行示例的参数应保存到名为.env
的文件中,配置示例位于.env.example
文件中
简化了您的任务吗?
如果该项目的代码帮助您完成了一个复杂任务,请考虑向作者通过以下PIX进行捐赠。
PIX键:d8ea7c17-2f20-492f-b45b-1913bf9d5819
注意
所有可验证的数据都必须有效。请始终使用有效的CPF/CNPJ、CEP、城市和州。为了避免打扰陌生人,请使用您自己的数据或任何知道情况的个人的数据,因为收账款总是记录在银行中央的实时系统中,并会出现在收款人的DDA上。示例数据不是有效的,如果未更改,则示例脚本将无法运行。
使用others
属性
others
属性允许向请求添加额外的数据,这些数据不是类属性定义的。这用于发送自定义信息或未映射到API标准结构的具体字段。
示例
use crtlmota\BancoSicrediConnector\Boleto; // Crie um objeto Serializable $serializable = new Boleto(); // Adicione dados adicionais ao atributo `others` $serializable->addOthers('custom_field_1', 'valor_do_campo_1'); $serializable->addOthers('custom_field_2', 'valor_do_campo_2'); // Converta o objeto para JSON $json = json_encode($serializable); // Exemplo de saída { ... outros dados "valor": 100, "dataEmissao": '2024-07-01', "dataVencimento": '2024-07-01' "custom_field_1": "valor_do_campo_1", "custom_field_2": "valor_do_campo_2" }
许可
本项目的所有代码均许可在GNU Lesser General Public License版本3下。
它可以未经修改地用于任何封闭或开源项目,所做的任何修改都必须以开源代码的形式提供给系统用户。