1
|
|
|
package afrikpay |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"crypto/md5" |
6
|
|
|
"encoding/json" |
7
|
|
|
"fmt" |
8
|
|
|
"io/ioutil" |
9
|
|
|
"net/http" |
10
|
|
|
"testing" |
11
|
|
|
|
12
|
|
|
"github.com/NdoleStudio/afrikpay-go/internal/helpers" |
13
|
|
|
"github.com/NdoleStudio/afrikpay-go/internal/stubs" |
14
|
|
|
"github.com/stretchr/testify/assert" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
func TestBillService_Pay_Request(t *testing.T) { |
18
|
|
|
// Setup |
19
|
|
|
t.Parallel() |
20
|
|
|
|
21
|
|
|
// Arrange |
22
|
|
|
requests := make([]*http.Request, 0) |
23
|
|
|
apiKey := "api-key" |
24
|
|
|
agentID := "agent-id" |
25
|
|
|
agentPlatform := "agent-platform" |
26
|
|
|
agentPassword := "agent-password" |
27
|
|
|
processingNumber := "processing-number" |
28
|
|
|
billerID := "999999999" |
29
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.BillPay()}, &requests) |
30
|
|
|
client := New( |
31
|
|
|
WithBaseURL(server.URL), |
32
|
|
|
WithAgentID(agentID), |
33
|
|
|
WithAPIKey(apiKey), |
34
|
|
|
WithAgentPassword(agentPassword), |
35
|
|
|
WithAgentPlatform(agentPlatform), |
36
|
|
|
) |
37
|
|
|
params := BillPayParams{ |
38
|
|
|
Biller: BillerEneoPostpay, |
39
|
|
|
BillID: billerID, |
40
|
|
|
Mode: ModeCash, |
41
|
|
|
ProcessingNumber: StringToPointer(processingNumber), |
42
|
|
|
} |
43
|
|
|
expectedRequest := map[string]interface{}{ |
44
|
|
|
"biller": params.Biller.string(), |
45
|
|
|
"billid": params.BillID, |
46
|
|
|
"mode": params.Mode.String(), |
47
|
|
|
"processingnumber": PointerToString(params.ProcessingNumber), |
48
|
|
|
"agentid": agentID, |
49
|
|
|
"agentplatform": agentPlatform, |
50
|
|
|
"agentpwd": agentPassword, |
51
|
|
|
"hash": fmt.Sprintf("%x", md5.Sum([]byte(params.Biller.string()+params.BillID+apiKey))), |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Act |
55
|
|
|
_, _, err := client.Bill.Pay(context.Background(), params) |
56
|
|
|
assert.NoError(t, err) |
57
|
|
|
|
58
|
|
|
assert.Equal(t, 1, len(requests)) |
59
|
|
|
buf, err := ioutil.ReadAll(requests[0].Body) |
60
|
|
|
assert.NoError(t, err) |
61
|
|
|
|
62
|
|
|
requestBody := map[string]interface{}{} |
63
|
|
|
err = json.Unmarshal(buf, &requestBody) |
64
|
|
|
assert.NoError(t, err) |
65
|
|
|
|
66
|
|
|
// Assert |
67
|
|
|
assert.Equal(t, expectedRequest, requestBody) |
68
|
|
|
|
69
|
|
|
// Teardown |
70
|
|
|
server.Close() |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
func TestBillService_Pay(t *testing.T) { |
74
|
|
|
// Setup |
75
|
|
|
t.Parallel() |
76
|
|
|
|
77
|
|
|
// Arrange |
78
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.BillPay()) |
79
|
|
|
client := New(WithBaseURL(server.URL)) |
80
|
|
|
params := BillPayParams{} |
81
|
|
|
|
82
|
|
|
// Act |
83
|
|
|
transaction, response, err := client.Bill.Pay(context.Background(), params) |
84
|
|
|
|
85
|
|
|
// Assert |
86
|
|
|
assert.NoError(t, err) |
87
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
88
|
|
|
assert.Equal(t, &BillResponse{ |
89
|
|
|
Code: 200, |
90
|
|
|
Message: "success", |
91
|
|
|
Result: &BillTransaction{ |
92
|
|
|
OperatorID: "xxxx-xxxx-xxxx-xxxx-5286 : 0000000000068 : 8.8 Kwh", |
93
|
|
|
TransactionID: "5xxxx", |
94
|
|
|
Status: "PENDING", |
95
|
|
|
Date: "2022-04-19 18:00:06", |
96
|
|
|
ReferenceID: nil, |
97
|
|
|
ProcessingNumber: "aaba045a-d571-41e9-9ea4-54cd78782e03", |
98
|
|
|
}, |
99
|
|
|
}, transaction) |
100
|
|
|
|
101
|
|
|
assert.True(t, transaction.IsSuccessful()) |
102
|
|
|
|
103
|
|
|
// Teardown |
104
|
|
|
server.Close() |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
func TestBillService_Pay_WithError(t *testing.T) { |
108
|
|
|
// Setup |
109
|
|
|
t.Parallel() |
110
|
|
|
|
111
|
|
|
// Arrange |
112
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.BillPayWithError()) |
113
|
|
|
client := New(WithBaseURL(server.URL)) |
114
|
|
|
params := AirtimeTransferParams{} |
115
|
|
|
|
116
|
|
|
// Act |
117
|
|
|
transaction, _, err := client.Airtime.Transfer(context.Background(), params) |
118
|
|
|
|
119
|
|
|
// Assert |
120
|
|
|
assert.NoError(t, err) |
121
|
|
|
assert.Nil(t, transaction.Result) |
122
|
|
|
|
123
|
|
|
assert.Equal(t, "General Failure", transaction.Message) |
124
|
|
|
assert.Equal(t, http.StatusInternalServerError, transaction.Code) |
125
|
|
|
assert.Nil(t, transaction.Result) |
126
|
|
|
assert.False(t, transaction.IsSuccessful()) |
127
|
|
|
|
128
|
|
|
// Teardown |
129
|
|
|
server.Close() |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
func TestBillService_Status_Request(t *testing.T) { |
133
|
|
|
// Setup |
134
|
|
|
t.Parallel() |
135
|
|
|
|
136
|
|
|
// Arrange |
137
|
|
|
requests := make([]*http.Request, 0) |
138
|
|
|
apiKey := "api-key" |
139
|
|
|
agentID := "agent-id" |
140
|
|
|
agentPlatform := "agent-platform" |
141
|
|
|
transactionID := "aaba045a-d571-41e9-9ea4-54cd78782e03" |
142
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.BillPay()}, &requests) |
143
|
|
|
client := New( |
144
|
|
|
WithBaseURL(server.URL), |
145
|
|
|
WithAgentID(agentID), |
146
|
|
|
WithAPIKey(apiKey), |
147
|
|
|
WithAgentPlatform(agentPlatform), |
148
|
|
|
) |
149
|
|
|
expectedRequest := map[string]interface{}{ |
150
|
|
|
"processingnumber": transactionID, |
151
|
|
|
"agentid": agentID, |
152
|
|
|
"agentplatform": agentPlatform, |
153
|
|
|
"hash": fmt.Sprintf("%x", md5.Sum([]byte(transactionID+apiKey))), |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Act |
157
|
|
|
_, _, err := client.Bill.Status(context.Background(), transactionID) |
158
|
|
|
assert.NoError(t, err) |
159
|
|
|
|
160
|
|
|
assert.Equal(t, 1, len(requests)) |
161
|
|
|
buf, err := ioutil.ReadAll(requests[0].Body) |
162
|
|
|
assert.NoError(t, err) |
163
|
|
|
|
164
|
|
|
requestBody := map[string]interface{}{} |
165
|
|
|
err = json.Unmarshal(buf, &requestBody) |
166
|
|
|
assert.NoError(t, err) |
167
|
|
|
|
168
|
|
|
// Assert |
169
|
|
|
assert.Equal(t, expectedRequest, requestBody) |
170
|
|
|
|
171
|
|
|
// Teardown |
172
|
|
|
server.Close() |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
func TestBillService_Status(t *testing.T) { |
176
|
|
|
// Setup |
177
|
|
|
t.Parallel() |
178
|
|
|
|
179
|
|
|
// Arrange |
180
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.BillPay()) |
181
|
|
|
client := New(WithBaseURL(server.URL)) |
182
|
|
|
transactionID := "aaba045a-d571-41e9-9ea4-54cd78782e03" |
183
|
|
|
|
184
|
|
|
// Act |
185
|
|
|
transaction, response, err := client.Bill.Status(context.Background(), transactionID) |
186
|
|
|
|
187
|
|
|
// Assert |
188
|
|
|
assert.NoError(t, err) |
189
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
190
|
|
|
assert.Equal(t, &BillResponse{ |
191
|
|
|
Code: 200, |
192
|
|
|
Message: "success", |
193
|
|
|
Result: &BillTransaction{ |
194
|
|
|
OperatorID: "xxxx-xxxx-xxxx-xxxx-5286 : 0000000000068 : 8.8 Kwh", |
195
|
|
|
TransactionID: "5xxxx", |
196
|
|
|
Status: "PENDING", |
197
|
|
|
Date: "2022-04-19 18:00:06", |
198
|
|
|
ReferenceID: nil, |
199
|
|
|
ProcessingNumber: "aaba045a-d571-41e9-9ea4-54cd78782e03", |
200
|
|
|
}, |
201
|
|
|
}, transaction) |
202
|
|
|
|
203
|
|
|
assert.True(t, transaction.IsSuccessful()) |
204
|
|
|
|
205
|
|
|
// Teardown |
206
|
|
|
server.Close() |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
func TestBillService_Status_WithError(t *testing.T) { |
210
|
|
|
// Setup |
211
|
|
|
t.Parallel() |
212
|
|
|
|
213
|
|
|
// Arrange |
214
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.BillPayWithError()) |
215
|
|
|
client := New(WithBaseURL(server.URL)) |
216
|
|
|
transactionID := "transaction-id" |
217
|
|
|
|
218
|
|
|
// Act |
219
|
|
|
transaction, _, err := client.Airtime.Status(context.Background(), transactionID) |
220
|
|
|
|
221
|
|
|
// Assert |
222
|
|
|
assert.NoError(t, err) |
223
|
|
|
assert.Nil(t, transaction.Result) |
224
|
|
|
|
225
|
|
|
assert.Equal(t, "General Failure", transaction.Message) |
226
|
|
|
assert.Equal(t, http.StatusInternalServerError, transaction.Code) |
227
|
|
|
assert.Nil(t, transaction.Result) |
228
|
|
|
assert.False(t, transaction.IsSuccessful()) |
229
|
|
|
|
230
|
|
|
// Teardown |
231
|
|
|
server.Close() |
232
|
|
|
} |
233
|
|
|
|