Skip to content

Commit

Permalink
Merge pull request #2 from fanchann/development
Browse files Browse the repository at this point in the history
add merchant & refactor some code
  • Loading branch information
zakirkun authored May 6, 2024
2 parents 39607c8 + f113d09 commit 5f37955
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 12 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: run unit test

on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup go
uses: actions/setup-go@v4
with:
go-version: '1.22.x'
- name: run unit test
run: go test -v -run=. github.com/zakirkun/go-tripay/client
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Client struct {
MerchantCode string
ApiKey string
PrivateKey string
Mode string
Mode utils.TRIPAY_MODE
}

func (c Client) HeaderRequest() []map[string]string {
Expand All @@ -20,8 +20,8 @@ func (c Client) HeaderRequest() []map[string]string {

func (c Client) BaseUrl() string {
if c.Mode == utils.MODE_DEVELOPMENT {
return utils.URL_DEVELOPMENT
return string(utils.URL_DEVELOPMENT)
}

return utils.URL_PRODUCTION
return string(utils.URL_PRODUCTION)
}
76 changes: 76 additions & 0 deletions client/merchant_pay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package client

import (
"context"
"encoding/json"

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

type MerchantResponseOK struct {
Success bool `json:"success"`
Message string `json:"message"`
Data []struct {
Group string `json:"group"`
Code string `json:"code"`
Name string `json:"name"`
Type string `json:"type"`
FeeMerchant struct {
Flat uint `json:"flat"`
Percent uint `json:"percent"`
} `json:"fee_merchant"`
FeeCustomer struct {
Flat uint `json:"flat"`
Percent uint `json:"percent"`
} `json:"fee_customer"`
TotalFee struct {
Flat uint `json:"flat"`
Percent float64 `json:"percent"`
} `json:"total_fee"`
MinimumFee uint `json:"minimum_fee"`
MaximumFee uint `json:"maximum_fee"`
IconURL string `json:"icon_url"`
Active bool `json:"active"`
} `json:"data"`
}

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

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

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

func merchantPay(c Client, ctx context.Context) (*MerchantResponseOK, *MerchantResponseFail) {
paramReq := requester.IRequesterParams{
Url: c.BaseUrl() + "merchant/payment-channel",
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 {
var failResponse MerchantResponseFail
_ = json.Unmarshal(bodyReq.ResponseBody, &failResponse)
return nil, &failResponse
}

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

import (
"context"
"testing"
"time"

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

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

reponseOk, responseBad := client.MerchantPay()
if responseBad != nil {
t.Errorf("ERROR: %v", responseBad)
}

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

func TestMerchantPayWithCtx(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, responseBad := client.MerchantPayWithContext(ctx)
if responseBad != nil {
t.Errorf("ERROR: %v", responseBad)
}

t.Log("Success: ", reponseOk)
}
3 changes: 3 additions & 0 deletions internal/requester/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package requester

import "context"

type IRequester interface {
DO() (*IResponseBody, error)
DOWithContext(ctx context.Context) (*IResponseBody, error)
}
27 changes: 22 additions & 5 deletions internal/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package requester

import (
"bytes"
"context"
"io/ioutil"
"net/http"
)
Expand All @@ -15,15 +16,31 @@ func NewRequester(params IRequesterParams) IRequester {
}
}

func (i IRequesterParams) DOWithContext(ctx context.Context) (*IResponseBody, error) {
return do(i, ctx)
}

func (i IRequesterParams) DO() (*IResponseBody, error) {
return do(i, nil)
}

// Create a new HTTP request with the POST method and the request body
req, err := http.NewRequest(i.Method, i.Url, bytes.NewBuffer(i.Body))
if err != nil {
return nil, err
func do(r IRequesterParams, ctx context.Context) (*IResponseBody, error) {
var req *http.Request
var errReq error

if ctx != nil {
req, errReq = http.NewRequestWithContext(ctx, r.Method, r.Url, bytes.NewBuffer(r.Body))
if errReq != nil {
return nil, errReq
}
} else {
req, errReq = http.NewRequest(r.Method, r.Url, bytes.NewBuffer(r.Body))
if errReq != nil {
return nil, errReq
}
}

for _, header := range i.Header {
for _, header := range r.Header {
for key, value := range header {
req.Header.Set(key, value)
}
Expand Down
11 changes: 7 additions & 4 deletions utils/constant.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package utils

const URL_DEVELOPMENT string = "https://tripay.co.id/api-sandbox/"
const URL_PRODUCTION string = "https://tripay.co.id/api/"
type TRIPAY_URL string
type TRIPAY_MODE string

const MODE_DEVELOPMENT string = "development"
const MODE_PRODUCTION string = "production"
const URL_DEVELOPMENT TRIPAY_URL = "https://tripay.co.id/api-sandbox/"
const URL_PRODUCTION TRIPAY_URL = "https://tripay.co.id/api/"

const MODE_DEVELOPMENT TRIPAY_MODE = "development"
const MODE_PRODUCTION TRIPAY_MODE = "production"

0 comments on commit 5f37955

Please sign in to comment.