Skip to content

Commit

Permalink
add: merchant transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
fanchann committed May 7, 2024
1 parent 6c22bec commit 465c7a3
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
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()
}
}

0 comments on commit 465c7a3

Please sign in to comment.