Skip to content

Commit 3f0c003

Browse files
committed
Add Api to get bill status
1 parent c0551bf commit 3f0c003

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import "github.com/NdoleStudio/flutterwave-go"
3030
- [BILLS](#bills)
3131
- `POST /bills/`: Create a bill payment
3232
- `GET /bill-items/{item_code}/validate`: Validate services like DSTV smartcard number, Meter number etc.
33+
- `GET /bills/{reference}`: Get the verbose status of a bill payment
3334

3435
## Usage
3536

@@ -101,6 +102,19 @@ if err != nil {
101102
log.Println(response.Status) // success
102103
```
103104

105+
#### Get verbose status of a bill payment
106+
107+
`GET /bills/{reference}`: get the verbose status of a bill purchase
108+
109+
```go
110+
response, _, err := flutterwaveClient.Bills.GetStatusVerbose(context.Background(), "9300049404444")
111+
112+
if err != nil {
113+
log.Fatal(err)
114+
}
115+
116+
log.Println(response.Status) // success
117+
```
104118

105119
## Testing
106120

bills_service.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,25 @@ func (service *billsService) Validate(ctx context.Context, itemCode string, bill
5757

5858
return &data, response, nil
5959
}
60+
61+
// GetStatusVerbose gets the verbose status of a bill payment.
62+
//
63+
// API Docs: https://developer.flutterwave.com/reference/get-status-of-a-bill-payment
64+
func (service *billsService) GetStatusVerbose(ctx context.Context, transactionReference string) (*BillsStatusVerboseResponse, *Response, error) {
65+
request, err := service.client.newRequest(ctx, http.MethodGet, "/v3/bills/"+transactionReference, map[string]int{"verbose": 1})
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
70+
response, err := service.client.do(request)
71+
if err != nil {
72+
return nil, response, err
73+
}
74+
75+
var data BillsStatusVerboseResponse
76+
if err = json.Unmarshal(*response.Body, &data); err != nil {
77+
return nil, response, err
78+
}
79+
80+
return &data, response, nil
81+
}

bills_service_models.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package flutterwave
22

3+
import "time"
4+
35
// BillsCreatePaymentRequest is data needed to creat a payment
46
type BillsCreatePaymentRequest struct {
57
Country string `json:"country"`
@@ -24,6 +26,11 @@ type BillsCreatePaymentResponse struct {
2426
} `json:"data"`
2527
}
2628

29+
// IsSuccessfull determines if the bill payment was successfull
30+
func (response BillsCreatePaymentResponse) IsSuccessfull() bool {
31+
return response.Status == "success" && response.Data.TxRef != ""
32+
}
33+
2734
// BillsValidateResponse is the response after validating a bill service
2835
type BillsValidateResponse struct {
2936
Status string `json:"status"`
@@ -42,3 +49,34 @@ type BillsValidateResponse struct {
4249
Minimum int `json:"minimum"`
4350
} `json:"data"`
4451
}
52+
53+
// IsSuccessfull determines if the bill validation was successfull
54+
func (response BillsValidateResponse) IsSuccessfull() bool {
55+
return response.Status == "success" && response.Data.Customer != ""
56+
}
57+
58+
// BillsStatusVerboseResponse is the verbose response of a bill payment
59+
type BillsStatusVerboseResponse struct {
60+
Status string `json:"status"`
61+
Message string `json:"message"`
62+
Data struct {
63+
Currency string `json:"currency"`
64+
CustomerID string `json:"customer_id"`
65+
Frequency string `json:"frequency"`
66+
Amount string `json:"amount"`
67+
Product string `json:"product"`
68+
ProductName string `json:"product_name"`
69+
Commission int `json:"commission"`
70+
TransactionDate time.Time `json:"transaction_date"`
71+
Country string `json:"country"`
72+
TxRef string `json:"tx_ref"`
73+
Extra interface{} `json:"extra"`
74+
ProductDetails string `json:"product_details"`
75+
Status string `json:"status"`
76+
} `json:"data"`
77+
}
78+
79+
// IsSuccessfull determines if the transaction was successfull
80+
func (response BillsStatusVerboseResponse) IsSuccessfull() bool {
81+
return response.Data.Status == "successful" && response.Data.TxRef != ""
82+
}

bills_service_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"net/http"
66
"testing"
7+
"time"
78

89
"github.com/NdoleStudio/flutterwave-go/internal/helpers"
910
"github.com/NdoleStudio/flutterwave-go/internal/stubs"
11+
"github.com/araddon/dateparse"
1012
"github.com/google/uuid"
1113
"github.com/stretchr/testify/assert"
1214
)
@@ -52,6 +54,7 @@ func TestBillsService_CreatePayment(t *testing.T) {
5254
}, data)
5355

5456
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
57+
assert.True(t, data.IsSuccessfull())
5558

5659
// Teardown
5760
server.Close()
@@ -102,6 +105,65 @@ func TestBillsService_Validate(t *testing.T) {
102105
}, data)
103106

104107
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
108+
assert.True(t, data.IsSuccessfull())
109+
110+
// Teardown
111+
server.Close()
112+
}
113+
114+
func TestBillsService_GetStatusVerbose(t *testing.T) {
115+
// Setup
116+
t.Parallel()
117+
118+
// Arrange
119+
server := helpers.MakeTestServer(http.StatusOK, stubs.BillsGetStatusVerboseResponse())
120+
client := New(WithBaseURL(server.URL))
121+
122+
// Act
123+
data, response, err := client.Bills.GetStatusVerbose(context.Background(), "9300049404444")
124+
125+
// Assert
126+
assert.Nil(t, err)
127+
128+
transactionDate, err := dateparse.ParseAny("2020-03-11T20:19:21.27Z")
129+
assert.Nil(t, err)
130+
131+
assert.Equal(t, &BillsStatusVerboseResponse{
132+
Status: "success",
133+
Message: "Bill status fetch successful",
134+
Data: struct {
135+
Currency string `json:"currency"`
136+
CustomerID string `json:"customer_id"`
137+
Frequency string `json:"frequency"`
138+
Amount string `json:"amount"`
139+
Product string `json:"product"`
140+
ProductName string `json:"product_name"`
141+
Commission int `json:"commission"`
142+
TransactionDate time.Time `json:"transaction_date"`
143+
Country string `json:"country"`
144+
TxRef string `json:"tx_ref"`
145+
Extra interface{} `json:"extra"`
146+
ProductDetails string `json:"product_details"`
147+
Status string `json:"status"`
148+
}{
149+
"NGN",
150+
"+23490803840303",
151+
"One Time",
152+
"500.0000",
153+
"AIRTIME",
154+
"9MOBILE",
155+
10,
156+
transactionDate,
157+
"NG",
158+
"CF-FLYAPI-20200311081921359990",
159+
nil,
160+
"FLY-API-NG-AIRTIME-9MOBILE",
161+
"successful",
162+
},
163+
}, data)
164+
165+
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
166+
assert.True(t, data.IsSuccessfull())
105167

106168
// Teardown
107169
server.Close()

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/NdoleStudio/flutterwave-go
33
go 1.17
44

55
require (
6+
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
67
github.com/google/uuid v1.3.0
78
github.com/stretchr/testify v1.7.0
89
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA=
2+
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw=
13
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
24
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
46
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7+
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
58
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
69
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
11+
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
712
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
813
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
914
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

internal/stubs/api_responses.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,28 @@ func BillsValidateDstvResponse() string {
3939
}
4040
`
4141
}
42+
43+
// BillsGetStatusVerboseResponse is a dummy response of a verbose bill status
44+
func BillsGetStatusVerboseResponse() string {
45+
return `
46+
{
47+
"status": "success",
48+
"message": "Bill status fetch successful",
49+
"data": {
50+
"currency": "NGN",
51+
"customer_id": "+23490803840303",
52+
"frequency": "One Time",
53+
"amount": "500.0000",
54+
"product": "AIRTIME",
55+
"product_name": "9MOBILE",
56+
"commission": 10,
57+
"transaction_date": "2020-03-11T20:19:21.27Z",
58+
"country": "NG",
59+
"tx_ref": "CF-FLYAPI-20200311081921359990",
60+
"extra": null,
61+
"product_details": "FLY-API-NG-AIRTIME-9MOBILE",
62+
"status": "successful"
63+
}
64+
}
65+
`
66+
}

0 commit comments

Comments
 (0)