Skip to content

Commit

Permalink
Fix the transaction result object
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Dec 21, 2024
1 parent d0fa02e commit 05b8942
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
26 changes: 25 additions & 1 deletion internal/stubs/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func RefundStatusResponse() []byte {
},
"parameters": {
"amount": "98",
"xauth": "WU5PVEVIRUFEOllOT1RFSEVBRDIwMjA=",
"xauth": "WU5PVEVIRUFExxxxxxxxxxx",
"channel_user_msisdn": "69xxxxxx",
"customer_key": "2fBAAq_xxxxxxx",
"customer_secret": "34nFkKxxxxxx",
Expand All @@ -64,3 +64,27 @@ func RefundStatusResponse() []byte {
}
`)
}

// RefundStatusResponseWithFailure represents a refund status response with a failure
func RefundStatusResponseWithFailure() []byte {
return []byte(`
{
"ErrorCode":5019,
"body":"Exception on getToken",
"ErrorMessage":"[Errno 97] Address family not supported by protocol",
"status":"FAILED",
"parameters":{
"amount":"10182",
"xauth":"WU5PVEVIRUFEOxxxxxxxxxxxxxx",
"channel_user_msisdn":"693xxxxxx",
"customer_key":"2fBAAq_xxxxxxxxxxxxxxxxxxxx",
"customer_secret":"34nFxxxxxxxxxxxxxxxxxxxxx",
"final_customer_name":"Arnold",
"final_customer_phone":"693xxxxxx"
},
"CreateAt":"12-20-2024 12:14:38",
"MessageId":"90be9142-4216-4113-bdc4-d1d11ede2460",
"RefundStep":"1"
}
`)
}
11 changes: 7 additions & 4 deletions refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ type RefundTransaction struct {

// RefundTransactionStatus is the response from a refund transaction status
type RefundTransactionStatus struct {
Result struct {
Status *string `json:"status"`
ErrorCode *int `json:"ErrorCode"`
ErrorMessage *string `json:"ErrorMessage"`
Result *struct {
Message string `json:"message"`
Data struct {
CreatedAt string `json:"createtime"`
Expand Down Expand Up @@ -67,15 +70,15 @@ type RefundTransactionStatus struct {

// IsPending checks if the refund transaction is pending
func (status *RefundTransactionStatus) IsPending() bool {
return status.Result.Data.Status == "" || status.Result.Data.Status == "PENDING" || status.Result.Data.Status == "INITIATED"
return status.Status == nil && (status.Result == nil || status.Result.Data.Status == "" || status.Result.Data.Status == "PENDING" || status.Result.Data.Status == "INITIATED")
}

// IsSuccessful checks if the refund transaction is successful
func (status *RefundTransactionStatus) IsSuccessful() bool {
return status.Result.Data.Status == "SUCCESSFULL" || status.Result.Data.Status == "SUCCESSFUL"
return status.Result != nil && (status.Result.Data.Status == "SUCCESSFULL" || status.Result.Data.Status == "SUCCESSFUL")
}

// IsFailed checks if the refund transaction is failed
func (status *RefundTransactionStatus) IsFailed() bool {
return status.Result.Data.Status == "FAILED" || status.Result.Data.Status == "EXPIRED"
return (status.Status != nil && *status.Status == "FAILED") || (status.ErrorCode != nil && *status.ErrorCode == 5019) || (status.Result != nil && (status.Result.Data.Status == "FAILED" || status.Result.Data.Status == "EXPIRED"))
}
30 changes: 30 additions & 0 deletions refund_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ func TestRefundService_Status(t *testing.T) {
server.Close()
}

func TestRefundService_StatusWithFailure(t *testing.T) {
// Setup
t.Parallel()

// Arrange
requests := make([]http.Request, 0)
responses := [][]byte{stubs.TokenResponse(), stubs.RefundStatusResponseWithFailure()}
server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests)
client := New(
WithTokenURL(server.URL),
WithAPIURL(server.URL),
WithClientID(testClientID),
WithClientSecret(testClientSecret),
)

// Act
transaction, response, err := client.Refund.Status(context.Background(), "")

// Assert
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
assert.Nil(t, transaction.Result)
assert.True(t, transaction.IsFailed())
assert.False(t, transaction.IsSuccessful())
assert.False(t, transaction.IsPending())

// Teardown
server.Close()
}

func TestRefundService_StatusWithError(t *testing.T) {
// Setup
t.Parallel()
Expand Down

0 comments on commit 05b8942

Please # to comment.