Skip to content

Commit

Permalink
wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
reywyn committed Jul 29, 2022
1 parent e237c68 commit 24b528d
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 15 deletions.
185 changes: 180 additions & 5 deletions adapter/common.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,215 @@
package adapter

import (
"bytes"
"encoding/json"
"errors"
"net/url"
"reflect"
"time"

"github.com/gorilla/schema"
)

var ErrNull = errors.New("can't be both IsNull and have value")
var Null = []byte("null")

var encoder = schema.NewEncoder()

const (
TimeLayout = "\"2006-01-02T15:04:05\""
)

type Boolean struct {
bool
IsNull bool
}

type Integer struct {
int
IsNull bool
}

type Long struct {
int64
IsNull bool
}

type BigDecimal struct {
float64
IsNull bool
}

type String struct {
string
IsNull bool
}

type Time struct {
time.Time
IsNull bool
}

func NewBoolean(v bool) *Boolean {
return &Boolean{v, false}
}

func NewInteger(v int) *Integer {
return &Integer{v, false}
}

func NewLong(v int64) *Long {
return &Long{v, false}
}

func NewBigDecimal(v float64) *BigDecimal {
return &BigDecimal{v, false}
}

func NewString(v string) *String {
return &String{v, false}
}

func NewTime(v time.Time) *Time {
return &Time{v, false}
}

func (v Boolean) MarshalJSON() ([]byte, error) {
switch {
case v.IsNull && v.bool:
return nil, ErrNull
case v.IsNull:
return Null, nil
default:
return json.Marshal(v.bool)
}
}

func (v *Boolean) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, Null) {
v.IsNull = true
return nil
}
return json.Unmarshal(src, &v.bool)
}

func (v Integer) MarshalJSON() ([]byte, error) {
switch {
case v.IsNull && v.int != 0:
return nil, ErrNull
case v.IsNull:
return Null, nil
default:
return json.Marshal(v.int)
}
}

func (v *Integer) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, Null) {
v.IsNull = true
return nil
}
return json.Unmarshal(src, &v.int)
}

func (v Long) MarshalJSON() ([]byte, error) {
switch {
case v.IsNull && v.int64 != 0:
return nil, ErrNull
case v.IsNull:
return Null, nil
default:
return json.Marshal(v.int64)
}
}

func (v *Long) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, Null) {
v.IsNull = true
return nil
}
return json.Unmarshal(src, &v.int64)
}

func (v BigDecimal) MarshalJSON() ([]byte, error) {
switch {
case v.IsNull && v.float64 != 0.0:
return nil, ErrNull
case v.IsNull:
return Null, nil
default:
return json.Marshal(v.float64)
}
}

func (v *BigDecimal) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, Null) {
v.IsNull = true
return nil
}
return json.Unmarshal(src, &v.float64)
}

func (v String) MarshalJSON() ([]byte, error) {
switch {
case v.IsNull && v.string != "":
return nil, ErrNull
case v.IsNull:
return Null, nil
default:
return json.Marshal(v.string)
}
}

func (v *String) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, Null) {
v.IsNull = true
return nil
}
return json.Unmarshal(src, &v.string)
}

func (v Time) MarshalJSON() ([]byte, error) {
switch {
case v.IsNull && !v.Time.IsZero():
return nil, ErrNull
case v.IsNull:
return Null, nil
default:
return v.Time.MarshalJSON()
}
}

func (t *Time) UnmarshalJSON(b []byte) error {
parse, err := time.Parse(TimeLayout, string(b))
func (v *Time) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, Null) {
v.IsNull = true
return nil
}

parse, err := time.Parse(TimeLayout, string(src))
if err != nil {
return err
}
t.Time = parse
v.Time = parse
return nil
}

var encoder = schema.NewEncoder()

func QueryParams(req interface{}) (string, error) {
queryParams := url.Values{}
err := encoder.Encode(req, queryParams)
if err != nil {
return "", err
}
<<<<<<< HEAD
//removeNulls(queryParams)

=======
for k, v := range queryParams {
if v == nil {
queryParams.Del(k)
}
}
>>>>>>> 73b36ad (wallet)
return queryParams.Encode(), nil
}

Expand Down
2 changes: 1 addition & 1 deletion adapter/rest/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func SendRequest(req *http.Request, v interface{}, opts model.RequestOptions) er

randomStr := GenerateRandomString()
hashStr := GenerateHash(req.URL.String(), opts.ApiKey, opts.SecretKey, randomStr, "")
fmt.Println(hashStr)
fmt.Println(req.URL.String())

req.Header.Set(model.ApiKeyHeaderName, opts.ApiKey)
req.Header.Set(model.RandomHeaderName, randomStr)
Expand Down
44 changes: 35 additions & 9 deletions adapter/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,49 @@ type Wallet struct {
}

type RetrieveMemberWalletRequest struct {
MemberId int64 `schema:"memberId"`
MemberId Long `schema:"memberId"`
}

type RetrieveMemberWalletResponse struct {
Id int64 `schema:"id"`
CreatedDate Time `schema:"createdDate"`
UpdatedDate Time `schema:"updatedDate"`
Amount float64 `schema:"amount"`
WithdrawalAmount float64 `schema:"withdrawalAmount"`
Currency model.Currency `schema:"currency"`
MemberId int64 `schema:"memberId"`
Id Long `schema:"id,omitempty"`
CreatedDate Time `schema:"createdDate,omitempty"`
UpdatedDate Time `schema:"updatedDate,omitempty"`
Amount BigDecimal `schema:"amount,omitempty"`
WithdrawalAmount BigDecimal `schema:"withdrawalAmount,omitempty"`
Currency model.Currency `schema:"currency,omitempty"`
MemberId Long `schema:"memberId,omitempty"`
}

type SearchWalletTransactionsRequest struct {
WalletId int64 `schema:"walletId"`
WalletTransactionType string `schema:"walletTransactionType,omitempty"`
Page int `schema:"page,omitempty"`
Size int `schema:"size,omitempty"`
}

type SearchWalletTransactionsResponse struct {
ID Long `json:"id"`
CreatedDate Time `json:"createdDate"`
WalletTransactionType String `json:"walletTransactionType"`
Amount BigDecimal `json:"amount"`
TransactionID Long `json:"transactionId"`
WalletID Long `json:"walletId"`
}

func (api *Wallet) RetrieveMemberWallet(request RetrieveMemberWalletRequest) (interface{}, error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/wallet/v1/members/%d/wallet", api.Opts.BaseURL, request.MemberId), nil)
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/wallet/v1/members/%d/wallet", api.Opts.BaseURL, request.MemberId.int64), nil)

res := model.Response[RetrieveMemberWalletResponse]{}
resErr := rest.SendRequest(req, &res, api.Opts)
return &res, resErr
}

func (api *Wallet) SearchWalletTransactions(request SearchWalletTransactionsRequest) (interface{}, error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/wallet/v1/wallets/%d/wallet-transactions", api.Opts.BaseURL, request.WalletId), nil)

req.URL.RawQuery, _ = QueryParams(request)

res := model.Response[model.DataResponse[SearchWalletTransactionsResponse]]{}
resErr := rest.SendRequest(req, &res, api.Opts)
return &res, resErr
}
40 changes: 40 additions & 0 deletions adapter/wallet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package adapter

import (
"craftgate-go-client/model"
"fmt"
"testing"
)

const (
baseURL = "https://sandbox-api.craftgate.io"
apiKey = "sandbox-YEhueLgomBjqsnvBlWVVuFsVhlvJlMHE"
secretKey = "sandbox-tBdcdKVGmGupzfaWcULcwDLMoglZZvTz"
)

var wallet = Wallet{
Opts: model.RequestOptions{
BaseURL: baseURL,
ApiKey: apiKey,
SecretKey: secretKey,
},
}

// test function
func TestWallet_RetrieveMemberWallet(t *testing.T) {
res, err := wallet.RetrieveMemberWallet(RetrieveMemberWalletRequest{MemberId: *NewLong(66988)})
fmt.Println(res)

if err != nil {
t.Errorf("Error %s", err)
}
}

func TestWallet_SearchWalletTransactions(t *testing.T) {
res, err := wallet.SearchWalletTransactions(SearchWalletTransactionsRequest{WalletId: 62181})
fmt.Println(res)

if err != nil {
t.Errorf("Error %s", err.Error())
}
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ func main() {

//res, _ := Craftgate.Installment.RetrieveBinNumber(adapter.RetrieveBinNumberRequest{BinNumber: "552096"})
//fmt.Println(res)
<<<<<<< HEAD
//
//res, _ := Craftgate.Installment.SearchInstallments(adapter.SearchInstallmentRequest{BinNumber: "552096", Price: 100.00})
//fmt.Println(res)
=======

//res, _ := Craftgate.Installment.SearchInstallments(adapter.SearchInstallmentRequest{BinNumber: "552096"})
//fmt.Println(res)

res, _ := Craftgate.Wallet.RetrieveMemberWallet(adapter.RetrieveMemberWalletRequest{MemberId: 66988})
fmt.Println(res)
>>>>>>> 73b36ad (wallet)

//resPaymentSearch, _ := Craftgate.PaymentReporting.SearchPayments(adapter.SearchPaymentsRequest{Currency: model.TRY})
//fmt.Println(resPaymentSearch)
Expand Down

0 comments on commit 24b528d

Please sign in to comment.