Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kachit committed Apr 2, 2022
1 parent 6df6d14 commit 6438fb2
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 35 deletions.
4 changes: 2 additions & 2 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type AccountsResource struct {
*ResourceAbstract
}

//GetBalances method
//GetBalances method - allow you to check your FasaPay account balance.
func (r *AccountsResource) GetBalances(currencies []CurrencyCode, ctx context.Context, attributes *RequestParamsAttributes) (*GetBalancesResponse, *http.Response, error) {
baseRequestParams := r.buildRequestParams(attributes)
requestParams := &GetBalancesRequest{baseRequestParams, currencies}
Expand All @@ -73,7 +73,7 @@ func (r *AccountsResource) GetBalances(currencies []CurrencyCode, ctx context.Co
return &result, rsp, nil
}

//GetAccounts method
//GetAccounts method - allow you to check specific FasaPay account, to indicate is it registered or not.
func (r *AccountsResource) GetAccounts(accounts []string, ctx context.Context, attributes *RequestParamsAttributes) (*GetAccountsResponse, *http.Response, error) {
baseRequestParams := r.buildRequestParams(attributes)
requestParams := &GetAccountsRequest{baseRequestParams, accounts}
Expand Down
28 changes: 23 additions & 5 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@ package fasapay
//CurrencyCode type
type CurrencyCode string

//CurrencyCodeUSD type
//CurrencyCodeUSD const
const CurrencyCodeUSD CurrencyCode = "USD"

//CurrencyCodeUSD type
//CurrencyCodeUSD const
const CurrencyCodeIDR CurrencyCode = "IDR"

//TransactionFeeMode type
type TransactionFeeMode string

//TransactionFeeModeFiR type
//TransactionFeeModeFiR const
const TransactionFeeModeFiR TransactionFeeMode = "FiR"

//TransactionFeeModeFiS type
//TransactionFeeModeFiS const
const TransactionFeeModeFiS TransactionFeeMode = "FiS"

//TransactionStatus type
type TransactionStatus string

//TransactionFeeModeFiR type
//TransactionStatusFinish const
const TransactionStatusFinish TransactionStatus = "FINISH"

//TransactionType type
type TransactionType string

//TransactionTypeTransfer const
const TransactionTypeTransfer = "transfer"

//TransactionTypeTopUp const
const TransactionTypeTopUp = "topup"

//TransactionTypeRedeem const
const TransactionTypeRedeem = "redeem"

//TransactionTypeExchange const
const TransactionTypeExchange = "exchange"

//TransactionTypeReceive const
const TransactionTypeReceive = "receive"
16 changes: 8 additions & 8 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const (
)

const (
ErrorMessageNotValidXmlRequest string = "NOT VALID XML REQUEST"
ErrorMessageUnauthorized string = "UNAUTHORIZED"
ErrorMessageNotAcceptableTransfer string = "NOT ACCEPTABLE TRANSFER"
ErrorMessageDetailRequestError string = "DETAIL REQUEST ERROR"
ErrorMessageHistoryRequestError string = "HISTORY REQUEST ERROR"
ErrorMessageBalanceRequestError string = "BALANCE REQUEST ERROR"
ErrorMessageAccountRequestError string = "ACCOUNT REQUEST ERROR"
ErrorMessageUnexpectedError string = "UNEXPECTED ERROR"
ErrorMessageNotValidXmlRequest string = "NOT VALID XML REQUEST" //The sended XML are not valid, broken or has wrong format
ErrorMessageUnauthorized string = "UNAUTHORIZED" //Authorisation failed.
ErrorMessageNotAcceptableTransfer string = "NOT ACCEPTABLE TRANSFER" //There is an error in the transfer operation
ErrorMessageDetailRequestError string = "DETAIL REQUEST ERROR" //There is an error in the detail operation
ErrorMessageHistoryRequestError string = "HISTORY REQUEST ERROR" //There is an error in the history operation
ErrorMessageBalanceRequestError string = "BALANCE REQUEST ERROR" //There is an error in the balance operation
ErrorMessageAccountRequestError string = "ACCOUNT REQUEST ERROR" //There is an error in the account operation
ErrorMessageUnexpectedError string = "UNEXPECTED ERROR" //Unexpected error
)
69 changes: 49 additions & 20 deletions transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@ import (

//CreateTransferRequestParams struct
type CreateTransferRequestParams struct {
XMLName xml.Name `xml:"transfer"`
Id string `xml:"id,attr,omitempty" json:"id"`
To string `xml:"to" json:"to"`
Amount float64 `xml:"amount" json:"amount"`
Currency CurrencyCode `xml:"currency" json:"currency"`
FeeMode string `xml:"fee_mode,omitempty" json:"fee_mode"`
Note string `xml:"note,omitempty" json:"note"`
Ref string `xml:"ref,omitempty" json:"ref"`
XMLName xml.Name `xml:"transfer"`
Id string `xml:"id,attr,omitempty" json:"id"` //id transfer for marking the transfer (max 50 character)
To string `xml:"to" json:"to"` //is the FasaPay account target format: FPnnnnn
Amount float64 `xml:"amount" json:"amount"` //is the amount of the transferred fund. with point (.) as the decimal separator
Currency CurrencyCode `xml:"currency" json:"currency"` //is the currency used in the transfer (IDR | USD)
FeeMode TransactionFeeMode `xml:"fee_mode,omitempty" json:"fee_mode"` //is Fee Mode used in the transfer. default to FiR (FiR | FiS)
Note string `xml:"note,omitempty" json:"note"` //is note of the transfer (max 255 character)
Ref string `xml:"ref,omitempty" json:"ref"` //Reference Code that can be used to track transaction (max 50 character)
}

//isValid method
func (ctr *CreateTransferRequestParams) isValid() error {
var err error
if ctr.To == "" {
err = fmt.Errorf(`parameter "to" is empty`)
} else if ctr.Currency == "" {
err = fmt.Errorf(`parameter "currency" is empty`)
} else if ctr.Amount == 0 {
err = fmt.Errorf(`parameter "amount" is empty`)
}
return err
}

//CreateTransferRequest struct
Expand Down Expand Up @@ -60,13 +73,13 @@ type GetHistoryRequest struct {

//GetHistoryRequestParams struct
type GetHistoryRequestParams struct {
StartDate string `xml:"start_date,omitempty" json:"start_date"`
EndDate string `xml:"end_date,omitempty" json:"end_date"`
Type string `xml:"type,omitempty" json:"type"`
OrderBy string `xml:"order_by,omitempty" json:"order_by"`
Order string `xml:"order,omitempty" json:"order"`
Page uint64 `xml:"page,omitempty" json:"page"`
PageSize uint64 `xml:"page_size,omitempty" json:"page_size"`
StartDate string `xml:"start_date,omitempty" json:"start_date"` //for specify start date. format : YYYY-mm-dd example : 2011-03-01
EndDate string `xml:"end_date,omitempty" json:"end_date"` //for specify end date. format : YYYY-mm-dd example : 2011-03-01
Type TransactionType `xml:"type,omitempty" json:"type"` //for specify transaction type. (transfer|topup|redeem|exchange|receive)
OrderBy string `xml:"order_by,omitempty" json:"order_by"` //for specify order/sort by specific parameters (sorting) (date|amount|to|from|currency|bank)
Order string `xml:"order,omitempty" json:"order"` //specify order type (ASC|DESC)
Page uint64 `xml:"page,omitempty" json:"page"` //for getting specific page from history transaction which has more than one page
PageSize uint64 `xml:"page_size,omitempty" json:"page_size"` //for specify how much transaction per page (max 20)
}

//GetHistoryResponse struct
Expand Down Expand Up @@ -117,8 +130,8 @@ type GetDetailsDetailParamsInterface interface {
//GetDetailsRequestDetailParamsStruct struct
type GetDetailsRequestDetailParamsStruct struct {
XMLName xml.Name `xml:"detail" json:"-"`
Ref string `xml:"ref,omitempty" json:"ref,omitempty"`
Note string `xml:"note,omitempty" json:"note,omitempty"`
Ref string `xml:"ref,omitempty" json:"ref,omitempty"` //REF parameter used to search for spesific fp_merchant_ref string that was saved by FasaPay during Transaction using SCI
Note string `xml:"note,omitempty" json:"note,omitempty"` //NOTE Parameter used to search for spesific note string that was saved by FasaPay During Transaction.
}

//GetDetailType method implementation
Expand Down Expand Up @@ -166,8 +179,12 @@ type TransfersResource struct {
*ResourceAbstract
}

//CreateTransfer method
//CreateTransfer method - allow you to transfer fund from one account to another. With this command you may transfer any of the available currencies that FasaPay supports. This function also permits you to perform multiple (bulk) transfers.
func (r *TransfersResource) CreateTransfer(transfers []*CreateTransferRequestParams, ctx context.Context, attributes *RequestParamsAttributes) (*CreateTransferResponse, *http.Response, error) {
err := r.validateTransferParams(transfers)
if err != nil {
return nil, nil, fmt.Errorf("TransfersResource.CreateTransfer error: %v", err)
}
baseRequestParams := r.buildRequestParams(attributes)
requestParams := &CreateTransferRequest{baseRequestParams, transfers}
bytesRequest, err := r.marshalRequestParams(requestParams)
Expand All @@ -189,7 +206,7 @@ func (r *TransfersResource) CreateTransfer(transfers []*CreateTransferRequestPar
return &result, rsp, nil
}

//GetHistory method
//GetHistory method - allow you to receive history transaction of your FasaPay account. this command has many additional parameter to filter the response like date range, currencies, type of transaction, account target, etc.
func (r *TransfersResource) GetHistory(history *GetHistoryRequestParams, ctx context.Context, attributes *RequestParamsAttributes) (*GetHistoryResponse, *http.Response, error) {
baseRequestParams := r.buildRequestParams(attributes)
requestParams := &GetHistoryRequest{baseRequestParams, history}
Expand All @@ -212,7 +229,7 @@ func (r *TransfersResource) GetHistory(history *GetHistoryRequestParams, ctx con
return &result, rsp, nil
}

//GetDetails method
//GetDetails method - allow you to receive detail information of specific transaction. You can include more than one of this command in single request.
func (r *TransfersResource) GetDetails(details []GetDetailsDetailParamsInterface, ctx context.Context, attributes *RequestParamsAttributes) (*GetDetailsResponse, *http.Response, error) {
baseRequestParams := r.buildRequestParams(attributes)
requestParams := &GetDetailsRequest{baseRequestParams, details}
Expand All @@ -234,3 +251,15 @@ func (r *TransfersResource) GetDetails(details []GetDetailsDetailParamsInterface
}
return &result, rsp, nil
}

//validateTransferParams method
func (r *TransfersResource) validateTransferParams(transfers []*CreateTransferRequestParams) error {
var err error
for _, transfer := range transfers {
err = transfer.isValid()
if err != nil {
break
}
}
return err
}
107 changes: 107 additions & 0 deletions transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,54 @@ func Test_Transfers_CreateTransferRequest_MarshalXmlSuccess(t *testing.T) {
assert.Equal(t, expected, string(bytes))
}

func Test_Transfers_CreateTransferRequest_IsValidSuccess(t *testing.T) {
transfer := &CreateTransferRequestParams{
Id: "123",
To: "FP89680",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
assert.Nil(t, transfer.isValid())
assert.NoError(t, transfer.isValid())
}

func Test_Transfers_CreateTransferRequest_IsValidEmptyParameterTo(t *testing.T) {
transfer := &CreateTransferRequestParams{
Id: "123",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
result := transfer.isValid()
assert.Error(t, result)
assert.Equal(t, `parameter "to" is empty`, result.Error())
}

func Test_Transfers_CreateTransferRequest_IsValidEmptyParameterCurrency(t *testing.T) {
transfer := &CreateTransferRequestParams{
Id: "123",
To: "FP89680",
Amount: 1000.0,
Note: "standart operation",
}
result := transfer.isValid()
assert.Error(t, result)
assert.Equal(t, `parameter "currency" is empty`, result.Error())
}

func Test_Transfers_CreateTransferRequest_IsValidEmptyParameterAmount(t *testing.T) {
transfer := &CreateTransferRequestParams{
Id: "123",
To: "FP89680",
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
result := transfer.isValid()
assert.Error(t, result)
assert.Equal(t, `parameter "amount" is empty`, result.Error())
}

func Test_Transfers_TransfersResource_CreateTransferSuccess(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand Down Expand Up @@ -365,6 +413,31 @@ func Test_Transfers_TransfersResource_CreateTransferSuccess(t *testing.T) {
assert.Equal(t, body, bodyRsp)
}

func Test_Transfers_TransfersResource_CreateTransferRequestError(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

cfg := BuildStubConfig()
transport := BuildStubHttpTransport()

resource := &TransfersResource{ResourceAbstract: NewResourceAbstract(transport, cfg)}

ctx := context.Background()
transfer := &CreateTransferRequestParams{
Id: "123",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
result, resp, err := resource.CreateTransfer([]*CreateTransferRequestParams{transfer}, ctx, nil)

assert.Error(t, err)
assert.Empty(t, resp)
assert.Empty(t, result)
//error
assert.Equal(t, `TransfersResource.CreateTransfer error: parameter "to" is empty`, err.Error())
}

func Test_Transfers_TransfersResource_CreateTransferXmlError(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand Down Expand Up @@ -452,6 +525,40 @@ func Test_Transfers_TransfersResource_CreateTransferNonXmlError(t *testing.T) {
assert.Equal(t, "TransfersResource.CreateTransfer error: EOF", err.Error())
}

func Test_Transfers_TransfersResource_ValidateTransferParamsValid(t *testing.T) {
resource := &TransfersResource{}
transfer := &CreateTransferRequestParams{
Id: "123",
To: "FP89680",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
err := resource.validateTransferParams([]*CreateTransferRequestParams{transfer})
assert.Nil(t, err)
assert.NoError(t, err)
}

func Test_Transfers_TransfersResource_ValidateTransferParamsInvalid(t *testing.T) {
resource := &TransfersResource{}
transfer1 := &CreateTransferRequestParams{
Id: "123",
To: "FP89680",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
transfer2 := &CreateTransferRequestParams{
Id: "123",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
err := resource.validateTransferParams([]*CreateTransferRequestParams{transfer1, transfer2})
assert.Error(t, err)
assert.Equal(t, `parameter "to" is empty`, err.Error())
}

func Test_Transfers_CreateTransferResponse_MarshalJsonSuccess(t *testing.T) {
var response CreateTransferResponse
body, _ := LoadStubResponseData("stubs/transfers/transfer/success.xml")
Expand Down

0 comments on commit 6438fb2

Please sign in to comment.