diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000..78ef8b7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: go +sudo: false +go: + - 1.8 +before_install: + - go get -t ./... + - go get github.com/mattn/goveralls +script: + - $GOPATH/bin/goveralls -service=travis-ci \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 02cf0a9..b05dad7 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +# 1.1.0 + +- Added: new endpoint (one step) + +# 1.0.3 + +- Added: new endpoint (settle charge) +- Added: new endpoint (settle parcel carnet) + +# 1.0.2 + +- Added: new endpoint (create charge balance sheet) + +# 1.0.1 + +- Added: new endpoint (update plan) +- Added: new endpoint (create subscription history) +- Updated: examples + # 1.0.0 -- Initial release +- Initial release \ No newline at end of file diff --git a/README.md b/README.md index b394ec5..632a900 100755 --- a/README.md +++ b/README.md @@ -1,75 +1,83 @@ -# gn-api-sdk-go - -> A go library for integration of your backend with the Gerencianet payment services. - -## Installation - -Install with: - -```bash -$ go get github.com/FilipeMata/gn-api-sdk-go/gerencianet -``` -## Tested with -``` -go 1.8 -``` -## Basic usage - -```go - -import ( - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" -) - -credentials := map[string]interface{} { - "client_id": "client_id", - "client_secret": "client_secret", - "sandbox": true, - "timeout": 10, -} - -gn := gerencianet.NewGerencianet(credentials) - -body = { - "items": [{ - "name": "Product 1", - "value": 1000, - "amount": 2, - }], - "shippings": [{ - "name": "Default Shipping Cost", - "value": 100, - }] -} - -res, err := gn.CreateCharge(body) - -``` - -## Examples - -You can run the examples inside `examples` with -`$ go run example.go`: - -```bash -$ go run charge/create_charge.go -``` - -Just remember to set the correct credentials inside `examples/configs.go` before running. - -## Tests - -To run the tests, just run: - -```bash -$ go test -``` - -## Changelog - -[CHANGELOG](CHANGELOG.md) - - -## License - -The library is available as open source under the terms of the [MIT License](LICENSE). +# gn-api-sdk-go + +> A go library for integration of your backend with the payment services +provided by [Gerencianet](http://gerencianet.com.br). + +[![Build Status](https://travis-ci.org/gerencianet/gn-api-sdk-go.svg)](https://travis-ci.org/gerencianet/gn-api-sdk-go) +[![Coverage Status](https://coveralls.io/repos/github/gerencianet/gn-api-sdk-go/badge.svg?branch=master)](https://coveralls.io/github/gerencianet/gn-api-sdk-go?branch=master) + +## Installation + +Install with: + +```bash + +$ go get github.com/gerencianet/gn-api-sdk-go/gerencianet +$ go mod init github.com/gerencianet/gn-api-sdk-go +``` +## Tested with +``` +go 1.8, 1.11.4 and 1.16.5 + +``` +## Basic usage + +```go + +import ( + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +credentials := map[string]interface{} { + "client_id": "client_id", + "client_secret": "client_secret", + "sandbox": true, + "timeout": 10, +} + +gn := gerencianet.NewGerencianet(credentials) + +body = { + "items": [{ + "name": "Product 1", + "value": 1000, + "amount": 2, + }], + "shippings": [{ + "name": "Default Shipping Cost", + "value": 100, + }] +} + +res, err := gn.CreateCharge(body) + +``` + +## Examples + +You can run the examples inside `_examples` with +`$ go run example.go`: + +```bash +$ go run charge/create_charge.go +``` + +Just remember to set the correct credentials inside `examples/configs.go` before running. + +## Tests + +To run the tests, just run: + +```bash +$ go test +``` + +## Changelog + +[CHANGELOG](CHANGELOG.md) + + +## License + +The library is available as open source under the terms of the [MIT License](LICENSE). diff --git a/examples/configs/configs.go b/examples/configs/configs.go index 3427f44..fe567e1 100755 --- a/examples/configs/configs.go +++ b/examples/configs/configs.go @@ -1,8 +1,10 @@ package configs var Credentials = map[string]interface{} { - "client_id": "Your Client_Id", - "client_secret": "Your Client_Secret", - "sandbox": true, + "client_id": "Client_Id_cc838f2acc53d20df92a4211a768c10c392f96ac", + "client_secret": "Client_Secret_74ec12aed00def635deca9a19cf6e24b1010c721", + "sandbox": false, "timeout": 20, + "CA" : "", + "Key" : "", } \ No newline at end of file diff --git a/examples/carnet/cancel_carnet.go b/examples/default/carnet/cancel_carnet.go similarity index 68% rename from examples/carnet/cancel_carnet.go rename to examples/default/carnet/cancel_carnet.go index 85db588..5b2c314 100755 --- a/examples/carnet/cancel_carnet.go +++ b/examples/default/carnet/cancel_carnet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/cancel_parcel.go b/examples/default/carnet/cancel_parcel.go similarity index 70% rename from examples/carnet/cancel_parcel.go rename to examples/default/carnet/cancel_parcel.go index 57d73cf..a8877c3 100755 --- a/examples/carnet/cancel_parcel.go +++ b/examples/default/carnet/cancel_parcel.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/create_carnet.go b/examples/default/carnet/create_carnet.go similarity index 81% rename from examples/carnet/create_carnet.go rename to examples/default/carnet/create_carnet.go index b523101..7325992 100755 --- a/examples/carnet/create_carnet.go +++ b/examples/default/carnet/create_carnet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/create_carnet_history.go b/examples/default/carnet/create_carnet_history.go similarity index 74% rename from examples/carnet/create_carnet_history.go rename to examples/default/carnet/create_carnet_history.go index 9783c5d..0526f04 100755 --- a/examples/carnet/create_carnet_history.go +++ b/examples/default/carnet/create_carnet_history.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/detail_carnet.go b/examples/default/carnet/detail_carnet.go similarity index 68% rename from examples/carnet/detail_carnet.go rename to examples/default/carnet/detail_carnet.go index c1ee664..6e82eb4 100755 --- a/examples/carnet/detail_carnet.go +++ b/examples/default/carnet/detail_carnet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/resend_carnet.go b/examples/default/carnet/resend_carnet.go similarity index 73% rename from examples/carnet/resend_carnet.go rename to examples/default/carnet/resend_carnet.go index 434cfa3..a75e3e8 100755 --- a/examples/carnet/resend_carnet.go +++ b/examples/default/carnet/resend_carnet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/resend_parcel.go b/examples/default/carnet/resend_parcel.go similarity index 75% rename from examples/carnet/resend_parcel.go rename to examples/default/carnet/resend_parcel.go index 47ce967..aeca78f 100755 --- a/examples/carnet/resend_parcel.go +++ b/examples/default/carnet/resend_parcel.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/default/carnet/settle_parcel.go b/examples/default/carnet/settle_parcel.go new file mode 100644 index 0000000..9e34fe1 --- /dev/null +++ b/examples/default/carnet/settle_parcel.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + res, err := gn.SettleCarnetParcel(1, 1) // no lugar dos 1s coloque o carnet_id e o numero da parcela respectivamente + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/carnet/update_carnet_metadata.go b/examples/default/carnet/update_carnet_metadata.go similarity index 75% rename from examples/carnet/update_carnet_metadata.go rename to examples/default/carnet/update_carnet_metadata.go index 701c9dc..0cf6911 100755 --- a/examples/carnet/update_carnet_metadata.go +++ b/examples/default/carnet/update_carnet_metadata.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/carnet/update_parcel.go b/examples/default/carnet/update_parcel.go similarity index 74% rename from examples/carnet/update_parcel.go rename to examples/default/carnet/update_parcel.go index 9649a5f..35068a9 100755 --- a/examples/carnet/update_parcel.go +++ b/examples/default/carnet/update_parcel.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/billet.go b/examples/default/charge/billet.go similarity index 80% rename from examples/charge/billet.go rename to examples/default/charge/billet.go index 7cf80f9..a1155a8 100755 --- a/examples/charge/billet.go +++ b/examples/default/charge/billet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/cancel_charge.go b/examples/default/charge/cancel_charge.go similarity index 68% rename from examples/charge/cancel_charge.go rename to examples/default/charge/cancel_charge.go index 491804b..4d0d4b3 100755 --- a/examples/charge/cancel_charge.go +++ b/examples/default/charge/cancel_charge.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/card.go b/examples/default/charge/card.go similarity index 85% rename from examples/charge/card.go rename to examples/default/charge/card.go index 4604da4..310e330 100755 --- a/examples/charge/card.go +++ b/examples/default/charge/card.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/create_charge.go b/examples/default/charge/create_charge.go similarity index 77% rename from examples/charge/create_charge.go rename to examples/default/charge/create_charge.go index cc8359f..55a0f23 100755 --- a/examples/charge/create_charge.go +++ b/examples/default/charge/create_charge.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/default/charge/create_charge_balance_sheet.go b/examples/default/charge/create_charge_balance_sheet.go new file mode 100755 index 0000000..9640295 --- /dev/null +++ b/examples/default/charge/create_charge_balance_sheet.go @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} { + "title": "Balancete Demonstrativo", + "body": []map[string]interface{} { + { + "header": "Demonstrativo de Consumo", + "tables": []map[string]interface{} { + { + "rows": [][]map[string]interface{} { + { + { + "align": "left", + "color": "#000000", + "style": "bold", + "text": "Exemplo de despesa", + "colspan": 2, + }, + { + "align": "left", + "color": "#000000", + "style": "bold", + "text": "Total lançado", + "colspan": 2, + }, + }, + { + { + "align": "left", + "color": "#000000", + "style": "normal", + "text": "Instalação", + "colspan": 2, + }, + { + "align": "left", + "color": "#000000", + "style": "normal", + "text": "R$ 100,00", + "colspan": 2, + }, + }, + }, + }, + }, + }, + { + "header": "Balancete Geral", + "tables": []map[string]interface{} { + { + "rows": [][]map[string]interface{} { + { + { + "align": "left", + "color": "#000000", + "style": "normal", + "text": "Confira na documentação da Gerencianet todas as configurações possíveis de um boleto balancete.", + "colspan": 4, + }, + }, + }, + }, + }, + }, + }, + } + + res, err := gn.CreateChargeBalanceSheet(1, body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/charge/create_charge_history.go b/examples/default/charge/create_charge_history.go similarity index 74% rename from examples/charge/create_charge_history.go rename to examples/default/charge/create_charge_history.go index 1035ac2..b6d7d13 100755 --- a/examples/charge/create_charge_history.go +++ b/examples/default/charge/create_charge_history.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/default/charge/create_charge_onestep.go b/examples/default/charge/create_charge_onestep.go new file mode 100755 index 0000000..2f09632 --- /dev/null +++ b/examples/default/charge/create_charge_onestep.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + customer := map[string]interface{}{ + "name": "Gorbadoc Oldbuck", + "cpf": "04267484171", + "phone_number": "51944916523", + "email": "gorb.oldbuck@gerencianet.com.br", + } + + body := map[string]interface{} { + "payment": map[string]interface{} { + "banking_billet": map[string]interface{} { + "expire_at": "2020-12-12", + "customer": customer, + }, + }, + "items": []map[string]interface{}{ + { + "name": "Product 1", + "value": 1000, + "amount": 2, + }, + }, + "shippings": []map[string]interface{} { + { + "name": "Default Shipping Cost", + "value": 100, + }, + }, + } + + res, err := gn.CreateChargeOneStep(body) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/default/charge/create_charge_onestep_billet_marketplace.go b/examples/default/charge/create_charge_onestep_billet_marketplace.go new file mode 100755 index 0000000..8ea7670 --- /dev/null +++ b/examples/default/charge/create_charge_onestep_billet_marketplace.go @@ -0,0 +1,64 @@ +package main + import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + repassOne := map[string]interface{}{ + "payee_code": "informe_payee_code_conta", // identificador da conta Gerencianet (repasse 1) + "percentage": 2500, // porcentagem de repasse (2500 = 25%) + } + + repassTwo := map[string]interface{}{ + "payee_code": "informe_payee_code_conta", // identificador da conta Gerencianet (repasse 2) + "percentage": 1500, // porcentagem de repasse (1500 = 15%) + } + + customer := map[string]interface{}{ + "name": "Gorbadoc Oldbuck", + "cpf": "04267484171", + "phone_number": "51944916523", + } + + body := map[string]interface{} { + "payment": map[string]interface{} { + "banking_billet": map[string]interface{} { + "expire_at": "2019-09-13", + "customer": customer, + }, + }, + "items": []map[string]interface{}{ + { + "name": "Product 1", + "value": 500, + "amount": 1, + "marketplace": map[string]interface{}{ + "repasses": []map[string]interface{}{ + repassOne, + repassTwo, + }, + }, + }, + }, + "shippings": []map[string]interface{} { + { + "name": "Default Shipping Cost", + "value": 100, + }, + }, + } + + res, err := gn.CreateChargeOneStep(body) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} diff --git a/examples/default/charge/create_charge_onestep_credit_card.go b/examples/default/charge/create_charge_onestep_credit_card.go new file mode 100755 index 0000000..f15f658 --- /dev/null +++ b/examples/default/charge/create_charge_onestep_credit_card.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + paymentToken := "428d7f3b2dc49117552ace464078557832c4ae4f"; + + customer := map[string]interface{}{ + "name": "Gorbadoc Oldbuck", + "cpf": "04267484171", + "phone_number": "51944916523", + "email": "gorb.oldbuck@gerencianet.com.br", + "birth": "1977-01-15", + } + + billingAddress := map[string]interface{} { + "street": "Av JK", + "number": 909, + "neighborhood": "Bauxita", + "zipcode": "35400000", + "city": "Ouro Preto", + "state": "MG", + } + + body := map[string]interface{} { + "payment": map[string]interface{} { + "credit_card": map[string]interface{} { + "installments": 1, + "billing_address": billingAddress, + "payment_token": paymentToken, + "customer": customer, + }, + }, + "items": []map[string]interface{}{ + { + "name": "Product 1", + "value": 1000, + "amount": 2, + }, + }, + "shippings": []map[string]interface{} { + { + "name": "Default Shipping Cost", + "value": 100, + }, + }, + } + + res, err := gn.CreateChargeOneStep(body) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/default/charge/create_charge_onestep_credit_card_marketplace.go b/examples/default/charge/create_charge_onestep_credit_card_marketplace.go new file mode 100755 index 0000000..456ae30 --- /dev/null +++ b/examples/default/charge/create_charge_onestep_credit_card_marketplace.go @@ -0,0 +1,79 @@ +package main + import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + paymentToken := "Insira_aqui_seu_paymentToken"; + + repassOne := map[string]interface{}{ + "payee_code": "informe_payee_code_conta", // identificador da conta Gerencianet (repasse 1) + "percentage": 2500, // porcentagem de repasse (2500 = 25%) + } + + repassTwo := map[string]interface{}{ + "payee_code": "informe_payee_code_conta", // identificador da conta Gerencianet (repasse 2) + "percentage": 1500, // porcentagem de repasse (1500 = 15%) + } + + customer := map[string]interface{}{ + "name": "Gorbadoc Oldbuck", + "cpf": "04267484171", + "phone_number": "51944916523", + "email": "gorb.oldbuck@gerencianet.com.br", + "birth": "1977-01-15", + } + + billingAddress := map[string]interface{} { + "street": "Av JK", + "number": 909, + "neighborhood": "Bauxita", + "zipcode": "35400000", + "city": "Ouro Preto", + "state": "MG", + } + + body := map[string]interface{} { + "payment": map[string]interface{} { + "credit_card": map[string]interface{} { + "installments": 1, + "billing_address": billingAddress, + "payment_token": paymentToken, + "customer": customer, + }, + }, + "items": []map[string]interface{}{ + { + "name": "Product 1", + "value": 500, + "amount": 1, + "marketplace": map[string]interface{}{ + "repasses": []map[string]interface{}{ + repassOne, + repassTwo, + }, + }, + }, + }, + "shippings": []map[string]interface{} { + { + "name": "Default Shipping Cost", + "value": 100, + }, + }, + } + + res, err := gn.CreateChargeOneStep(body) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} diff --git a/examples/charge/detail_charge.go b/examples/default/charge/detail_charge.go similarity index 68% rename from examples/charge/detail_charge.go rename to examples/default/charge/detail_charge.go index 7bc42af..02a7cfd 100755 --- a/examples/charge/detail_charge.go +++ b/examples/default/charge/detail_charge.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/link_charge.go b/examples/default/charge/link_charge.go similarity index 77% rename from examples/charge/link_charge.go rename to examples/default/charge/link_charge.go index 8130fde..319c8aa 100755 --- a/examples/charge/link_charge.go +++ b/examples/default/charge/link_charge.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/resend_billet.go b/examples/default/charge/resend_billet.go similarity index 73% rename from examples/charge/resend_billet.go rename to examples/default/charge/resend_billet.go index 81e4519..dfa3664 100755 --- a/examples/charge/resend_billet.go +++ b/examples/default/charge/resend_billet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/default/charge/settle_charge.go b/examples/default/charge/settle_charge.go new file mode 100644 index 0000000..27e8f94 --- /dev/null +++ b/examples/default/charge/settle_charge.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + res, err := gn.SettleCharge(1) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/charge/update_billet.go b/examples/default/charge/update_billet.go similarity index 72% rename from examples/charge/update_billet.go rename to examples/default/charge/update_billet.go index 68012b2..ca5937c 100755 --- a/examples/charge/update_billet.go +++ b/examples/default/charge/update_billet.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/update_charge_metadata.go b/examples/default/charge/update_charge_metadata.go similarity index 75% rename from examples/charge/update_charge_metadata.go rename to examples/default/charge/update_charge_metadata.go index 19cc00b..668e456 100755 --- a/examples/charge/update_charge_metadata.go +++ b/examples/default/charge/update_charge_metadata.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/charge/update_link_charge.go b/examples/default/charge/update_link_charge.go similarity index 77% rename from examples/charge/update_link_charge.go rename to examples/default/charge/update_link_charge.go index 1a22fc6..fd0d690 100755 --- a/examples/charge/update_link_charge.go +++ b/examples/default/charge/update_link_charge.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/extras/get_installments.go b/examples/default/extras/get_installments.go similarity index 67% rename from examples/extras/get_installments.go rename to examples/default/extras/get_installments.go index a16ad94..041578c 100755 --- a/examples/extras/get_installments.go +++ b/examples/default/extras/get_installments.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/extras/get_notification.go b/examples/default/extras/get_notification.go similarity index 68% rename from examples/extras/get_notification.go rename to examples/default/extras/get_notification.go index fae6796..317c732 100755 --- a/examples/extras/get_notification.go +++ b/examples/default/extras/get_notification.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/subscription/cancel_subscription.go b/examples/default/subscription/cancel_subscription.go similarity index 69% rename from examples/subscription/cancel_subscription.go rename to examples/default/subscription/cancel_subscription.go index 80930bb..8fabd9a 100755 --- a/examples/subscription/cancel_subscription.go +++ b/examples/default/subscription/cancel_subscription.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/subscription/create_plan.go b/examples/default/subscription/create_plan.go similarity index 70% rename from examples/subscription/create_plan.go rename to examples/default/subscription/create_plan.go index 7f3e720..bdaabf6 100755 --- a/examples/subscription/create_plan.go +++ b/examples/default/subscription/create_plan.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/subscription/create_subscription.go b/examples/default/subscription/create_subscription.go similarity index 76% rename from examples/subscription/create_subscription.go rename to examples/default/subscription/create_subscription.go index 2f04630..a6392c0 100755 --- a/examples/subscription/create_subscription.go +++ b/examples/default/subscription/create_subscription.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/default/subscription/create_subscription_history.go b/examples/default/subscription/create_subscription_history.go new file mode 100755 index 0000000..ec61b05 --- /dev/null +++ b/examples/default/subscription/create_subscription_history.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} { + "description": "This subscription was not fully paid", + } + + res, err := gn.CreateSubscriptionHistory(13100, body) // no lugar do 1 coloque o subscription_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/subscription/delete_plan.go b/examples/default/subscription/delete_plan.go similarity index 68% rename from examples/subscription/delete_plan.go rename to examples/default/subscription/delete_plan.go index 2e20fbc..00db42f 100755 --- a/examples/subscription/delete_plan.go +++ b/examples/default/subscription/delete_plan.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/subscription/detail_subscription.go b/examples/default/subscription/detail_subscription.go similarity index 69% rename from examples/subscription/detail_subscription.go rename to examples/default/subscription/detail_subscription.go index ce5f11b..21001f3 100755 --- a/examples/subscription/detail_subscription.go +++ b/examples/default/subscription/detail_subscription.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/subscription/list_plans.go b/examples/default/subscription/list_plans.go similarity index 66% rename from examples/subscription/list_plans.go rename to examples/default/subscription/list_plans.go index da9bad4..c1897e3 100755 --- a/examples/subscription/list_plans.go +++ b/examples/default/subscription/list_plans.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/subscription/pay_subscription.go b/examples/default/subscription/pay_subscription.go similarity index 80% rename from examples/subscription/pay_subscription.go rename to examples/default/subscription/pay_subscription.go index 78f557b..765c99f 100755 --- a/examples/subscription/pay_subscription.go +++ b/examples/default/subscription/pay_subscription.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/default/subscription/update_plan.go b/examples/default/subscription/update_plan.go new file mode 100755 index 0000000..681c7ae --- /dev/null +++ b/examples/default/subscription/update_plan.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} { + "name": "My new plan", + } + + res, err := gn.UpdatePlan(1, body) // no lugar do 1 coloque o plan_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/subscription/update_subscription_metadata.go b/examples/default/subscription/update_subscription_metadata.go similarity index 76% rename from examples/subscription/update_subscription_metadata.go rename to examples/default/subscription/update_subscription_metadata.go index ae5c52a..95ddbf0 100755 --- a/examples/subscription/update_subscription_metadata.go +++ b/examples/default/subscription/update_subscription_metadata.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "github.com/FilipeMata/gn-api-sdk-go/gerencianet" - "github.com/FilipeMata/gn-api-sdk-go/examples/configs" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" ) func main(){ diff --git a/examples/gn/account/balance.go b/examples/gn/account/balance.go new file mode 100755 index 0000000..2343ae0 --- /dev/null +++ b/examples/gn/account/balance.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + res, err := gn.Balance(nil) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/gn/account/listSettings.go b/examples/gn/account/listSettings.go new file mode 100755 index 0000000..5034a96 --- /dev/null +++ b/examples/gn/account/listSettings.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + res, err := gn.ListSettings(nil) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/gn/account/updateSettings.go b/examples/gn/account/updateSettings.go new file mode 100755 index 0000000..acb20de --- /dev/null +++ b/examples/gn/account/updateSettings.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + + body := map[string]interface{} { + + "pix": map[string]interface{}{ + "receberSemChave": false, + "chaves": map[string]interface{} { + "48044e07-e215-417f-b1ad-32ee2d99c2bc":map[string]interface{} { + "recebimento":map[string]interface{} { + "txidObrigatorio": false, + "qrCodeEstatico":map[string]interface{} { + "recusarTodos": false, + + }, + + }, + + }, + + }, + + }, + + } + + res, err := gn.UpdateSettings(body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/gn/key/create.go b/examples/gn/key/create.go new file mode 100755 index 0000000..e44bb7b --- /dev/null +++ b/examples/gn/key/create.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} {} + + + res, err := gn.CreateKey(body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/gn/key/delete.go b/examples/gn/key/delete.go new file mode 100755 index 0000000..4a2e8fe --- /dev/null +++ b/examples/gn/key/delete.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} {} + const key = "48044e07-e215-417f-b1ad-32ee2d99c2bc" + + res, err := gn.DeleteKey(key, body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/gn/key/list.go b/examples/gn/key/list.go new file mode 100755 index 0000000..ef22f12 --- /dev/null +++ b/examples/gn/key/list.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + res, err := gn.ListKey(nil) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/charge/createCharge.go b/examples/pix/charge/createCharge.go new file mode 100755 index 0000000..4362fe5 --- /dev/null +++ b/examples/pix/charge/createCharge.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const txid = "adssshdsjdsjeyccdyddsasdstxid29" + + body := map[string]interface{} { + + "calendario": map[string]interface{} { + "expiracao": 3600, + }, + "devedor": map[string]interface{}{ + + "cnpj": "12345678000195", + "nome": "Empresa de Serviços SA", + + }, + "valor": map[string]interface{} { + + "original": "00.01", + + }, + "chave": "48044e07-e215-417f-b1ad-32ee2d99c2bc", + "solicitacaoPagador": "Teste.", + "infoAdicionais": []map[string]interface{} { + { + "nome": "Campo 1", + "valor": "Informação Adicional1 do PSP-Recebedor", + }, + }, + } + + res, err := gn.CreateCharge(txid,body) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/charge/createImmediateCharge.go b/examples/pix/charge/createImmediateCharge.go new file mode 100755 index 0000000..100ee99 --- /dev/null +++ b/examples/pix/charge/createImmediateCharge.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + + body := map[string]interface{} { + + "calendario": map[string]interface{} { + "expiracao": 3600, + }, + "devedor": map[string]interface{}{ + + "cpf": "12345678909", + "nome": "Francisco da Silva", + + }, + "valor": map[string]interface{} { + + "original": "00.01", + + }, + "chave": "48044e07-e215-417f-b1ad-32ee2d99c2bc", + "solicitacaoPagador": "Teste.", + "infoAdicionais": []map[string]interface{} { + { + "nome": "Campo 1", + "valor": "Informação Adicional1 do PSP-Recebedor", + }, + }, + } + + res, err := gn.CreateImmediateCharge(body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/charge/detailCharge.go b/examples/pix/charge/detailCharge.go new file mode 100755 index 0000000..d3519d6 --- /dev/null +++ b/examples/pix/charge/detailCharge.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const txid = "adssshdsjdsjeyccdyddsasdstxid23" + + + + res, err := gn.DetailCharge(txid) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/charge/listCharges.go b/examples/pix/charge/listCharges.go new file mode 100755 index 0000000..7a4f491 --- /dev/null +++ b/examples/pix/charge/listCharges.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const inicio = "2021-03-01T03:01:35Z" + const fim = "2021-03-05T22:01:35Z" + + + res, err := gn.ListCharges(inicio, fim) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/location/createLoc.go b/examples/pix/location/createLoc.go new file mode 100755 index 0000000..480efa6 --- /dev/null +++ b/examples/pix/location/createLoc.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + + body := map[string]interface{} { + "tipoCob": "cob", + } + + res, err := gn.CreateLoc(body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/location/deleteTxid.go b/examples/pix/location/deleteTxid.go new file mode 100755 index 0000000..c8ac50a --- /dev/null +++ b/examples/pix/location/deleteTxid.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} {} + const id = "423" + + + res, err := gn.DeleteTxid(id, body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/location/generateQRCode.go b/examples/pix/location/generateQRCode.go new file mode 100755 index 0000000..5118ad1 --- /dev/null +++ b/examples/pix/location/generateQRCode.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + const id = "423" + + res, err := gn.GenerateQRCode(id) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/location/getLoc.go b/examples/pix/location/getLoc.go new file mode 100755 index 0000000..e5e7d2d --- /dev/null +++ b/examples/pix/location/getLoc.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + const id = "423" + + res, err := gn.GetLoc(id) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/location/listLoc.go b/examples/pix/location/listLoc.go new file mode 100755 index 0000000..e2c7727 --- /dev/null +++ b/examples/pix/location/listLoc.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const inicio = "2021-03-01T03:01:35Z" + const fim = "2021-03-05T22:01:35Z" + + + res, err := gn.ListLoc(inicio, fim) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/pix/createDevolution.go b/examples/pix/pix/createDevolution.go new file mode 100755 index 0000000..0c86a5b --- /dev/null +++ b/examples/pix/pix/createDevolution.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const e2eid = "E00416968202105211756Rh0iPsaJ1RK" + const id = "adssshdsjdsjeyccdyddsasdstxid29" + + body := map[string]interface{} { + "valor": "0.01", + } + + res, err := gn.CreateDevolution(e2eid,id,body) // no lugar do 1 coloque o charge_id certo + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/pix/devolutionList.go b/examples/pix/pix/devolutionList.go new file mode 100755 index 0000000..5a2a4f6 --- /dev/null +++ b/examples/pix/pix/devolutionList.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const e2eid = "E00416968202105211756Rh0iPsaJ1RK" + const id = "adssshdsjdsjeyccdyddsasdstxid29" + + + res, err := gn.DevolutionList(e2eid,id) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/pix/pixDetail.go b/examples/pix/pix/pixDetail.go new file mode 100755 index 0000000..82b7f6c --- /dev/null +++ b/examples/pix/pix/pixDetail.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const e2eid = "E00416968202105211756Rh0iPsaJ1RK" + + + + res, err := gn.PixDetail(e2eid) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/pix/pixList.go b/examples/pix/pix/pixList.go new file mode 100755 index 0000000..4d431a1 --- /dev/null +++ b/examples/pix/pix/pixList.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const inicio = "2021-03-01T03:01:35Z" + const fim = "2021-03-05T22:01:35Z" + + + res, err := gn.PixList(inicio, fim) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/pix/pixSend.go b/examples/pix/pix/pixSend.go new file mode 100755 index 0000000..8c8c62e --- /dev/null +++ b/examples/pix/pix/pixSend.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + + body := map[string]interface{} { + "valor": "10.00", + "pagador": map[string]interface{} { + "chave": "48044e07-e215-417f-b1ad-32ee2d99c2bc", + "infoPagador": "Segue o pagamento da conta", + }, + "favorecido": map[string]interface{} { + "chave": "620e2ddb-9746-4e0e-9350-dc6dff699224", + }, + } + + res, err := gn.PixSend(body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/webhook/delete.go b/examples/pix/webhook/delete.go new file mode 100755 index 0000000..77e4a7e --- /dev/null +++ b/examples/pix/webhook/delete.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + body := map[string]interface{} {} + const chave = "2c5c7441-a91e-4982-8c25-61e18ae" + + + + res, err := gn.DeleteWebhook(chave, body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/webhook/detail.go b/examples/pix/webhook/detail.go new file mode 100755 index 0000000..08354d0 --- /dev/null +++ b/examples/pix/webhook/detail.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const chave = "2c5c7441-a91e-4982-8c25-e18ae" + + + + res, err := gn.GetWebhook(chave) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/webhook/list.go b/examples/pix/webhook/list.go new file mode 100755 index 0000000..3c3a2de --- /dev/null +++ b/examples/pix/webhook/list.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const inicio = "2021-03-01T03:01:35Z" + const fim = "2021-06-05T22:01:35Z" + + + res, err := gn.ListWebhooks(inicio, fim) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/examples/pix/webhook/update.go b/examples/pix/webhook/update.go new file mode 100755 index 0000000..8cf0fe2 --- /dev/null +++ b/examples/pix/webhook/update.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "github.com/gerencianet/gn-api-sdk-go/gerencianet" + "github.com/gerencianet/gn-api-sdk-go/examples/configs" +) + +func main(){ + + credentials := configs.Credentials + gn := gerencianet.NewGerencianet(credentials) + + + const chave = "937e676f-583b-42df-82cd-fbb4" + + body := map[string]interface{} { + + "webhookUrl": "https://seu_webhook", + } + + res, err := gn.UpdateWebhook(chave,body) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + } +} \ No newline at end of file diff --git a/gerencianet/auth_internal_test.go b/gerencianet/auth_internal_test.go deleted file mode 100755 index 3a6ef01..0000000 --- a/gerencianet/auth_internal_test.go +++ /dev/null @@ -1,84 +0,0 @@ -package gerencianet - -import ( - "bytes" - "errors" - "io/ioutil" - "net/http" - "testing" -) - -type MockClient struct { - authorized bool - DoFunc func(req *http.Request) (*http.Response, error) -} - -func (m *MockClient) Do(req *http.Request) (*http.Response, error) { - if m.DoFunc != nil { - return m.DoFunc(req) - } - return &http.Response{}, errors.New("auth error") -} - -func Test_newAuth(t *testing.T) { - actualResult := newAuth("cidTest", "csTest", true, 10) - if actualResult.clientID != "cidTest" || - actualResult.clientSecret != "csTest" || - actualResult.sandbox != true || - actualResult.timeout != 10 { - t.Error("Error on constructor") - } -} - -func Test_getAccessTokenToReturnTokenData(t *testing.T) { - - httpClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewReader([]byte("test"))), - }, nil - }, - } - - auth := auth{"cidTest", "csTest", true, 10, httpClient} - _, err := auth.getAccessToken() - if err != nil { - t.Error("Expected err to be nil") - } -} - -func Test_getAccessTokenToReturnUnauthorizedError(t *testing.T) { - - httpClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: 401, - Status: "Unauthorized", - Body: ioutil.NopCloser(bytes.NewReader([]byte("test"))), - }, nil - }, - } - - auth := auth{"cidTest", "csTest", false, 10, httpClient} - expected := "Status: Unauthorized Could not authenticate. \nPlease make sure you are using correct credentials and if you are using then in the correct environment." - _, err := auth.getAccessToken() - if err.Error() != expected { - t.Errorf("Error actual = %v, and Expected = %v.", err, expected) - } -} - -func Test_getAccessTokenToReturnHttpRequestError(t *testing.T) { - - httpClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return nil, errors.New("Test") - }, - } - - auth := auth{"cidTest", "csTest", true, 10, httpClient} - _, err := auth.getAccessToken() - if err == nil { - t.Errorf("Expected error but got no one") - } -} diff --git a/gerencianet/auth_pix.go b/gerencianet/auth_pix.go new file mode 100755 index 0000000..2bc5331 --- /dev/null +++ b/gerencianet/auth_pix.go @@ -0,0 +1,86 @@ +package pix + +import ( + "bytes" + b64 "encoding/base64" + "encoding/json" + "errors" + "net/http" + "time" + "crypto/tls" +) + +type auth struct { + clientID, clientSecret string + CA, Key string + sandbox bool + timeout int + netClient interface { + Do(req *http.Request) (*http.Response, error) + } +} + +type authResponseBody struct { + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + ExpiresIn int `json:"expires_in"` + EspiresAt string `json:"expire_at"` + TokenType string `json:"token_type"` +} + + +func newAuth(clientID string, clientSecret string,CA string, Key string, sandbox bool, timeout int) *auth { + var cert, erro = tls.LoadX509KeyPair(CA, Key) + if erro != nil { + println("Falha ao carregar par de chaves",erro) + } + var netTransport = &http.Transport{ + TLSClientConfig: &tls.Config{ + Certificates: []tls.Certificate{cert}, + }, + } + httpClient := &http.Client{Timeout: time.Second * time.Duration(timeout),Transport: netTransport} + return &auth{clientID, clientSecret,CA, Key, sandbox, timeout, httpClient} +} + + + +func (auth auth) getAccessToken() (authResponseBody, error) { + var response authResponseBody + credentials := auth.clientID + ":" + auth.clientSecret + authCredentials := b64.StdEncoding.EncodeToString([]byte(credentials)) + postParams := map[string]string{"grant_type": "client_credentials"} + buffer := new(bytes.Buffer) + json.NewEncoder(buffer).Encode(postParams) + + var gnURL string + if auth.sandbox { + gnURL = UrlSandbox + } else { + gnURL = UrlProduction + } + req, _ := http.NewRequest("POST", gnURL+"/oauth/token", buffer) + req.Header.Add("Authorization", "Basic "+authCredentials) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("charset", "utf-8") + req.Header.Add("api-sdk", "go-" + Version) + + + res, resErr := auth.netClient.Do(req) + + + + + if resErr != nil { + return response, resErr + } + + if res.StatusCode != http.StatusOK { + return response, errors.New("Status: " + res.Status + " Could not authenticate. \nPlease make sure you are using correct credentials and if you are using then in the correct environment.") + } + + json.NewDecoder(res.Body).Decode(&response) + + return response, nil + +} \ No newline at end of file diff --git a/gerencianet/constants.go b/gerencianet/constants.go index 124fea3..caf5cde 100755 --- a/gerencianet/constants.go +++ b/gerencianet/constants.go @@ -1,6 +1,6 @@ package gerencianet -var Version = "1.0.0" +var Version = "1.1.0" const ( UrlSandbox = "https://sandbox.gerencianet.com.br/v1" diff --git a/gerencianet/constants_pix.go b/gerencianet/constants_pix.go new file mode 100755 index 0000000..90cfc83 --- /dev/null +++ b/gerencianet/constants_pix.go @@ -0,0 +1,8 @@ +package pix + +var Version = "2.0.0" + +const ( + UrlSandbox = "https://api-pix-h.gerencianet.com.br" + UrlProduction = "https://api-pix.gerencianet.com.br" +) diff --git a/gerencianet/endpoints.go b/gerencianet/endpoints.go index 7bf1f77..4c5fd7d 100755 --- a/gerencianet/endpoints.go +++ b/gerencianet/endpoints.go @@ -12,6 +12,10 @@ func (endpoints endpoints) CreateCharge(body map[string]interface{}) (string, er return endpoints.requester.request("/charge", "POST", nil, body) } +func (endpoints endpoints) CreateChargeOneStep(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/charge/one-step", "POST", nil, body) +} + func (endpoints endpoints) DetailCharge(chargeID int) (string, error) { params := map[string]string{ "id": strconv.Itoa(chargeID) } return endpoints.requester.request("/charge/:id", "GET", params, nil) @@ -163,4 +167,32 @@ func (endpoints endpoints) ChargeLink(chargeID int, body map[string]interface{}) func (endpoints endpoints) UpdateChargeLink(chargeID int, body map[string]interface{}) (string, error) { params := map[string]string{ "id": strconv.Itoa(chargeID) } return endpoints.requester.request("/charge/:id/link", "PUT", params, body) -} \ No newline at end of file +} + +func (endpoints endpoints) CreateSubscriptionHistory(subscriptionID int, body map[string]interface{}) (string, error) { + params := map[string]string{ "id": strconv.Itoa(subscriptionID) } + return endpoints.requester.request("/subscription/:id/history", "POST", params, body) +} + +func (endpoints endpoints) UpdatePlan(planID int, body map[string]interface{}) (string, error) { + params := map[string]string{ "id": strconv.Itoa(planID) } + return endpoints.requester.request("/plan/:id", "PUT", params, body) +} + +func (endpoints endpoints) CreateChargeBalanceSheet(chargeID int, body map[string]interface{}) (string, error) { + params := map[string]string{ "id": strconv.Itoa(chargeID) } + return endpoints.requester.request("/charge/:id/balance-sheet", "POST", params, body) +} + +func (endpoints endpoints) SettleCarnetParcel(carnetID int, parcel int) (string, error) { + params := map[string]string{ + "id": strconv.Itoa(carnetID), + "parcel": strconv.Itoa(parcel), + } + return endpoints.requester.request("/carnet/:id/parcel/:parcel/settle", "PUT", params, nil) +} + +func (endpoints endpoints) SettleCharge(chargeID int) (string, error) { + params := map[string]string{ "id": strconv.Itoa(chargeID) } + return endpoints.requester.request("/charge/:id/settle", "PUT", params, nil) +} diff --git a/gerencianet/endpoints_internal_test.go b/gerencianet/endpoints_internal_test.go deleted file mode 100755 index deb53b5..0000000 --- a/gerencianet/endpoints_internal_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package gerencianet - -import ( - "testing" -) - -type MockRequester struct {} - -func (m *MockRequester) request(endpoint string, httpVerb string, requestParams map[string]string, body map[string]interface{}) (string, error) { - return "", nil -} - -func Test_endpoints(t *testing.T) { - requester := &MockRequester{} - endpoints := endpoints{requester} - endpoints.CreateCharge(nil) - endpoints.DetailCharge(1) - endpoints.UpdateChargeMetadata(1, nil) - endpoints.UpdateBillet(1, nil) - endpoints.PayCharge(1, nil) - endpoints.CancelCharge(1) - endpoints.CreateCarnet(nil) - endpoints.DetailCarnet(1) - endpoints.UpdateParcel(1, 1, nil) - endpoints.UpdateCarnetMetadata(1, nil) - endpoints.GetNotification("") - endpoints.GetPlans(1, 1) - endpoints.CreatePlan(nil) - endpoints.DeletePlan(1) - endpoints.CreateSubscription(1, nil) - endpoints.DetailSubscription(1) - endpoints.PaySubscription(1, nil) - endpoints.CancelSubscription(1) - endpoints.UpdateSubscriptionMetadata(1, nil) - endpoints.GetInstallments(20000, "visa") - endpoints.ResendBillet(1, nil) - endpoints.CreateChargeHistory(1, nil) - endpoints.ResendCarnet(1, nil) - endpoints.ResendParcel(1, 1, nil) - endpoints.CreateCarnetHistory(1, nil) - endpoints.CancelCarnet(1) - endpoints.CancelParcel(1, 1) - endpoints.ChargeLink(1, nil) - endpoints.UpdateChargeLink(1, nil) - t.Skip("skipping endpoints tests") -} \ No newline at end of file diff --git a/gerencianet/endpoints_pix.go b/gerencianet/endpoints_pix.go new file mode 100755 index 0000000..6405202 --- /dev/null +++ b/gerencianet/endpoints_pix.go @@ -0,0 +1,130 @@ +package pix + + +type endpoints struct { + requester interface { + request(endpoint string, httpVerb string, requestParams map[string]string, body map[string]interface{}) (string, error) + } +} + + + + +func (endpoints endpoints) CreateImmediateCharge(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/cob", "POST", nil, body) +} + + +func (endpoints endpoints) CreateCharge(txid string, body map[string]interface{}) (string, error) { + params := map[string]string{ "txid": (txid) } + return endpoints.requester.request("/v2/cob/:txid", "PUT", params, body) +} + +func (endpoints endpoints) DetailCharge(txid string) (string, error) { + params := map[string]string{ "txid": (txid) } + return endpoints.requester.request("/v2/cob/:txid", "GET", params, nil) +} + +func (endpoints endpoints) ListCharges(inicio string, fim string) (string, error) { + params := map[string]string{ + "inicio": (inicio), + "fim": (fim), + } + return endpoints.requester.request("/v2/cob?inicio=:inicio&fim=:fim", "GET", params, nil) +} + +func (endpoints endpoints) CreateDevolution(e2eid string, id string, body map[string]interface{}) (string, error) { + params := map[string]string{ + "e2eid": (e2eid), + "id": (id), } + return endpoints.requester.request("/v2/pix/:e2eid/devolucao/:id", "PUT", params, body) +} + +func (endpoints endpoints) DevolutionList(e2eid string, id string) (string, error) { + params := map[string]string{ + "e2eid": (e2eid), + "id": (id), } + return endpoints.requester.request("/v2/pix/:e2eid/devolucao/:id", "GET", params, nil) +} + +func (endpoints endpoints) PixDetail(e2eid string) (string, error) { + params := map[string]string{ "e2eid": (e2eid) } + return endpoints.requester.request("/v2/pix/:e2eid", "GET", params, nil) +} + +func (endpoints endpoints) PixSend(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/pix", "POST", nil, body) +} + +func (endpoints endpoints) CreateLoc(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/loc", "POST", nil, body) +} + +func (endpoints endpoints) DeleteTxid(id string, body map[string]interface{}) (string, error) { + params := map[string]string{ "id": (id) } + return endpoints.requester.request("/v2/loc/:id/txid", "DELETE", params, body) +} + +func (endpoints endpoints) GetLoc(id string) (string, error) { + params := map[string]string{ "id": (id) } + return endpoints.requester.request("/v2/loc/:id", "GET", params, nil) +} + +func (endpoints endpoints) ListLoc(inicio string, fim string) (string, error) { + params := map[string]string{ + "inicio": (inicio), + "fim": (fim), + } + return endpoints.requester.request("/v2/loc?inicio=:inicio&fim=:fim", "GET", params, nil) +} + +func (endpoints endpoints) GenerateQRCode(id string) (string, error) { + params := map[string]string{ "id": (id) } + return endpoints.requester.request("/v2/loc/:id/qrcode", "GET", params, nil) +} + +func (endpoints endpoints) Balance(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/gn/saldo", "GET", nil, body) +} +func (endpoints endpoints) ListSettings(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/gn/config", "GET", nil, body) +} + +func (endpoints endpoints) UpdateSettings(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/gn/config", "PUT", nil, body) +} +func (endpoints endpoints) CreateKey(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/gn/evp", "POST", nil, body) +} + +func (endpoints endpoints) ListKey(body map[string]interface{}) (string, error) { + return endpoints.requester.request("/v2/gn/evp", "GET", nil, body) +} + +func (endpoints endpoints) DeleteKey(key string, body map[string]interface{}) (string, error) { + params := map[string]string{ "key": (key) } + return endpoints.requester.request("/v2/gn/evp/:key", "DELETE", params, body) +} + +func (endpoints endpoints) UpdateWebhook(chave string, body map[string]interface{}) (string, error) { + params := map[string]string{ "chave": (chave) } + return endpoints.requester.request("/v2/webhook/:chave", "PUT", params, body) +} + +func (endpoints endpoints) DeleteWebhook(chave string, body map[string]interface{}) (string, error) { + params := map[string]string{ "chave": (chave) } + return endpoints.requester.request("/v2/webhook/:chave", "DELETE", params, body) +} + +func (endpoints endpoints) GetWebhook(chave string) (string, error) { + params := map[string]string{ "chave": (chave) } + return endpoints.requester.request("/v2/webhook/:chave", "GET", params, nil) +} + +func (endpoints endpoints) ListWebhooks(inicio string, fim string) (string, error) { + params := map[string]string{ + "inicio": (inicio), + "fim": (fim), + } + return endpoints.requester.request("/v2/webhook?inicio=:inicio&fim=:fim", "GET", params, nil) +} \ No newline at end of file diff --git a/gerencianet/gerencianet_pix.go b/gerencianet/gerencianet_pix.go new file mode 100755 index 0000000..23e9099 --- /dev/null +++ b/gerencianet/gerencianet_pix.go @@ -0,0 +1,19 @@ +package pix + +type gerencianet struct { + endpoints +} + +func NewGerencianet(configs map[string]interface{}) *pix { + clientID := configs["client_id"].(string) + clientSecret := configs["client_secret"].(string) + CA := configs["CA"].(string) + Key := configs["Key"].(string) + sandbox := configs["sandbox"].(bool) + timeout := configs["timeout"].(int) + + requester := newRequester(clientID, clientSecret,CA, Key, sandbox, timeout) + gn := pix{} + gn.requester = *requester + return &gn +} \ No newline at end of file diff --git a/gerencianet/gerencianet_test.go b/gerencianet/gerencianet_test.go deleted file mode 100755 index c4c5b24..0000000 --- a/gerencianet/gerencianet_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package gerencianet - -import ( - "testing" -) - -func TestNewGerencianet(t *testing.T) { - params := map[string]interface{}{ - "client_id": "cidTest", - "client_secret": "csTest", - "sandbox": true, - "timeout": 10, - } - gn := NewGerencianet(params) - if gn == nil { - t.Error("Error on constructor") - } -} \ No newline at end of file diff --git a/gerencianet/requester_internal_test.go b/gerencianet/requester_internal_test.go deleted file mode 100755 index 2d38e87..0000000 --- a/gerencianet/requester_internal_test.go +++ /dev/null @@ -1,123 +0,0 @@ -package gerencianet - -import ( - "bytes" - "errors" - "io/ioutil" - "net/http" - "testing" - "time" -) - -type MockAuth struct { - authorized bool -} - -func (a *MockAuth) getAccessToken() (authResponseBody, error) { - var response authResponseBody - if a.authorized == false { - return response, errors.New("401 error") - } - response.AccessToken = "token_test" - return response, nil -} - -func Test_newRequestToReturnSandboxRequester(t *testing.T) { - requester := newRequester("cidTest", "csTest", true, 10) - if requester.url != UrlSandbox || - requester.timeout != 10 { - t.Error("Error on constructor") - } -} - -func Test_newRequestToReturnProductionRequester(t *testing.T) { - requester := newRequester("cidTest", "csTest", false, 10) - if requester.url != UrlProduction || - requester.timeout != 10 { - t.Error("Error on constructor") - } -} - -func Test_requestToReturnString(t *testing.T) { - - httpClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewReader([]byte(req.URL.String()))), - }, nil - }, - } - - auth := auth{"cidTest", "csTest", true, 10, httpClient} - requester := requester{auth, "sandbox.com", 10, "token", time.Time{}, httpClient} - params := map[string]string{"id": "1", "token": "2", "data": "test"} - requestBody := map[string]interface{} {"test": "test"} - - res, err := requester.request("/test/:id", "POST", params, requestBody) - if err != nil { - t.Error("Expected err to be nil") - } - if res != "sandbox.com/test/1?token=2&data=test" { - t.Error("Expected parse params on route") - } -} - -func Test_requestToReturnAuthError(t *testing.T) { - - httpClient := &MockClient{} - auth := auth{"cidTest", "csTest", true, 10, httpClient} - requester := requester{auth, "sandbox.com", 10, "token", time.Time{}, httpClient} - _, err := requester.request("/test", "GET", nil, nil) - if err.Error() != "auth error" { - t.Error("Expected err to be an auth error") - } -} - - -func Test_requestToReturnServerError(t *testing.T) { - authClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewReader([]byte("auth test"))), - }, nil - }, - } - requestClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: 500, - Body: ioutil.NopCloser(bytes.NewReader([]byte("erro 500 test"))), - }, nil - }, - } - auth := auth{"cidTest", "csTest", true, 10, authClient} - requester := requester{auth, "sandbox.com", 10, "token", time.Time{}, requestClient} - _, err := requester.request("test", "PUT", nil, nil) - if err.Error() != "erro 500 test" { - t.Error("Expected err to be an 500 http error") - } -} - -func Test_requestToReturnHttpDoError(t *testing.T) { - authClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewReader([]byte("auth test"))), - }, nil - }, - } - requestClient := &MockClient{ - DoFunc: func(req *http.Request) (*http.Response, error) { - return nil, errors.New("http Do error") - }, - } - auth := auth{"cidTest", "csTest", true, 10, authClient} - requester := requester{auth, "sandbox.com", 10, "token", time.Time{}, requestClient} - _, err := requester.request("test", "PUT", nil, nil) - if err.Error() != "http Do error" { - t.Error("Expected err to be an http/net error") - } -} diff --git a/gerencianet/requester_pix.go b/gerencianet/requester_pix.go new file mode 100755 index 0000000..6a218d9 --- /dev/null +++ b/gerencianet/requester_pix.go @@ -0,0 +1,124 @@ +package pix + +import ( + "bytes" + "encoding/json" + "errors" + "io/ioutil" + "net/http" + "net/url" + "regexp" + "strings" + "time" + "crypto/tls" +) + +type requester struct { + auth interface { + getAccessToken() (authResponseBody, error) + } + url string + timeout int + Token string + TokenDue time.Time + netClient interface { + Do(req *http.Request) (*http.Response, error) + } +} + +func newRequester(clientID string, clientSecret string, CA string, Key string, sandbox bool, timeout int) *requester { + auth := newAuth(clientID, clientSecret,CA, Key, sandbox, timeout) + var cert, _ = tls.LoadX509KeyPair(CA, Key) + + var netTransport = &http.Transport{ + TLSClientConfig: &tls.Config{ + Certificates: []tls.Certificate{cert}, + }, + } + httpClient := &http.Client{Timeout: time.Second * time.Duration(timeout),Transport: netTransport} + var gnURL string + if sandbox { + gnURL = UrlSandbox + } else { + gnURL = UrlProduction + } + return &requester{*auth, gnURL, timeout, "", time.Time{}, httpClient} +} + +func authenticate(requester *requester) (bool, error) { + if requester.Token == "" || requester.TokenDue.Before(time.Now()) { + tokenData, authErr := requester.auth.getAccessToken() + if authErr != nil { + return false, authErr + } + requester.Token = tokenData.AccessToken + requester.TokenDue = time.Now().Local().Add(time.Second * time.Duration(tokenData.ExpiresIn)) + } + return true, nil +} + +func (requester requester) request(endpoint string, httpVerb string, requestParams map[string]string, body map[string]interface{}) (string, error) { + requestBody := new(bytes.Buffer) + json.NewEncoder(requestBody).Encode(body) + + _, authErr := authenticate(&requester) + if authErr != nil { + _, authErr = authenticate(&requester) + } + if authErr != nil { + return "", authErr + } + + route := getRoute(endpoint, requestParams) + route += getQueryString(requestParams) + req, _ := http.NewRequest(httpVerb, requester.url+route, requestBody) + + if ( httpVerb == "POST" || httpVerb == "PUT" ) && body != nil { + req.Header.Add("Content-Type", "application/json") + } + req.Header.Add("accept", "application/json") + req.Header.Add("api-sdk", "go-" + Version) + req.Header.Add("Authorization", "Bearer "+requester.Token) + res, resErr := requester.netClient.Do(req) + + if resErr != nil { + return "", resErr + } + + defer res.Body.Close() + + reqResp, _ := ioutil.ReadAll(res.Body) + response := string(reqResp) + + if res.StatusCode != http.StatusOK { + return "", errors.New(response) + } + + return response, nil +} + +func getRoute(endpoint string, params map[string]string) string { + patern, _ := regexp.Compile("\\:(\\w+)") + variables := patern.FindAllStringSubmatch(endpoint, -1) + for i := 0; i < len(variables); i++ { + if value, exists := params[variables[i][1]]; exists { + endpoint = strings.Replace(endpoint, variables[i][0], value, -1) + delete(params, variables[i][1]) + } + } + return endpoint +} + +func getQueryString(params map[string]string) string { + var query string + for key, value := range params { + if query != "" { + query += "&" + } else { + query += "?" + } + query += key + "=" + url.QueryEscape(value) + } + + return query +} \ No newline at end of file