-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |