diff --git a/ach/ach.go b/ach/ach.go new file mode 100644 index 0000000..c3dc905 --- /dev/null +++ b/ach/ach.go @@ -0,0 +1,101 @@ +package ach + +import ( + "encoding/json" + + "github.com/dapi-co/dapi-go/config" + "github.com/dapi-co/dapi-go/constants" + "github.com/dapi-co/dapi-go/request" + "github.com/dapi-co/dapi-go/response" +) + +// ACH is the base type that allows talking to the ach endpoints +type ACH struct { + Config *config.Config +} + +// CreatePull represents the pull transfer to be created +type CreatePull struct { + Transfer request.ACHPullTransfer +} + +// CreatePull talks to the create pull endpoint +func (a *ACH) CreatePull( + accessToken string, + userSecret string, + pullTransfer CreatePull, + userInputs []response.UserInput, + operationID string, +) (*response.BaseResponse, error) { + + baseRequest := &request.CreateACHPullRequest{ + BaseRequest: request.BaseRequest{ + UserSecret: userSecret, + AppSecret: a.Config.AppSecret, + UserInputs: userInputs, + OperationID: operationID, + }, + Transfer: pullTransfer.Transfer, + } + + jsonData, err := json.Marshal(baseRequest) + if err != nil { + return nil, err + } + + baseHeader := &request.BaseHeader{ + AccessToken: accessToken, + } + + body, err := request.DapiRequest(jsonData, constants.DAPI_URL.ACH_URLS.CREATE_PULL, request.GetHTTPHeader(baseHeader)) + if err != nil { + return nil, err + } + + res := response.BaseResponse{} + + err = json.Unmarshal(body, &res) + if err != nil { + return nil, err + } + + return &res, nil +} + +func (a *ACH) GetPull( + accessToken string, + userSecret string, + userInputs []response.UserInput, + operationID string, +) (*response.GetACHPullResponse, error) { + + baseRequest := &request.BaseRequest{ + UserSecret: userSecret, + AppSecret: a.Config.AppSecret, + UserInputs: userInputs, + OperationID: operationID, + } + + jsonData, err := json.Marshal(baseRequest) + if err != nil { + return nil, err + } + + baseHeader := &request.BaseHeader{ + AccessToken: accessToken, + } + + body, err := request.DapiRequest(jsonData, constants.DAPI_URL.ACH_URLS.GET_PULL, request.GetHTTPHeader(baseHeader)) + if err != nil { + return nil, err + } + + res := response.GetACHPullResponse{} + + err = json.Unmarshal(body, &res) + if err != nil { + return nil, err + } + + return &res, nil +} diff --git a/constants/url.go b/constants/url.go index 32afe29..338bb66 100644 --- a/constants/url.go +++ b/constants/url.go @@ -29,6 +29,11 @@ type operationEndpoints struct { OPERATION_STATUS string } +type achEndpoints struct { + CREATE_PULL string + GET_PULL string +} + type dapiEndpoints struct { BASE_URL string DATA_URLS dataEndpoints @@ -36,6 +41,7 @@ type dapiEndpoints struct { PAYMENT_URLS paymentEndpoints AUTH_URLS authEndpoints OPERATION_URLS operationEndpoints + ACH_URLS achEndpoints } const DD_URL = "https://dd.dapi.com" @@ -68,4 +74,8 @@ var DAPI_URL = &dapiEndpoints{ OPERATION_URLS: operationEndpoints{ OPERATION_STATUS: "/operation/status", }, + ACH_URLS: achEndpoints{ + CREATE_PULL: "/ach/pull/create", + GET_PULL: "/ach/pull/get", + }, } diff --git a/request/ach.go b/request/ach.go new file mode 100644 index 0000000..73f0022 --- /dev/null +++ b/request/ach.go @@ -0,0 +1,8 @@ +package request + +// ACHPullTransfer represents the transfer details +type ACHPullTransfer struct { + SenderID string `json:"senderID"` + Amount float64 `json:"amount"` + Description string `json:"description,omitempty"` +} diff --git a/request/request.go b/request/request.go index 71c36f9..59a5ae6 100644 --- a/request/request.go +++ b/request/request.go @@ -73,6 +73,13 @@ type BeneficiaryRequest struct { CreateBeneficiaryInfo } +// CreateACHPullRequest holds the fields that's needed by the ACH's +// create pull endpoint. +type CreateACHPullRequest struct { + BaseRequest + Transfer ACHPullTransfer `json:"transfer"` +} + type NoHeader struct{} // BaseHeader holds any fields that's needed in the header of the request diff --git a/response/ach.go b/response/ach.go new file mode 100644 index 0000000..0b43f5e --- /dev/null +++ b/response/ach.go @@ -0,0 +1,7 @@ +package response + +type ACHPullTransferInfo struct { + Amount float64 `json:"amount,omitempty"` + Currency Currency `json:"currency,omitempty"` + Status string `json:"status,omitempty"` +} diff --git a/response/response.go b/response/response.go index 1b3eb9d..6e3d79a 100644 --- a/response/response.go +++ b/response/response.go @@ -15,9 +15,9 @@ type IBaseResponse interface { } type BaseResponse struct { - OperationID string `json:"operationID,omitempty"` - Success bool `json:"success,omitempty"` - Status constants.ApiStatus `json:"status,omitempty"` + OperationID string `json:"operationID"` + Success bool `json:"success"` + Status constants.ApiStatus `json:"status"` UserInputs []UserInput `json:"userInputs,omitempty"` Type string `json:"type,omitempty"` Msg string `json:"msg,omitempty"` @@ -103,3 +103,8 @@ type AccountsMetadataResponse struct { BaseResponse AccountsMetadata GetAccountsMetadata `json:"accountsMetadata,omitempty"` } + +type GetACHPullResponse struct { + BaseResponse + Transfer *ACHPullTransferInfo `json:"transfer,omitempty"` +}