Skip to content

Commit

Permalink
add transaction package
Browse files Browse the repository at this point in the history
  • Loading branch information
fenriz07 committed Jan 7, 2021
1 parent 3d8a9c2 commit bc9ce3c
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@ 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)
}

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)

}
31 changes: 31 additions & 0 deletions pkg/response/refundTransactionResponse.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions pkg/response/statusTransactionResponse.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions pkg/response/transactionAuthorizeResponse.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions pkg/transaction/details.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
97 changes: 97 additions & 0 deletions pkg/transaction/transaction.go
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit bc9ce3c

Please sign in to comment.