|
1
|
|
|
package mobilenig |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"errors" |
|
7
|
|
|
"strconv" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
// BillsService is the API client for the `/bills/` endpoint |
|
11
|
|
|
type BillsService service |
|
12
|
|
|
|
|
13
|
|
|
const ( |
|
14
|
|
|
billsServiceDStv = "DSTV" |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
// CheckDStvUser validates a DStv smartcard number |
|
18
|
|
|
// POST /bills/user_check |
|
19
|
|
|
// API Doc: https://mobilenig.com/API/docs/dstv |
|
20
|
|
|
func (service *BillsService) CheckDStvUser(ctx context.Context, smartcardNumber string) (*DStvUser, *Response, error) { |
|
21
|
|
|
payload := map[string]string{ |
|
22
|
|
|
"service": billsServiceDStv, |
|
23
|
|
|
"number": smartcardNumber, |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
request, err := service.client.newRequest(ctx, "/bills/user_check", payload) |
|
27
|
|
|
if err != nil { |
|
28
|
|
|
return nil, nil, err |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
resp, err := service.client.do(request) |
|
32
|
|
|
if err != nil { |
|
33
|
|
|
return nil, resp, err |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
var dstvUser DStvUser |
|
37
|
|
|
if err = json.Unmarshal(*resp.Body, &dstvUser); err != nil { |
|
38
|
|
|
return nil, resp, err |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return &dstvUser, resp, nil |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// GetDStvPackage returns the client's current DStv package. |
|
45
|
|
|
// POST /bills/get_package |
|
46
|
|
|
// API Doc: https://mobilenig.com/API/docs/dstv |
|
47
|
|
|
func (service *BillsService) GetDStvPackage(ctx context.Context, customerNumber int64) (*string, *Response, error) { |
|
48
|
|
|
payload := map[string]string{ |
|
49
|
|
|
"service": billsServiceDStv, |
|
50
|
|
|
"customerNumber": strconv.FormatInt(customerNumber, 10), |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
request, err := service.client.newRequest(ctx, "/bills/get_package", payload) |
|
54
|
|
|
if err != nil { |
|
55
|
|
|
return nil, nil, err |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
resp, err := service.client.do(request) |
|
59
|
|
|
if err != nil { |
|
60
|
|
|
return nil, resp, err |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
details := map[string]*string{} |
|
64
|
|
|
if err = json.Unmarshal(*resp.Body, &details); err != nil { |
|
65
|
|
|
return nil, resp, err |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return details["packageName"], resp, nil |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// PayDStv pays a DStv subscription |
|
72
|
|
|
// POST /bills/dstv |
|
73
|
|
|
// API Doc: https://mobilenig.com/API/docs/dstv |
|
74
|
|
|
func (service *BillsService) PayDStv(ctx context.Context, options *PayDstvOptions) (*DStvTransaction, *Response, error) { |
|
75
|
|
|
if options == nil { |
|
76
|
|
|
return nil, nil, errors.New("options cannot be nil") |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
payload := map[string]string{ |
|
80
|
|
|
"product_code": string(options.ProductCode), |
|
81
|
|
|
"customer_name": options.CustomerName, |
|
82
|
|
|
"customer_number": options.CustomerNumber, |
|
83
|
|
|
"price": options.Price, |
|
84
|
|
|
"smartno": options.SmartcardNumber, |
|
85
|
|
|
"trans_id": options.TransactionID, |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
uri := "/bills/dstv" |
|
89
|
|
|
if service.client.environment == TestEnvironment { |
|
90
|
|
|
uri += "_test" |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
request, err := service.client.newRequest(ctx, uri, payload) |
|
94
|
|
|
if err != nil { |
|
95
|
|
|
return nil, nil, err |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
resp, err := service.client.do(request) |
|
99
|
|
|
if err != nil { |
|
100
|
|
|
return nil, resp, err |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
var transaction DStvTransaction |
|
104
|
|
|
if err = json.Unmarshal(*resp.Body, &transaction); err != nil { |
|
105
|
|
|
return nil, resp, err |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return &transaction, resp, nil |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// QueryDStv fetches a DStv transaction using the transaction ID |
|
112
|
|
|
// POST /bills/dstv |
|
113
|
|
|
// API Doc: https://mobilenig.com/API/docs/dstv |
|
114
|
|
|
func (service *BillsService) QueryDStv(ctx context.Context, transactionID string) (*DStvTransaction, *Response, error) { |
|
115
|
|
|
payload := map[string]string{ |
|
116
|
|
|
"trans_id": transactionID, |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
request, err := service.client.newRequest(ctx, "/bills/query", payload) |
|
120
|
|
|
if err != nil { |
|
121
|
|
|
return nil, nil, err |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
resp, err := service.client.do(request) |
|
125
|
|
|
if err != nil { |
|
126
|
|
|
return nil, resp, err |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
var transaction DStvTransaction |
|
130
|
|
|
if err = json.Unmarshal(*resp.Body, &transaction); err != nil { |
|
131
|
|
|
return nil, resp, err |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return &transaction, resp, nil |
|
135
|
|
|
} |
|
136
|
|
|
|