Skip to content

Commit

Permalink
Merge pull request #3 from fanchann/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
zakirkun authored May 9, 2024
2 parents 5f37955 + 465c7a3 commit 939ee45
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 35 deletions.
76 changes: 76 additions & 0 deletions client/fee_calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package client

import (
"context"
"encoding/json"
"fmt"
"net/url"

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

type (
FeeCalcResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data []struct {
Code string
Name string
Fee struct {
Flat int `json:"flat"`
Percent string `json:"percent"`
Min interface{} `json:"min"`
Max interface{} `json:"max"`
} `json:"fee"`
TotalFee struct {
Merchant int `json:"merchant"`
Customer int `json:"customer"`
} `json:"total_fee"`
} `json:"data"`
}

FeeCalcParam struct {
Amount int
Code utils.TRIPAY_CHANNEL
}
)

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

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) (*FeeCalcResponse, error) {
param := url.Values{}
param.Set("code", string(p.Code))
param.Set("amount", fmt.Sprintf("%d", p.Amount))

paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "merchant/fee-calculator?" + param.Encode(),
Method: "GET",
Body: nil,
Header: c.HeaderRequest(),
}

req := requester.NewRequester(paramReq)

bodyReq := new(requester.IResponseBody)
var errReq error
if ctx != nil {
bodyReq, errReq = req.DOWithContext(ctx)
} else {
bodyReq, errReq = req.DO()
}

if errReq != nil {
return nil, errReq
}

var successResponse FeeCalcResponse
json.Unmarshal(bodyReq.ResponseBody, &successResponse)
return &successResponse, nil
}
51 changes: 51 additions & 0 deletions client/fee_calc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package client

import (
"context"
"testing"

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

func TestFeeCalcWithContextSuccess(t *testing.T) {
client := Client{
MerchantCode: "T14302",
ApiKey: "DEV-ZKIDl5gE3AsCDThj7mWX6yvQ8f42NZWJWlZ7TSzS",
PrivateKey: "J2WTm-93avv-w0PZV-ur1t4-4TCjd",
Mode: utils.MODE_DEVELOPMENT,
}

feeCalcParam := FeeCalcParam{
Code: utils.CHANNEL_ALFAMIDI,
Amount: 100000,
}
response, err := client.FeeCalcWithContext(context.Background(), feeCalcParam)
if err != nil {
t.Errorf("ERROR: %v", err)
}

t.Log("Success: ", response)
}

func TestFeeCalcWithContextFail(t *testing.T) {
client := Client{
MerchantCode: "T14302",
ApiKey: "DEV-ZKIDl5gE3AsCDThj7mWX6yvQ8f42NZWJWlZ7TSzS",
PrivateKey: "J2WTm-93avv-w0PZV-ur1t4-4TCjd",
Mode: utils.MODE_DEVELOPMENT,
}

feeCalcParam := FeeCalcParam{
Code: "salah",
Amount: 100000,
}
response, err := client.FeeCalcWithContext(context.Background(), feeCalcParam)
if err != nil {
t.FailNow()
}

if response.Success != false {
t.FailNow()
}

}
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
}
127 changes: 127 additions & 0 deletions client/merchant_transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package client

import (
"context"
"encoding/json"
"fmt"
"net/url"

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

type (
OrderItem struct {
SKU interface{} `json:"sku"`
Name string `json:"name"`
Price int `json:"price"`
Quantity int `json:"quantity"`
Subtotal int `json:"subtotal"`
}

Data struct {
Reference string `json:"reference"`
MerchantRef string `json:"merchant_ref"`
PaymentSelection string `json:"payment_selection_"`
PaymentMethod string `json:"payment_method"`
PaymentName string `json:"payment_name"`
CustomerName string `json:"customer_name"`
CustomerEmail string `json:"customer_email"`
CustomerPhone interface{} `json:"customer_phone"`
CallbackURL interface{} `json:"callback_url"`
ReturnURL interface{} `json:"return_url"`
Amount int `json:"amount"`
FeeMerchant int `json:"fee_merchant"`
FeeCustomer int `json:"fee_customer"`
TotalFee int `json:"total_fee"`
AmountReceived int `json:"amount_received"`
PayCode int64 `json:"pay_code"`
PayURL interface{} `json:"pay_url"`
CheckoutURL string `json:"checkout_url"`
OrderItems []OrderItem `json:"order_items"`
Status string `json:"status"`
Note interface{} `json:"note"`
CreatedAt int64 `json:"created_at"`
ExpiredAt int64 `json:"expired_at"`
PaidAt interface{} `json:"paid_at"`
}

Pagination struct {
Sort string `json:"sort"`
Offset struct {
From int `json:"from"`
To int `json:"to"`
} `json:"offset"`
CurrentPage int `json:"current_page"`
PreviousPage interface{} `json:"previous_page"`
NextPage interface{} `json:"next_page"`
LastPage int `json:"last_page"`
PerPage int `json:"per_page"`
TotalRecords int `json:"total_records"`
}

MerchantTransactionsResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data []Data `json:"data"`
Pagination Pagination `json:"pagination"`
}

MerchantTransactionsParam struct {
Page int
PerPage int
Sort string
Reference string
MerchantRef string
Method string
Status string
}
)

func (c Client) MerchantTransactions(p ...MerchantTransactionsParam) (*MerchantTransactionsResponse, error) {
return merchantTransactions(c, nil, p...)
}

func (c Client) MerchantTransactionsWithContext(ctx context.Context, p ...MerchantTransactionsParam) (*MerchantTransactionsResponse, error) {
return merchantTransactions(c, ctx, p...)
}

func merchantTransactions(c Client, ctx context.Context, p ...MerchantTransactionsParam) (*MerchantTransactionsResponse, error) {
var merchatsParams MerchantTransactionsParam

for _, m := range p {
merchatsParams = m
}

urlParam := url.Values{}
urlParam.Set("page", fmt.Sprintf("%d", merchatsParams.Page))
urlParam.Set("per_page", fmt.Sprintf("%d", merchatsParams.PerPage))
urlParam.Set("sort", merchatsParams.Sort)
urlParam.Set("reference", merchatsParams.Reference)
urlParam.Set("merchant_ref", merchatsParams.MerchantRef)
urlParam.Set("method", merchatsParams.Method)
urlParam.Set("status", merchatsParams.Status)

paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "merchant/transactions?" + urlParam.Encode(),
Method: "GET",
Body: nil,
Header: c.HeaderRequest(),
}

req := requester.NewRequester(paramReq)
bodyReq := new(requester.IResponseBody)
var errReq error
if ctx != nil {
bodyReq, errReq = req.DOWithContext(ctx)
} else {
bodyReq, errReq = req.DO()
}

if errReq != nil {
return nil, errReq
}

var response MerchantTransactionsResponse
json.Unmarshal(bodyReq.ResponseBody, &response)
return &response, nil
}
46 changes: 46 additions & 0 deletions client/merchant_transactions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client

import (
"context"
"testing"
"time"

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

func TestMerchantTransactionsWithContextSuccess(t *testing.T) {
client := Client{
MerchantCode: "T14302",
ApiKey: "DEV-ZKIDl5gE3AsCDThj7mWX6yvQ8f42NZWJWlZ7TSzS",
PrivateKey: "J2WTm-93avv-w0PZV-ur1t4-4TCjd",
Mode: utils.MODE_DEVELOPMENT,
}

ctx, timeout := context.WithTimeout(context.Background(), 5*time.Second)
defer timeout()

reponseOk, err := client.MerchantTransactionsWithContext(ctx)
if err != nil {
t.FailNow()
}

t.Log(reponseOk)
}

func TestMerchantTransactionsWithContextFailed(t *testing.T) {
client := Client{
MerchantCode: "T14302",
ApiKey: "DEV-ZKIDl5gE3AsCDThj7mWX6yvQ8f42NZWJWlZ7TSz",
PrivateKey: "J2WTm-93avv-w0PZV-ur1t4-4TCjd",
Mode: utils.MODE_DEVELOPMENT,
}

ctx, timeout := context.WithTimeout(context.Background(), 5*time.Second)
defer timeout()

reponseOk, err := client.MerchantTransactionsWithContext(ctx)
if err != nil || reponseOk.Success {
t.Log(reponseOk)
t.FailNow()
}
}
Loading

0 comments on commit 939ee45

Please sign in to comment.