From 95bdfa7dd0b0ecf52bb14dc9ee653f1f4fbf3553 Mon Sep 17 00:00:00 2001 From: Serhat Tunca <35580464+tuncaserhat@users.noreply.github.com> Date: Fri, 21 Jul 2023 15:28:59 +0300 Subject: [PATCH] add payout account crud operations (#39) --- adapter/model.go | 46 +++++++++++++++++++- adapter/settlement.go | 61 +++++++++++++++++++++++++++ tests/settlement_test.go | 90 +++++++++++++++++++++++++++++++++++----- 3 files changed, 184 insertions(+), 13 deletions(-) diff --git a/adapter/model.go b/adapter/model.go index c6f9fb5..892bd6b 100644 --- a/adapter/model.go +++ b/adapter/model.go @@ -35,6 +35,8 @@ type WalletTransactionType string type WebhookEventType string type WebhookStatus string type FileStatus string +type AccountOwner string +type PayoutAccountType string const ( ApiKeyHeaderName = "x-api-key" @@ -215,8 +217,9 @@ const ( // settlementEarningsDestination type declaration const ( - SettlementEarningsDestinationIBAN SettlementEarningsDestination = "IBAN" - SettlementEarningsDestinationWALLET SettlementEarningsDestination = "WALLET" + SettlementEarningsDestinationIBAN SettlementEarningsDestination = "IBAN" + SettlementEarningsDestinationWALLET SettlementEarningsDestination = "WALLET" + SettlementEarningsDestinationCROSS_BORDER SettlementEarningsDestination = "CROSS_BORDER" ) // refundDestinationType type declaration @@ -321,6 +324,15 @@ const ( FileStatusAPPROVED FileStatus = "APPROVED" ) +const ( + AccountOwnerMERCHANT AccountOwner = "MERCHANT" + AccountOwnerSUB_MERCHANT_MEMBER AccountOwner = "SUB_MERCHANT_MEMBER" +) + +const ( + PayoutAccountTypeWISE PayoutAccountType = "WISE" +) + // requests type CreatePaymentRequest struct { Price float64 `json:"price,omitempty"` @@ -564,6 +576,27 @@ type CheckMasterpassUserRequest struct { MasterpassGsmNumber string `json:"masterpassGsmNumber"` } +type CreatePayoutAccountRequest struct { + AccountType PayoutAccountType `json:"type,omitempty"` + ExternalAccountId string `json:"externalAccountId,omitempty"` + Currency Currency `json:"currency,omitempty"` + AccountOwner AccountOwner `json:"accountOwner,omitempty"` + SubMerchantMemberId int64 `json:"subMerchantMemberId,omitempty"` +} + +type UpdatePayoutAccountRequest struct { + AccountType PayoutAccountType `json:"type,omitempty"` + ExternalAccountId string `json:"externalAccountId,omitempty"` +} + +type SearchPayoutAccountRequest struct { + Currency Currency `json:"currency,omitempty"` + AccountOwner AccountOwner `json:"accountOwner,omitempty"` + SubMerchantMemberId int64 `json:"subMerchantMemberId,omitempty"` + Page int `schema:"page,omitempty"` + Size int `schema:"size,omitempty"` +} + // responses type PaymentResponse struct { Id *int64 `json:"id"` @@ -685,6 +718,15 @@ type ApmDepositPaymentResponse struct { WalletTransaction *WalletTransaction `json:"walletTransaction"` } +type PayoutAccountResponse struct { + Id int64 `json:"id"` + AccountType PayoutAccountType `json:"type"` + ExternalAccountId string `json:"externalAccountId"` + Currency Currency `json:"currency"` + AccountOwner AccountOwner `json:"accountOwner"` + SubMerchantMemberId *int64 `json:"subMerchantMemberId"` +} + type RefundWalletTransactionRequest struct { RefundPrice float64 `json:"refundPrice"` } diff --git a/adapter/settlement.go b/adapter/settlement.go index 14052f5..5c2f0b1 100644 --- a/adapter/settlement.go +++ b/adapter/settlement.go @@ -2,6 +2,7 @@ package adapter import ( "context" + "fmt" "net/http" ) @@ -23,3 +24,63 @@ func (api *Settlement) CreateInstantWalletSettlement(ctx context.Context, reques return response.Data, nil } + +func (api *Settlement) CreatePayoutAccount(ctx context.Context, request CreatePayoutAccountRequest) (*PayoutAccountResponse, error) { + newRequest, err := api.Client.NewRequest(ctx, http.MethodPost, "/settlement/v1/payout-accounts", request) + if err != nil { + return nil, err + } + + response := &Response[PayoutAccountResponse]{} + err = api.Client.Do(ctx, newRequest, response) + if err != nil { + return nil, err + } + + return response.Data, nil +} + +func (api *Settlement) UpdatePayoutAccount(ctx context.Context, id int64, request UpdatePayoutAccountRequest) error { + newRequest, err := api.Client.NewRequest(ctx, http.MethodPut, fmt.Sprintf("/settlement/v1/payout-accounts/%d", id), request) + if err != nil { + return err + } + + response := &Void{} + err = api.Client.Do(ctx, newRequest, response) + if err != nil { + return err + } + + return nil +} + +func (api *Settlement) DeletePayoutAccount(ctx context.Context, id int64) error { + newRequest, err := api.Client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("/settlement/v1/payout-accounts/%d", id), nil) + if err != nil { + return err + } + + response := &Void{} + err = api.Client.Do(ctx, newRequest, response) + if err != nil { + return err + } + + return nil +} + +func (api *Settlement) SearchPayoutAccounts(ctx context.Context, request SearchPayoutAccountRequest) (*DataResponse[PayoutAccountResponse], error) { + newRequest, err := api.Client.NewRequest(ctx, http.MethodGet, "/settlement/v1/payout-accounts", request) + if err != nil { + return nil, err + } + + response := &Response[DataResponse[PayoutAccountResponse]]{} + err = api.Client.Do(ctx, newRequest, response) + if err != nil { + return nil, err + } + + return response.Data, nil +} diff --git a/tests/settlement_test.go b/tests/settlement_test.go index 36608dc..9708ef0 100644 --- a/tests/settlement_test.go +++ b/tests/settlement_test.go @@ -1,22 +1,90 @@ package tests import ( - "context" - "github.com/craftgate/craftgate-go-client/adapter" - craftgate "github.com/craftgate/craftgate-go-client/adapter" - "github.com/davecgh/go-spew/spew" - "testing" + "context" + "github.com/craftgate/craftgate-go-client/adapter" + craftgate "github.com/craftgate/craftgate-go-client/adapter" + "github.com/davecgh/go-spew/spew" + "testing" ) var settlementClient, _ = craftgate.New("api-key", "secret-key", "https://sandbox-api.craftgate.io") func TestSettlement_CreateInstantWalletSettlement(t *testing.T) { - request := adapter.CreateInstantWalletSettlementRequest{ExcludedSubMerchantMemberIds: []int64{1, 2}} + request := adapter.CreateInstantWalletSettlementRequest{ExcludedSubMerchantMemberIds: []int64{1, 2}} - res, err := settlementClient.Settlement.CreateInstantWalletSettlement(context.Background(), request) - _, _ = spew.Printf("%#v\n", res) + res, err := settlementClient.Settlement.CreateInstantWalletSettlement(context.Background(), request) + _, _ = spew.Printf("%#v\n", res) - if err != nil { - t.Errorf("Error %s", err) - } + if err != nil { + t.Errorf("Error %s", err) + } +} + +func TestSettlement_CreateMerchantPayoutAccount(t *testing.T) { + request := adapter.CreatePayoutAccountRequest{ + AccountType: craftgate.PayoutAccountTypeWISE, + ExternalAccountId: "wiseRecipientId", + Currency: craftgate.USD, + AccountOwner: craftgate.AccountOwnerMERCHANT, + } + + res, err := settlementClient.Settlement.CreatePayoutAccount(context.Background(), request) + _, _ = spew.Printf("%#v\n", res) + + if err != nil { + t.Errorf("Error %s", err) + } +} + +func TestSettlement_CreateSubMerchantPayoutAccount(t *testing.T) { + request := adapter.CreatePayoutAccountRequest{ + AccountType: craftgate.PayoutAccountTypeWISE, + ExternalAccountId: "wiseRecipientId", + Currency: craftgate.EUR, + AccountOwner: craftgate.AccountOwnerSUB_MERCHANT_MEMBER, + SubMerchantMemberId: 1, + } + + res, err := settlementClient.Settlement.CreatePayoutAccount(context.Background(), request) + _, _ = spew.Printf("%#v\n", res) + + if err != nil { + t.Errorf("Error %s", err) + } +} + +func TestSettlement_UpdatePayoutAccount(t *testing.T) { + request := adapter.UpdatePayoutAccountRequest{ + AccountType: craftgate.PayoutAccountTypeWISE, + ExternalAccountId: "wiseRecipientId2", + } + + err := settlementClient.Settlement.UpdatePayoutAccount(context.Background(), 18, request) + + if err != nil { + t.Errorf("Error %s", err) + } +} + +func TestSettlement_DeletePayoutAccount(t *testing.T) { + err := settlementClient.Settlement.DeletePayoutAccount(context.Background(), 18) + + if err != nil { + t.Errorf("Error %s", err) + } +} + +func TestSettlement_SearchPayoutAccounts(t *testing.T) { + request := adapter.SearchPayoutAccountRequest{ + Currency: craftgate.USD, + AccountOwner: craftgate.AccountOwnerMERCHANT, + } + + res, err := settlementClient.Settlement.SearchPayoutAccounts(context.Background(), request) + _, _ = spew.Printf("%#v\n", res) + + if err != nil { + t.Errorf("Error %s", err) + } }