From 05b8942c1a1d89892e59cb368ef73b458922af05 Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Sat, 21 Dec 2024 04:35:48 +0200 Subject: [PATCH] Fix the transaction result object --- internal/stubs/refund.go | 26 +++++++++++++++++++++++++- refund.go | 11 +++++++---- refund_service_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/internal/stubs/refund.go b/internal/stubs/refund.go index 99b2403..145d1b4 100644 --- a/internal/stubs/refund.go +++ b/internal/stubs/refund.go @@ -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", @@ -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" +} +`) +} diff --git a/refund.go b/refund.go index 2cbecde..6e73514 100644 --- a/refund.go +++ b/refund.go @@ -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"` @@ -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")) } diff --git a/refund_service_test.go b/refund_service_test.go index f7b02a8..282fda5 100644 --- a/refund_service_test.go +++ b/refund_service_test.go @@ -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()