github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/mock/pefisa.go (about)

     1  package mock
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  
     7  	"github.com/gin-gonic/gin"
     8  )
     9  
    10  func getTokenPefisa(c *gin.Context) {
    11  	b, _ := ioutil.ReadAll(c.Request.Body)
    12  	const tok = `{
    13  		"access_token": "9a7a4813-63e5-4d9a-95cc-30e3800de95e",
    14  		"token_type": "bearer",
    15  		"expires_in": 86399,
    16  		"scope": "app"
    17  	}`
    18  
    19  	const tokError = `
    20  	{
    21  		"error": "unauthorized",
    22  		"error_description": "SD-756: clientId e/ou secret inv¿lido(s)"
    23  	}`
    24  
    25  	if strings.Contains(string(b), `grant_type=client_credentials`) {
    26  		c.Data(200, "application/json", []byte(tok))
    27  	} else {
    28  
    29  		c.Data(401, "application/json", []byte(tokError))
    30  	}
    31  
    32  }
    33  
    34  func registerPefisa(c *gin.Context) {
    35  	b, _ := ioutil.ReadAll(c.Request.Body)
    36  	const resp = `
    37  	{
    38  		"data": {
    39  			"codigoBarras": "17496772400001300000000000002670000000101364",
    40  			"linhaDigitavel": "17490.00004   00002.670008   00001.013648   6   77240000130000",
    41  			"idTitulo": 382565
    42  		}
    43  	}`
    44  
    45  	const respError = `
    46  	{
    47  		"error" : [ {
    48  		  "code" : "COB-2344",
    49  		  "message" : "Inser¿¿o do T¿tulo negada, pois a data de emiss¿o(05/12/2018@DataProximoDiaUtil=22/11/2018) ¿ posterior a D+1(@#DataProximoDiaUtil@#) da data de refer¿ncia do sistema.",
    50  		  "action" : "Verificar a data de emiss¿o do titulo."
    51  		} ]
    52  	}`
    53  
    54  	const respErrorTitleExist = `
    55  	{
    56  		"error": [
    57  			{
    58  				"code": "COB-2186",
    59  				"message": "Já existe um título em aberto cadastrado com o cedente \"267\", seu numero \"021045640\", data de vencimento \"30/01/2019\", valor \"2\" e emitente \"211\". ",
    60  				"action": "Altere um dos dados."
    61  			}
    62  		]
    63  	}`
    64  
    65  	const respErrorSpecialCaractere = `{
    66  		"error" : [ {
    67  			"code" : "MTR-002",
    68  			"message" : "O campo pagador.nome do JSON enviado tem um formato inválido. ",
    69  			"field" : "pagador.nome"
    70  		} ]
    71  	}`
    72  
    73  	if strings.Contains(string(b), `"valorTitulo": "2.00"`) {
    74  		c.Data(200, "application/json", []byte(resp))
    75  	} else if strings.Contains(string(b), `"valorTitulo": "2.01"`) {
    76  		c.Data(400, "application/json", []byte(respErrorSpecialCaractere))
    77  	} else if strings.Contains(string(b), `"valorTitulo": "3.00"`) {
    78  		c.Data(400, "application/json", []byte(respErrorTitleExist))
    79  	} else if strings.Contains(string(b), `"valorTitulo": "1.00"`) {
    80  		panic("")
    81  	} else {
    82  		c.Data(400, "application/json", []byte(respError))
    83  	}
    84  
    85  }