Skip to content

Commit

Permalink
chore: change struct to error
Browse files Browse the repository at this point in the history
  • Loading branch information
fanchann committed May 7, 2024
1 parent 0893c14 commit 6c22bec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
20 changes: 6 additions & 14 deletions client/fee_calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"

"github.com/zakirkun/go-tripay/internal/requester"
"github.com/zakirkun/go-tripay/utils"
)

type (
FeeCalcSuccessResponse struct {
FeeCalcResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data []struct {
Expand All @@ -31,26 +30,21 @@ type (
} `json:"data"`
}

FeeCalcFailResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}

FeeCalcParam struct {
Amount int
Code utils.TRIPAY_CHANNEL
}
)

func (c Client) FeeCalc(p FeeCalcParam) (*FeeCalcSuccessResponse, error) {
func (c Client) FeeCalc(p FeeCalcParam) (*FeeCalcResponse, error) {
return feeCalc(c, p, nil)
}

func (c Client) FeeCalcWithContext(ctx context.Context, p FeeCalcParam) (*FeeCalcSuccessResponse, error) {
func (c Client) FeeCalcWithContext(ctx context.Context, p FeeCalcParam) (*FeeCalcResponse, error) {
return feeCalc(c, p, ctx)
}

func feeCalc(c Client, p FeeCalcParam, ctx context.Context) (*FeeCalcSuccessResponse, error) {
func feeCalc(c Client, p FeeCalcParam, ctx context.Context) (*FeeCalcResponse, error) {
param := url.Values{}
param.Set("code", string(p.Code))
param.Set("amount", fmt.Sprintf("%d", p.Amount))
Expand All @@ -72,13 +66,11 @@ func feeCalc(c Client, p FeeCalcParam, ctx context.Context) (*FeeCalcSuccessResp
bodyReq, errReq = req.DO()
}

if errReq != nil || strings.Contains(string(paramReq.Body), "false") {
var failResponse FeeCalcFailResponse
json.Unmarshal(bodyReq.ResponseBody, &failResponse)
if errReq != nil {
return nil, errReq
}

var successResponse FeeCalcSuccessResponse
var successResponse FeeCalcResponse
json.Unmarshal(bodyReq.ResponseBody, &successResponse)
return &successResponse, nil
}
19 changes: 6 additions & 13 deletions client/merchant_pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/zakirkun/go-tripay/internal/requester"
)

type MerchantResponseOK struct {
type MerchantResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data []struct {
Expand All @@ -34,20 +34,15 @@ type MerchantResponseOK struct {
} `json:"data"`
}

type MerchantResponseFail struct {
Success bool `json:"success"`
Message string `json:"message"`
}

func (c Client) MerchantPay() (*MerchantResponseOK, *MerchantResponseFail) {
func (c Client) MerchantPay() (*MerchantResponse, error) {
return merchantPay(c, nil)
}

func (c Client) MerchantPayWithContext(ctx context.Context) (*MerchantResponseOK, *MerchantResponseFail) {
func (c Client) MerchantPayWithContext(ctx context.Context) (*MerchantResponse, error) {
return merchantPay(c, ctx)
}

func merchantPay(c Client, ctx context.Context) (*MerchantResponseOK, *MerchantResponseFail) {
func merchantPay(c Client, ctx context.Context) (*MerchantResponse, error) {
paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "merchant/payment-channel",
Method: "GET",
Expand All @@ -65,12 +60,10 @@ func merchantPay(c Client, ctx context.Context) (*MerchantResponseOK, *MerchantR
}

if errReq != nil {
var failResponse MerchantResponseFail
_ = json.Unmarshal(bodyReq.ResponseBody, &failResponse)
return nil, &failResponse
return nil, errReq
}

var successResponse MerchantResponseOK
var successResponse MerchantResponse
json.Unmarshal(bodyReq.ResponseBody, &successResponse)
return &successResponse, nil
}

0 comments on commit 6c22bec

Please sign in to comment.