generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbill_service.go
128 lines (111 loc) · 3.66 KB
/
bill_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package afrikpay
import (
"context"
"encoding/json"
"net/http"
)
// billService is the API client for the `/api/bill/` endpoint
type billService service
// Pay Bills or Subscriptions
//
// API Docs: https://developer.afrikpay.com/documentation/bill/v2/
func (service *billService) Pay(ctx context.Context, params BillPayParams) (*BillPayResponse, *Response, error) {
request, err := service.client.newRequest(
ctx,
http.MethodPost,
"/api/bill/v2/",
service.billPayParamsToPayload(params),
)
if err != nil {
return nil, nil, err
}
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
status := new(BillPayResponse)
if err = json.Unmarshal(*response.Body, status); err != nil {
return nil, response, err
}
return status, response, nil
}
// Status is intended for getting the status of an airtime transaction
//
// API Docs: https://developer.afrikpay.com/documentation/airtime/status/v2/
func (service *billService) Status(ctx context.Context, transactionID string) (*BillPayResponse, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/api/bill/status/v2/", map[string]string{
"processingnumber": transactionID,
"agentid": service.client.agentID,
"agentplatform": service.client.agentPlatform,
"hash": service.client.hash(transactionID, service.client.apiKey),
})
if err != nil {
return nil, nil, err
}
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
status := new(BillPayResponse)
if err = json.Unmarshal(*response.Body, status); err != nil {
return nil, response, err
}
return status, response, nil
}
// Amount is used to get the amount of a specific bill
//
// API Docs: https://developer.afrikpay.com/documentation/bill/getamount/
func (service *billService) Amount(ctx context.Context, biller Biller, billID string) (*BillAmountResponse, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/api/bill/getamount/", map[string]string{
"biller": biller.string(),
"billid": billID,
"agentid": service.client.agentID,
"agentplatform": service.client.agentPlatform,
"hash": service.client.hash(biller.string(), billID, service.client.apiKey),
})
if err != nil {
return nil, nil, err
}
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
status := new(BillAmountResponse)
if err = json.Unmarshal(*response.Body, status); err != nil {
return nil, response, err
}
return status, response, nil
}
func (service *billService) billPayParamsToPayload(params BillPayParams) map[string]string {
payload := map[string]string{
"biller": params.Biller.string(),
"billid": params.BillID,
"mode": params.Mode.String(),
"agentid": service.client.agentID,
"agentplatform": service.client.agentPlatform,
"agentpwd": service.client.agentPassword,
"hash": service.client.hash(params.Biller.string(), params.BillID, PointerToString(params.Amount), service.client.apiKey),
}
if params.Amount != nil {
payload["amount"] = PointerToString(params.Amount)
}
if params.Provider != nil {
payload["provider"] = PointerToString(params.Provider)
}
if params.Account != nil {
payload["account"] = PointerToString(params.Account)
}
if params.Mobile != nil {
payload["mobile"] = PointerToString(params.Mobile)
}
if params.Code != nil {
payload["code"] = PointerToString(params.Code)
}
if params.SMS != nil {
payload["sms"] = params.SMS.string()
}
if params.ProcessingNumber != nil {
payload["processingnumber"] = PointerToString(params.ProcessingNumber)
}
return payload
}