From 6a3d428cabd8e692e835434699bcdb7195ee28e4 Mon Sep 17 00:00:00 2001 From: Alican Akkus Date: Wed, 27 Jul 2022 10:01:15 +0300 Subject: [PATCH] adds payment reporting adapter as initial --- .idea/.gitignore | 8 +++ .idea/craftgate-go-client.iml | 9 +++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ adapter/payment_reporting.go | 71 ++++++++++++++++++++++ utils.go => adapter/rest/rest_client.go | 26 ++++---- client.go | 41 ++++--------- installment.go | 10 ++-- main.go | 14 ++++- model/model.go | 79 +++++++++++++++++++++++++ payment.go | 2 +- 11 files changed, 222 insertions(+), 52 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/craftgate-go-client.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 adapter/payment_reporting.go rename utils.go => adapter/rest/rest_client.go (73%) create mode 100644 model/model.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/craftgate-go-client.iml b/.idea/craftgate-go-client.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/craftgate-go-client.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d6c2163 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/adapter/payment_reporting.go b/adapter/payment_reporting.go new file mode 100644 index 0000000..52dfe24 --- /dev/null +++ b/adapter/payment_reporting.go @@ -0,0 +1,71 @@ +package adapter + +import ( + "craftgate-go-client/adapter/rest" + "craftgate-go-client/model" + "fmt" + "net/http" + "time" +) + +type PaymentReportingApi struct { + Opts model.RequestOptions +} + +type SearchPaymentsRequest struct { + Page int + Size int + PaymentId int64 + PaymentTransactionId int64 + BuyerMemberId int64 + SubMerchantMemberId int64 + ConversationId string + ExternalId string + OrderId string + PaymentType model.PaymentType + PaymentProvider model.PaymentProvider + PaymentStatus model.PaymentStatus + PaymentSource model.PaymentSource + PaymentChannel string + BinNumber string + LastFourDigits string + Currency model.Currency + MinPaidPrice float64 + MaxPaidPrice float64 + Installment int + IsThreeDS bool + MinCreatedDate time.Time + MaxCreatedDate time.Time +} + +type ReportingPaymentResponse struct { + Id int64 `json:"id"` + //CreatedDate time.Time `json:"createdDate"` + OrderId string `json:"orderId"` + Price float64 `json:"price"` + PaidPrice float64 `json:"paidPrice"` + WalletPrice float64 `json:"walletPrice"` + Currency model.Currency `json:"currency"` +} + +func (api *PaymentReportingApi) SearchPayments(request SearchPaymentsRequest) (*model.Response[ReportingPaymentResponse], error) { + req, _ := http.NewRequest("GET", fmt.Sprintf("%s/payment-reporting/v1/payments?", api.Opts.BaseURL), nil) + + q := req.URL.Query() + //q.Add("currency", string(request.Currency)) + + //t := reflect.TypeOf(request) + //for i := 0; i < t.NumField(); i++ { + // field := t.Field(i) + // value := reflect.ValueOf(field) + // fmt.Println(field, value) + // + // //q.Add(field.Name, string(field.)) + //} + + req.URL.RawQuery = q.Encode() + + res := model.Response[ReportingPaymentResponse]{} + resErr := rest.SendRequest(req, &res, api.Opts) + return &res, resErr +} diff --git a/utils.go b/adapter/rest/rest_client.go similarity index 73% rename from utils.go rename to adapter/rest/rest_client.go index 0cfbc2a..b916f83 100644 --- a/utils.go +++ b/adapter/rest/rest_client.go @@ -1,6 +1,7 @@ -package main +package rest import ( + "craftgate-go-client/model" "crypto/sha256" "encoding/base64" "encoding/json" @@ -11,13 +12,7 @@ import ( "time" ) -type RequestOptions struct { - BaseURL string - ApiKey string - SecretKey string -} - -func sendRequest(req *http.Request, v interface{}, opts RequestOptions) error { +func SendRequest(req *http.Request, v interface{}, opts model.RequestOptions) error { client := &http.Client{ Timeout: time.Minute, } @@ -25,11 +20,12 @@ func sendRequest(req *http.Request, v interface{}, opts RequestOptions) error { randomStr := GenerateRandomString() hashStr := GenerateHash(req.URL.String(), opts.ApiKey, opts.SecretKey, randomStr, "") fmt.Println(hashStr) - req.Header.Set(ApiKeyHeaderName, opts.ApiKey) - req.Header.Set(RandomHeaderName, randomStr) - req.Header.Set(AuthVersionHeaderName, "1") - req.Header.Set(ClientVersionHeaderName, "craftgate-go-client:1.0.0") - req.Header.Set(SignatureHeaderName, hashStr) + + req.Header.Set(model.ApiKeyHeaderName, opts.ApiKey) + req.Header.Set(model.RandomHeaderName, randomStr) + req.Header.Set(model.AuthVersionHeaderName, model.AuthVersion) + req.Header.Set(model.ClientVersionHeaderName, model.ClientVersion) + req.Header.Set(model.SignatureHeaderName, hashStr) req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "application/json; charset=utf-8") @@ -37,7 +33,7 @@ func sendRequest(req *http.Request, v interface{}, opts RequestOptions) error { defer res.Body.Close() if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest { - var errRes Response[any] + var errRes model.Response[any] if err = json.NewDecoder(res.Body).Decode(&v); err == nil { return errors.New(errRes.Errors.ErrorGroup) } @@ -66,5 +62,5 @@ func GenerateRandomString() string { /*s := strconv.FormatInt(time.Now().UnixNano(), 16) fmt.Println(s[8:]) return s[8:]*/ - return "12345678" + return "1234567890" } diff --git a/client.go b/client.go index 638f9de..0651594 100644 --- a/client.go +++ b/client.go @@ -1,41 +1,24 @@ package main -const ( - ApiKeyHeaderName = "x-api-key" - RandomHeaderName = "x-rnd-key" - AuthVersionHeaderName = "x-auth-version" - ClientVersionHeaderName = "x-client-version" - SignatureHeaderName = "x-signature" +import ( + "craftgate-go-client/adapter" + "craftgate-go-client/model" ) -type Response[T any] struct { - DataResponse DataResponse[T] `json:"data"` - Errors ErrorResponse `json:"errors"` -} - -type ErrorResponse struct { - ErrorGroup string `json:"errorGroup"` - ErrorDescription string `json:"errorDescription"` - ErrorCode string `json:"errorCode"` -} - -type DataResponse[T any] struct { - Items []T `json:"items"` -} - type Client struct { - Installment InstallmentApi + Installment InstallmentApi + PaymentReporting adapter.PaymentReportingApi } func CraftgateClient(apiKey, secretKey, baseURL string) *Client { + options := model.RequestOptions{ + ApiKey: apiKey, + SecretKey: secretKey, + BaseURL: baseURL, + } return &Client{ - Installment: InstallmentApi{ - Opts: RequestOptions{ - ApiKey: apiKey, - SecretKey: secretKey, - BaseURL: baseURL, - }, - }, + Installment: InstallmentApi{Opts: options}, + PaymentReporting: adapter.PaymentReportingApi{Opts: options}, } } diff --git a/installment.go b/installment.go index 222f1c6..5a3af7a 100644 --- a/installment.go +++ b/installment.go @@ -1,12 +1,14 @@ package main import ( + rest "craftgate-go-client/adapter/rest" + "craftgate-go-client/model" "fmt" "net/http" ) type InstallmentApi struct { - Opts RequestOptions + Opts model.RequestOptions } type SearchInstallmentRequest struct { @@ -38,7 +40,7 @@ type InstallmentResponse struct { InstallmentPrices []InstallmentPrice `json:"installmentPrices"` } -func (api *InstallmentApi) SearchInstallments(request SearchInstallmentRequest) (*Response[InstallmentResponse], error) { +func (api *InstallmentApi) SearchInstallments(request SearchInstallmentRequest) (*model.Response[InstallmentResponse], error) { req, _ := http.NewRequest("GET", fmt.Sprintf("%s/installment/v1/installments", api.Opts.BaseURL), nil) q := req.URL.Query() @@ -46,7 +48,7 @@ func (api *InstallmentApi) SearchInstallments(request SearchInstallmentRequest) q.Add("price", fmt.Sprintf("%f", request.Price)) req.URL.RawQuery = q.Encode() - res := Response[InstallmentResponse]{} - resErr := sendRequest(req, &res, api.Opts) + res := model.Response[InstallmentResponse]{} + resErr := rest.SendRequest(req, &res, api.Opts) return &res, resErr } diff --git a/main.go b/main.go index ea5ef9b..9d2b1aa 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,20 @@ package main -import "fmt" +import ( + "craftgate-go-client/adapter" + "craftgate-go-client/model" + "fmt" +) func main() { baseURL := "https://sandbox-api.craftgate.io" apiKey := "sandbox-YEhueLgomBjqsnvBlWVVuFsVhlvJlMHE" secretKey := "sandbox-tBdcdKVGmGupzfaWcULcwDLMoglZZvTz" Craftgate := CraftgateClient(apiKey, secretKey, baseURL) - res, _ := Craftgate.Installment.SearchInstallments(SearchInstallmentRequest{BinNumber: "487074", Price: 100.00}) - fmt.Println(res) + + //res, _ := Craftgate.Installment.SearchInstallments(SearchInstallmentRequest{BinNumber: "487074", Price: 100.00}) + //fmt.Println(res) + + resPaymentSearch, _ := Craftgate.PaymentReporting.SearchPayments(adapter.SearchPaymentsRequest{Currency: model.TRY}) + fmt.Println(resPaymentSearch) } diff --git a/model/model.go b/model/model.go new file mode 100644 index 0000000..5cff1cc --- /dev/null +++ b/model/model.go @@ -0,0 +1,79 @@ +package model + +type PaymentType string +type PaymentProvider string +type PaymentStatus string +type PaymentSource string +type Currency string + +type RequestOptions struct { + BaseURL string + ApiKey string + SecretKey string +} + +type Response[T any] struct { + DataResponse DataResponse[T] `json:"data"` + Errors ErrorResponse `json:"errors"` +} + +type ErrorResponse struct { + ErrorGroup string `json:"errorGroup"` + ErrorDescription string `json:"errorDescription"` + ErrorCode string `json:"errorCode"` +} + +type DataResponse[T any] struct { + Items []T `json:"items"` +} + +const ( + ApiKeyHeaderName = "x-api-key" + RandomHeaderName = "x-rnd-key" + AuthVersionHeaderName = "x-auth-version" + ClientVersionHeaderName = "x-client-version" + SignatureHeaderName = "x-signature" + AuthVersion = "1" + ClientVersion = "craftgate-go-client:1.0.0" +) + +// payment type declaration +const ( + CARD_PAYMENT PaymentType = "CARD_PAYMENT" + DEPOSIT_PAYMENT PaymentType = "DEPOSIT_PAYMENT" + WALLET_PAYMENT PaymentType = "WALLET_PAYMENT" + CARD_AND_WALLET_PAYMENT PaymentType = "CARD_AND_WALLET_PAYMENT" + BANK_TRANSFER PaymentType = "BANK_TRANSFER" +) + +// payment provider declaration +const ( + BANK PaymentProvider = "BANK" + CG_WALLET PaymentProvider = "CG_WALLET" + MASTERPASS PaymentProvider = "MASTERPASS" + GARANTI_PAY PaymentProvider = "GARANTI_PAY" +) + +// payment status declaration +const ( + FAILURE PaymentStatus = "FAILURE" + SUCCESS PaymentStatus = "SUCCESS" + INIT_THREEDS PaymentStatus = "INIT_THREEDS" + CALLBACK_THREEDS PaymentStatus = "CALLBACK_THREEDS" + WAITING PaymentStatus = "WAITING" +) + +// payment source declaration +const ( + API PaymentSource = "API" + CHECKOUT_FORM PaymentSource = "CHECKOUT_FORM" + PAY_BY_LINK PaymentSource = "PAY_BY_LINK" +) + +// currency declaration +const ( + TRY Currency = "TRY" + USD Currency = "USD" + EUR Currency = "EUR" + GBP Currency = "GBP" +) diff --git a/payment.go b/payment.go index 2e280cf..f2c5590 100644 --- a/payment.go +++ b/payment.go @@ -71,7 +71,7 @@ func (c *Client) Pay(ctx context.Context, paymentParams PaymentParams) (*Payment page = options.Page } - req, err := http.NewRequest("GET", fmt.Sprintf("%s/faces?limit=%d&page=%d", c.BaseURL, limit, page), nil) + req, err := rest.NewRequest("GET", fmt.Sprintf("%s/faces?limit=%d&page=%d", c.BaseURL, limit, page), nil) if err != nil { return nil, err }