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

     1  package mock
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/gin-gonic/gin"
     9  )
    10  
    11  const contentApplication = "text/json"
    12  
    13  const success = `
    14     {
    15  	"account_id": "946b50ce-ed5d-45ab-8c86-ce3baf90a73a",
    16  	"amount": 5000,
    17  	"barcode": "19799900100000050000000038911660052705632042",
    18  	"beneficiary": {
    19  		"account_code": "23172018",
    20  		"branch_code": "1",
    21  		"document": "14994237000140",
    22  		"document_type": "cnpj",
    23  		"legal_name": "MUNDIPAGG TECNOLOGIA EM PAGAMENTOS S.A.",
    24  		"trade_name": "MUNDIPAGG TECNOLOGIA EM PAGAMENTOS S.A."
    25  	},
    26  	"created_at": "2021-07-01T14:32:21Z",
    27  	"created_by": "application:3279b005-5e40-41c1-996e-8cec24f8006b",
    28  	"customer": {
    29  		"document": "13621248773",
    30  		"document_type": "cpf",
    31  		"legal_name": "Matheus Palanowski",
    32  		"trade_name": null
    33  	},
    34  	"discounts": [],
    35  	"expiration_date": "2022-05-30",
    36  	"expired_at": null,
    37  	"fee": 0,
    38  	"fee_metadata": {
    39  		"billing_exemption_participant": true,
    40  		"fee": 0,
    41  		"max_free": 5,
    42  		"original_fee": 200,
    43  		"remaining_free": 5
    44  	},
    45  	"fine": null,
    46  	"id": "46e902e7-05e6-4efb-a28f-cf8b16ce9eed",
    47  	"interest": null,
    48  	"invoice_type": "bill_of_exchange",
    49  	"issuance_date": "2021-07-01",
    50  	"limit_date": "2022-05-30",
    51  	"our_number": "38911660052705632042",
    52  	"receiver": null,
    53  	"registered_at": null,
    54  	"settled_at": null,
    55  	"status": "CREATED",
    56  	"writable_line": "19790000053891166005827056320420990010000005000"
    57  }
    58  `
    59  const successWithoutOurNumber = `
    60     {
    61  	"account_id": "946b50ce-ed5d-45ab-8c86-ce3baf90a73a",
    62  	"amount": 5000,
    63  	"barcode": "19799900100000050000000038911660052705632042",
    64  	"beneficiary": {
    65  		"account_code": "23172018",
    66  		"branch_code": "1",
    67  		"document": "14994237000140",
    68  		"document_type": "cnpj",
    69  		"legal_name": "MUNDIPAGG TECNOLOGIA EM PAGAMENTOS S.A.",
    70  		"trade_name": "MUNDIPAGG TECNOLOGIA EM PAGAMENTOS S.A."
    71  	},
    72  	"created_at": "2021-07-01T14:32:21Z",
    73  	"created_by": "application:3279b005-5e40-41c1-996e-8cec24f8006b",
    74  	"customer": {
    75  		"document": "13621248773",
    76  		"document_type": "cpf",
    77  		"legal_name": "Matheus Palanowski",
    78  		"trade_name": null
    79  	},
    80  	"discounts": [],
    81  	"expiration_date": "2022-05-30",
    82  	"expired_at": null,
    83  	"fee": 0,
    84  	"fee_metadata": {
    85  		"billing_exemption_participant": true,
    86  		"fee": 0,
    87  		"max_free": 5,
    88  		"original_fee": 200,
    89  		"remaining_free": 5
    90  	},
    91  	"fine": null,
    92  	"id": "46e902e7-05e6-4efb-a28f-cf8b16ce9eed",
    93  	"interest": null,
    94  	"invoice_type": "bill_of_exchange",
    95  	"issuance_date": "2021-07-01",
    96  	"limit_date": "2022-05-30",
    97  	"our_number": "",
    98  	"receiver": null,
    99  	"registered_at": null,
   100  	"settled_at": null,
   101  	"status": "CREATED",
   102  	"writable_line": "19790000053891166005827056320420990010000005000"
   103  }
   104  `
   105  
   106  const successWithFineOnly = `
   107  {
   108      "account_id": "7e785e98-c859-46c6-9dc7-37ea522ceadf",
   109      "amount": 5000,
   110      "barcode": "19791895800000050000000088115105980288509901",
   111      "beneficiary": {
   112          "account_code": "88266374",
   113          "address": {
   114              "city": "Rio de Janeiro",
   115              "country": "Brasil",
   116              "extra": null,
   117              "neighborhood": "Vila Isabel",
   118              "postal_code": "20551080",
   119              "state": "RJ",
   120              "street": "Rua Visconde de Abaete",
   121              "street_number": "97"
   122          },
   123          "branch_code": "1",
   124          "document": "92961229022",
   125          "document_type": "cpf",
   126          "legal_name": "Nome do CPF 92961229022",
   127          "trade_name": null
   128      },
   129      "cancelled_at": null,
   130      "cancelled_reason": null,
   131      "cancelled_reason_description": null,
   132      "created_at": "2022-04-12T17:47:33Z",
   133      "created_by": "application:3279b005-5e40-41c1-996e-8cec24f8006b",
   134      "customer": {
   135          "document": "13621248773",
   136          "document_type": "cpf",
   137          "legal_name": "Matheus Palanowski",
   138          "trade_name": null
   139      },
   140      "discounts": [],
   141      "expiration_date": "2022-04-17",
   142      "expired_at": null,
   143      "fee": 0,
   144      "fee_metadata": {
   145          "billing_exemption_participant": true,
   146          "fee": 0,
   147          "max_free": 5,
   148          "original_fee": 200,
   149          "remaining_free": 5
   150      },
   151      "fine": {
   152          "date": "2022-04-18",
   153          "value": "2.0"
   154      },
   155      "id": "ea95560a-6455-4cf9-83dc-af11f9ccf734",
   156      "interest": null,
   157      "invoice_type": "bill_of_exchange",
   158      "issuance_date": "2022-04-12",
   159      "limit_date": "2022-04-22",
   160      "our_number": "88115105980288509901",
   161      "receiver": null,
   162      "registered_at": null,
   163      "settled_at": null,
   164      "status": "CREATED",
   165      "writable_line": "19790000058811510598802885099016189580000005000"
   166  }
   167  `
   168  
   169  const failedFineValue = `
   170  {
   171      "reason": [
   172          {
   173              "error": "Percentage must be equal or lower than 2.0",
   174              "path": [
   175                  "fine",
   176                  "value"
   177              ]
   178          }
   179      ],
   180      "type": "srn:error:validation"
   181  }
   182  `
   183  
   184  const failedFineDate = `
   185  {
   186      "reason": [
   187          {
   188              "error": "fine date should be greater than expiration date",
   189              "path": [
   190                  "fine"
   191              ]
   192          }
   193      ],
   194      "type": "srn:error:validation"
   195  }
   196  `
   197  
   198  const successWithInterestOnly = `
   199  {
   200      "account_id": "7e785e98-c859-46c6-9dc7-37ea522ceadf",
   201      "amount": 5000,
   202      "barcode": "19797895800000050000000015924667218367048068",
   203      "beneficiary": {
   204          "account_code": "88266374",
   205          "address": {
   206              "city": "Rio de Janeiro",
   207              "country": "Brasil",
   208              "extra": null,
   209              "neighborhood": "Vila Isabel",
   210              "postal_code": "20551080",
   211              "state": "RJ",
   212              "street": "Rua Visconde de Abaete",
   213              "street_number": "97"
   214          },
   215          "branch_code": "1",
   216          "document": "92961229022",
   217          "document_type": "cpf",
   218          "legal_name": "Nome do CPF 92961229022",
   219          "trade_name": null
   220      },
   221      "cancelled_at": null,
   222      "cancelled_reason": null,
   223      "cancelled_reason_description": null,
   224      "created_at": "2022-04-12T19:15:20Z",
   225      "created_by": "application:3279b005-5e40-41c1-996e-8cec24f8006b",
   226      "customer": {
   227          "document": "13621248773",
   228          "document_type": "cpf",
   229          "legal_name": "Matheus Palanowski",
   230          "trade_name": null
   231      },
   232      "discounts": [],
   233      "expiration_date": "2022-04-17",
   234      "expired_at": null,
   235      "fee": 0,
   236      "fee_metadata": {
   237          "billing_exemption_participant": true,
   238          "fee": 0,
   239          "max_free": 5,
   240          "original_fee": 200,
   241          "remaining_free": 5
   242      },
   243      "fine": null,
   244      "id": "b4541f0a-0e76-4bc4-82bd-2897b1ca1563",
   245      "interest": {
   246          "date": "2022-04-18",
   247          "value": "1.0"
   248      },
   249      "invoice_type": "bill_of_exchange",
   250      "issuance_date": "2022-04-12",
   251      "limit_date": "2022-04-22",
   252      "our_number": "15924667218367048068",
   253      "receiver": null,
   254      "registered_at": null,
   255      "settled_at": null,
   256      "status": "CREATED",
   257      "writable_line": "19790000051592466721383670480686789580000005000"
   258  }
   259  `
   260  
   261  const failedInterestValue = `
   262  {
   263      "reason": [
   264          {
   265              "error": "Percentage must be equal or lower than 1.0",
   266              "path": [
   267                  "interest",
   268                  "value"
   269              ]
   270          }
   271      ],
   272      "type": "srn:error:validation"
   273  }
   274  `
   275  
   276  const failedInterestDate = `
   277  {
   278      "reason": [
   279          {
   280              "error": "interest date should be greater than expiration date",
   281              "path": [
   282                  "interest"
   283              ]
   284          }
   285      ],
   286      "type": "srn:error:validation"
   287  }
   288  `
   289  
   290  const successWithFineAndInterest = `
   291  {
   292      "account_id": "7e785e98-c859-46c6-9dc7-37ea522ceadf",
   293      "amount": 5000,
   294      "barcode": "19798895800000050000000037485189119692884798",
   295      "beneficiary": {
   296          "account_code": "88266374",
   297          "address": {
   298              "city": "Rio de Janeiro",
   299              "country": "Brasil",
   300              "extra": null,
   301              "neighborhood": "Vila Isabel",
   302              "postal_code": "20551080",
   303              "state": "RJ",
   304              "street": "Rua Visconde de Abaete",
   305              "street_number": "97"
   306          },
   307          "branch_code": "1",
   308          "document": "92961229022",
   309          "document_type": "cpf",
   310          "legal_name": "Nome do CPF 92961229022",
   311          "trade_name": null
   312      },
   313      "cancelled_at": null,
   314      "cancelled_reason": null,
   315      "cancelled_reason_description": null,
   316      "created_at": "2022-04-12T19:36:59Z",
   317      "created_by": "application:3279b005-5e40-41c1-996e-8cec24f8006b",
   318      "customer": {
   319          "document": "13621248773",
   320          "document_type": "cpf",
   321          "legal_name": "Matheus Palanowski",
   322          "trade_name": null
   323      },
   324      "discounts": [],
   325      "expiration_date": "2022-04-17",
   326      "expired_at": null,
   327      "fee": 0,
   328      "fee_metadata": {
   329          "billing_exemption_participant": true,
   330          "fee": 0,
   331          "max_free": 5,
   332          "original_fee": 200,
   333          "remaining_free": 5
   334      },
   335      "fine": {
   336          "date": "2022-04-18",
   337          "value": "2.0"
   338      },
   339      "id": "c073397a-b87c-4718-830a-eb50eb6d1f92",
   340      "interest": {
   341          "date": "2022-04-18",
   342          "value": "1.0"
   343      },
   344      "invoice_type": "bill_of_exchange",
   345      "issuance_date": "2022-04-12",
   346      "limit_date": "2022-04-22",
   347      "our_number": "37485189119692884798",
   348      "receiver": null,
   349      "registered_at": null,
   350      "settled_at": null,
   351      "status": "CREATED",
   352      "writable_line": "19790000053748518911496928847985889580000005000"
   353  }
   354  `
   355  
   356  const unauthenticated = `{ "type": "srn:error:unauthenticated" }`
   357  
   358  const unauthorized = `{ "type": "srn:error:unauthorized" }`
   359  
   360  const conflict = `{	"type": "srn:error:conflict"  }`
   361  
   362  const unprocessableEntity = `{"reason":"barcode_payment_invoice_bill_of_exchange is not ena bled on this account","type":"srn:error:product_not_enabled"}`
   363  
   364  const customer_doc_invalid = `{"reason":[{"error":"is invalid","path":["customer","document"]}],"type":"srn:error:validation"}`
   365  
   366  const customer_blank_name = `{"reason":[{"error":"can't be blank","path":["customer","legal_name"]}],"type":"srn:error:validation"}`
   367  
   368  const amount_not_allowed = `{"reason":[{"error":"not allowed","path":["amount"]}],"type": "srn:error:validation"}`
   369  
   370  const multipleValidationErrorsPath = `{"reason":[{"error":"is invalid","path":["receiver","document"]}],"type": "srn:error:validation"}`
   371  
   372  const multipleValidationErrorsReason = `{"reason":[{"error": "is invalid","path":["account_id"]},{"error": "not allowed","path":["amount"]}],"type":"srn:error:validation"}`
   373  
   374  const tkStone = `{
   375  	"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI4d3NUd3BhYTRJWUZIYWV5ZFRubnRoRC1UaVlCaU9kanNmOGx6RUlMR1hVIn0.eyJqdGkiOiIyZTlkNGZkMy0zN2M1LTRjOWUtYTJjYy1lMjQ1N2MxZDgyMWQiLCJleHAiOjE2MjQ4OTQ1NDQsIm5iZiI6MCwiaWF0IjoxNjI0ODkzNjQ0LCJpc3MiOiJodHRwczovL3NhbmRib3gtYWNjb3VudHMub3BlbmJhbmsuc3RvbmUuY29tLmJyL2F1dGgvcmVhbG1zL3N0b25lX2JhbmsiLCJzdWIiOiJkNDY0ZDg3MC1mYzc2LTRjZGMtYWM5OC1hNjcyYjYyOTdhOGYiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiIzMjc5YjAwNS01ZTQwLTQxYzEtOTk2ZS04Y2VjMjRmODAwNmIiLCJhdXRoX3RpbWUiOjAsInNlc3Npb25fc3RhdGUiOiJhM2MyYzY3OC0wOGIxLTRmNmQtYmQ2Yi0wNjgzMjQ1M2UzNmMiLCJhY3IiOiIxIiwic2NvcGUiOiJwYXltZW50YWNjb3VudDpwYXltZW50bGlua3M6d3JpdGUgcGF5bWVudGFjY291bnQ6Y29udGFjdDp3cml0ZSBwaXg6cGF5bWVudF9pbnZvaWNlIHBpeDpwYXltZW50IHBpeDplbnRyeV9jbGFpbSBwYXltZW50YWNjb3VudDpyZWFkIHBpeDplbnRyeSBwYXltZW50YWNjb3VudDp0cmFuc2ZlcnM6aW50ZXJuYWwgcGF5bWVudGFjY291bnQ6ZmVlczpyZWFkIHBheW1lbnRhY2NvdW50OnBheW1lbnRzIHN0b25lX3N1YmplY3RfaWQgcGF5bWVudGFjY291bnQ6Y29udGFjdDpyZWFkIHNpZ251cDpwYXltZW50YWNjb3VudCBwYXltZW50YWNjb3VudDpib2xldG9pc3N1YW5jZSBwYXltZW50YWNjb3VudDpwYXltZW50bGlua3M6cmVhZCBwYXltZW50YWNjb3VudDp0cmFuc2ZlcnM6ZXh0ZXJuYWwiLCJjbGllbnRJZCI6IjMyNzliMDA1LTVlNDAtNDFjMS05OTZlLThjZWMyNGY4MDA2YiIsImNsaWVudEhvc3QiOiIxMC4xMC4zLjE3MiIsInN0b25lX3N1YmplY3RfaWQiOiJhcHBsaWNhdGlvbjozMjc5YjAwNS01ZTQwLTQxYzEtOTk2ZS04Y2VjMjRmODAwNmIiLCJjbGllbnRBZGRyZXNzIjoiMTAuMTAuMy4xNzIifQ.JloXzaTUFW0IVDi191U_WujRLhIIPiZUZngDb1nbhHo9mclG176CIgdSsBPmoOZr35ry47JCLgEq5ZAos8Sts72kpi1BivvVq0rJn5_NrmSyb0zqMSK4sNYzbhBafK7U6wamUZCjDeJmQ_wBUDNvPxGC1gToreMFnhrbak0pQr_CWp9Csgkn-9QUvFFpTRkJ3fdca57YnKoGsEWJWMs8Suq6g097244EWHISlUtO1ZGt01mypDeU8g5Z_eYD8qdN_woUeCGL86QhDoH-V8Dl_NIwbsHGTm8iRDTqjRBid2XH6Cj0RAMH10EpTKI8buSBzJ872bKLoCwXQQUnWIYT4Q",
   376  	"expires_in": 900,
   377  	"refresh_expires_in": 2700,
   378  	"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCIg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50YWNjb3VudDpwYXltZW50bGlua3M6cmVhZCBwYXltZW50YWNjb3VudDp0cmFuc2ZlcnM6ZXh0ZXJuYWwifQ.hXQSiQ-Bbto35TjlOdtnxUAbNysiS3TZIhqOpxV7A2s",
   379  	"token_type": "bearer",
   380  	"not-before-policy": 1620910623,
   381  	"session_state": "a3c2c678-08b1-4f6d-bd6b-06832453e36c",
   382  	"scope": "paymentaccount:paymentlinks:write paymentaccount:contact:write pix:payment_invoice pix:payment pix:entry_claim paymentaccount:read pix:entry paymentaccount:transfers:internal paymentaccount:fees:read paymentaccount:payments stone_subject_id paymentaccount:contact:read signup:paymentaccount paymentaccount:boletoissuance paymentaccount:paymentlinks:read paymentaccount:transfers:external"
   383  }`
   384  
   385  const specialCaractereError = `{"type":"srn:error:bad_request"}`
   386  
   387  func authStone(c *gin.Context) {
   388  	c.Data(200, "text/json", []byte(tkStone))
   389  }
   390  
   391  func registerStone(c *gin.Context) {
   392  	d, _ := ioutil.ReadAll(c.Request.Body)
   393  	json := string(d)
   394  
   395  	if strings.Contains(json, `amount": 201,`) {
   396  		c.Data(201, contentApplication, []byte(success))
   397  	} else if strings.Contains(json, `amount": 200,`) {
   398  		c.Data(201, contentApplication, []byte(successWithoutOurNumber))
   399  	} else if strings.Contains(json, `amount": 202,`) {
   400  		c.Data(201, contentApplication, []byte(successWithFineOnly))
   401  	} else if strings.Contains(json, `amount": 203,`) {
   402  		c.Data(201, contentApplication, []byte(successWithInterestOnly))
   403  	} else if strings.Contains(json, `amount": 204,`) {
   404  		c.Data(201, contentApplication, []byte(successWithFineAndInterest))
   405  	} else if strings.Contains(json, `amount": 401,`) {
   406  		c.Data(401, contentApplication, []byte(unauthenticated))
   407  	} else if strings.Contains(json, `amount": 403,`) {
   408  		c.Data(403, contentApplication, []byte(unauthorized))
   409  	} else if strings.Contains(json, `amount": 409,`) {
   410  		c.Data(409, contentApplication, []byte(conflict))
   411  	} else if strings.Contains(json, `amount": 422,`) {
   412  		c.Data(422, contentApplication, []byte(unprocessableEntity))
   413  	} else if strings.Contains(json, `amount": 301,`) {
   414  		c.Data(400, contentApplication, []byte(failedFineValue))
   415  	} else if strings.Contains(json, `amount": 302,`) {
   416  		c.Data(400, contentApplication, []byte(failedInterestValue))
   417  	} else if strings.Contains(json, `amount": 303,`) {
   418  		c.Data(400, contentApplication, []byte(failedFineDate))
   419  	} else if strings.Contains(json, `amount": 304,`) {
   420  		c.Data(400, contentApplication, []byte(failedInterestDate))
   421  	} else if strings.Contains(json, `amount": 4001,`) {
   422  		c.Data(400, contentApplication, []byte(customer_doc_invalid))
   423  	} else if strings.Contains(json, `amount": 4002,`) {
   424  		c.Data(400, contentApplication, []byte(customer_blank_name))
   425  	} else if strings.Contains(json, `amount": 4003,`) {
   426  		c.Data(400, contentApplication, []byte(amount_not_allowed))
   427  	} else if strings.Contains(json, `amount": 4004,`) {
   428  		c.Data(400, contentApplication, []byte(multipleValidationErrorsPath))
   429  	} else if strings.Contains(json, `amount": 4005,`) {
   430  		c.Data(400, contentApplication, []byte(multipleValidationErrorsReason))
   431  	} else if strings.Contains(json, `amount": 504,`) {
   432  		time.Sleep(35 * time.Second)
   433  		c.Data(504, contentApplication, []byte("timeout"))
   434  	} else if strings.Contains(json, `amount": 505,`) {
   435  		c.Data(500, contentApplication, []byte(specialCaractereError))
   436  	} else {
   437  		c.Data(401, contentApplication, []byte(unauthenticated))
   438  	}
   439  }