-
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.
Merge pull request #2 from fanchann/development
add merchant & refactor some code
- Loading branch information
Showing
7 changed files
with
170 additions
and
12 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
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 |
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,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 | ||
} |
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,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) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package requester | ||
|
||
import "context" | ||
|
||
type IRequester interface { | ||
DO() (*IResponseBody, error) | ||
DOWithContext(ctx context.Context) (*IResponseBody, error) | ||
} |
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 |
---|---|---|
@@ -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" |