-
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 #3 from fanchann/development
Development
- Loading branch information
Showing
6 changed files
with
330 additions
and
35 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,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 | ||
} |
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,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() | ||
} | ||
|
||
} |
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,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 | ||
} |
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,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() | ||
} | ||
} |
Oops, something went wrong.