Skip to content

Commit

Permalink
adds payment reporting adapter as initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Alican Akkus authored and Alican Akkus committed Jul 27, 2022
1 parent 3c98135 commit 6a3d428
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 52 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/craftgate-go-client.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions adapter/payment_reporting.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 11 additions & 15 deletions utils.go → adapter/rest/rest_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main
package rest

import (
"craftgate-go-client/model"
"crypto/sha256"
"encoding/base64"
"encoding/json"
Expand All @@ -11,33 +12,28 @@ 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,
}

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")

res, err := client.Do(req)
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)
}
Expand Down Expand Up @@ -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"
}
41 changes: 12 additions & 29 deletions client.go
Original file line number Diff line number Diff line change
@@ -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},
}
}
10 changes: 6 additions & 4 deletions installment.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -38,15 +40,15 @@ 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()
q.Add("binNumber", request.BinNumber)
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
}
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
79 changes: 79 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
@@ -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"
)
2 changes: 1 addition & 1 deletion payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6a3d428

Please sign in to comment.