From bc9ce3c664830c834a7b3d7f3d88d25f5d356c9b Mon Sep 17 00:00:00 2001 From: Servio Zambrano Date: Wed, 6 Jan 2021 22:34:01 -0300 Subject: [PATCH] add transaction package --- cmd/main.go | 22 ++++- pkg/response/refundTransactionResponse.go | 31 +++++++ pkg/response/statusTransactionResponse.go | 41 +++++++++ pkg/response/transactionAuthorizeResponse.go | 42 +++++++++ pkg/transaction/details.go | 19 ++++ pkg/transaction/transaction.go | 97 ++++++++++++++++++++ 6 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 pkg/response/refundTransactionResponse.go create mode 100644 pkg/response/statusTransactionResponse.go create mode 100644 pkg/response/transactionAuthorizeResponse.go create mode 100644 pkg/transaction/details.go create mode 100644 pkg/transaction/transaction.go diff --git a/cmd/main.go b/cmd/main.go index 8981bd9..c32425b 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,15 +3,17 @@ package main import ( "fmt" - "github.com/fenriz07/Golang-Transbank-Oneclick-mall/pkg/inscription" "github.com/fenriz07/Golang-Transbank-Oneclick-mall/pkg/oneclickmall" + "github.com/fenriz07/Golang-Transbank-Oneclick-mall/pkg/transaction" ) func main() { oneclickmall.SetEnvironmentIntegration() - response, err := inscription.DeleteInscription("b1c2abdb-8df8-439b-be7a-53280627e070", "1212") + detail := transaction.CreateDetailTransaction("597055555542", "7oneclickgolang", 4000, 1) + + response, err := transaction.AuthorizeTransaction("1212", "6130952c-52db-4aed-b791-530a0fd1f94d", "7oneclickgolang", detail) if err != nil { fmt.Println(err) @@ -19,4 +21,20 @@ func main() { fmt.Println(response) + statusTransaction, err := transaction.StatusTransaction("7oneclickgolang") + + if err != nil { + fmt.Println(err) + } + + fmt.Println(statusTransaction) + + refundTransaction, err := transaction.RefundTransaction("7oneclickgolang", "597055555542", "7oneclickgolang", 2000) + + if err != nil { + fmt.Println(err) + } + + fmt.Println(refundTransaction) + } diff --git a/pkg/response/refundTransactionResponse.go b/pkg/response/refundTransactionResponse.go new file mode 100644 index 0000000..8016793 --- /dev/null +++ b/pkg/response/refundTransactionResponse.go @@ -0,0 +1,31 @@ +package response + +import ( + "encoding/json" + "log" + "time" +) + +type RefundTransactionResponse struct { + Type string `json:"type"` + AuthorizationCode string `json:"authorization_code"` + AuthorizationDate time.Time `json:"authorization_date"` + NullifiedAmount float64 `json:"nullified_amount"` + Balance float64 `json:"balance"` + ResponseCode int `json:"response_code"` +} + +/*GetRefundTransactionResponse fascade with return struct CreateInscriptionRespons*/ +func GetRefundTransactionResponse(response []byte) (RefundTransactionResponse, error) { + var rtr RefundTransactionResponse + + err := json.Unmarshal(response, &rtr) + + if err != nil { + log.Printf("Error in GetRefundTransactionResponse %v", err) + + return rtr, err + } + + return rtr, nil +} diff --git a/pkg/response/statusTransactionResponse.go b/pkg/response/statusTransactionResponse.go new file mode 100644 index 0000000..3cce830 --- /dev/null +++ b/pkg/response/statusTransactionResponse.go @@ -0,0 +1,41 @@ +package response + +import ( + "encoding/json" + "log" + "time" +) + +type StatusTransactionResponse struct { + BuyOrder string `json:"buy_order"` + CardDetail struct { + CardNumber string `json:"card_number"` + } `json:"card_detail"` + AccountingDate string `json:"accounting_date"` + TransactionDate time.Time `json:"transaction_date"` + Details []struct { + Amount int `json:"amount"` + Status string `json:"status"` + AuthorizationCode string `json:"authorization_code"` + PaymentTypeCode string `json:"payment_type_code"` + ResponseCode int `json:"response_code"` + InstallmentsNumber int `json:"installments_number"` + CommerceCode string `json:"commerce_code"` + BuyOrder string `json:"buy_order"` + } `json:"details"` +} + +/*GetStatusTransactionResponse fascade with return struct CreateInscriptionRespons*/ +func GetStatusTransactionResponse(response []byte) (StatusTransactionResponse, error) { + var str StatusTransactionResponse + + err := json.Unmarshal(response, &str) + + if err != nil { + log.Printf("Error in GetStatusTransactionResponse %v", err) + + return str, err + } + + return str, nil +} diff --git a/pkg/response/transactionAuthorizeResponse.go b/pkg/response/transactionAuthorizeResponse.go new file mode 100644 index 0000000..e7e1937 --- /dev/null +++ b/pkg/response/transactionAuthorizeResponse.go @@ -0,0 +1,42 @@ +package response + +import ( + "encoding/json" + "log" + "time" +) + +/*TransactionAuthorizeResponse struct with contain skeleton json to transactionAuthorizeResponse*/ +type TransactionAuthorizeResponse struct { + BuyOrder string `json:"buy_order"` + CardDetail struct { + CardNumber string `json:"card_number"` + } `json:"card_detail"` + AccountingDate string `json:"accounting_date"` + TransactionDate time.Time `json:"transaction_date"` + Details []struct { + Amount int `json:"amount"` + Status string `json:"status"` + AuthorizationCode string `json:"authorization_code"` + PaymentTypeCode string `json:"payment_type_code"` + ResponseCode int `json:"response_code"` + InstallmentsNumber int `json:"installments_number"` + CommerceCode string `json:"commerce_code"` + BuyOrder string `json:"buy_order"` + } `json:"details"` +} + +/*GetTransactionAuthorizeResponse fascade with return struct TransactionAuthorizeResponse*/ +func GetTransactionAuthorizeResponse(response []byte) (TransactionAuthorizeResponse, error) { + var tar TransactionAuthorizeResponse + + err := json.Unmarshal(response, &tar) + + if err != nil { + log.Printf("GetTransactionAuthorizeResponse %v", err) + + return tar, err + } + + return tar, nil +} diff --git a/pkg/transaction/details.go b/pkg/transaction/details.go new file mode 100644 index 0000000..faeeb5b --- /dev/null +++ b/pkg/transaction/details.go @@ -0,0 +1,19 @@ +package transaction + +type detailsTransaction struct { + CommerceCode string `json:"commerce_code"` + BuyOrder string `json:"buy_order"` + Amount int `json:"amount"` + InstallmentsNumber int `json:"installments_number"` +} + +/*CreateDetailTransaction return details struct*/ +func CreateDetailTransaction(commerceCode string, buyOrder string, amount int, installmentsNumber int) detailsTransaction { + + return detailsTransaction{ + CommerceCode: commerceCode, + BuyOrder: buyOrder, + Amount: amount, + InstallmentsNumber: installmentsNumber, + } +} diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go new file mode 100644 index 0000000..b06b677 --- /dev/null +++ b/pkg/transaction/transaction.go @@ -0,0 +1,97 @@ +package transaction + +import ( + "fmt" + "log" + + "github.com/fenriz07/Golang-Transbank-Oneclick-mall/pkg/client" + "github.com/fenriz07/Golang-Transbank-Oneclick-mall/pkg/response" +) + +const transactionEndpoint = "rswebpaytransaction/api/oneclick/v1.0/transactions" +const transactonRefundEndpoint = "/rswebpaytransaction/api/oneclick/v1.0/transactions/%s/refunds" + +/*AuthorizeTransaction Allows you to authorize a payment.*/ +func AuthorizeTransaction(username string, tbkUSER string, parentBuyOrder string, details detailsTransaction) (response.TransactionAuthorizeResponse, error) { + + detailBody := make(map[string]interface{}, 4) + + detailBody["commerce_code"] = details.CommerceCode + detailBody["buy_order"] = details.BuyOrder + detailBody["amount"] = details.Amount + detailBody["installments_number"] = details.InstallmentsNumber + + var detailsBody [1]map[string]interface{} + + detailsBody[0] = detailBody + + body := map[string]interface{}{ + "username": username, + "tbk_user": tbkUSER, + "buy_order": parentBuyOrder, + "details": detailsBody, + } + + httpClient := client.GetInstance() + + resp, err := httpClient.Post(transactionEndpoint, body) + + if err != nil { + log.Printf("Transaction fail in authorize func. \n%v\n", err) + + return response.TransactionAuthorizeResponse{}, err + } + + transactionAuthorizeResponse, err := response.GetTransactionAuthorizeResponse(resp.Body()) + + return transactionAuthorizeResponse, err +} + +/*StatusTransaction This allows the operation to obtain the status of the transaction at any time*/ +func StatusTransaction(buyOrder string) (response.StatusTransactionResponse, error) { + + httpClient := client.GetInstance() + + endpoint := fmt.Sprintf("%s/%s", transactionEndpoint, buyOrder) + + resp, err := httpClient.Get(endpoint) + + if err != nil { + log.Printf("Status Transaction fail in StatusTransaction func. \n%v\n", err) + + return response.StatusTransactionResponse{}, err + } + + refundTransactionResponse, err := response.GetStatusTransactionResponse(resp.Body()) + + return refundTransactionResponse, err +} + +/*RefundTransaction Allows you to reverse or void a previously authorized sales +transaction. This method returns a unique identifier of the reverse / abort transaction +as a response.*/ +func RefundTransaction(buyOrder string, commerceCode string, detailBuyOrder string, amount int) (response.RefundTransactionResponse, error) { + + httpClient := client.GetInstance() + + endpoint := fmt.Sprintf(transactonRefundEndpoint, buyOrder) + + body := map[string]interface{}{ + "commerce_code": commerceCode, + "detail_buy_order": detailBuyOrder, + "amount": amount, + } + + resp, err := httpClient.Post(endpoint, body) + + if err != nil { + log.Printf("Refund Transaction fail in RefundTransaction func. \n%v\n", err) + + return response.RefundTransactionResponse{}, err + } + + refundTransactionResponse, err := response.GetRefundTransactionResponse(resp.Body()) + + return refundTransactionResponse, err + +}