1
|
|
|
package smobilpay |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"net/http" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// topupService is the API client for the `/` endpoint |
11
|
|
|
type topupService service |
12
|
|
|
|
13
|
|
|
// GetPackages returns a list of all available topup packages. |
14
|
|
|
// |
15
|
|
|
// https://apidocs.smobilpay.com/s3papi/API-Reference.2066448558.html |
16
|
|
|
func (service *topupService) GetPackages(ctx context.Context, serviceID string, options ...RequestOption) (*[]TopupPackage, *Response, error) { |
17
|
|
|
request, err := service.client.newRequest(ctx, options, http.MethodGet, fmt.Sprintf("/topup?serviceid=%s", serviceID), nil) |
18
|
|
|
if err != nil { |
19
|
|
|
return nil, nil, err |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
response, err := service.client.do(request) |
23
|
|
|
if err != nil { |
24
|
|
|
return nil, response, err |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
packages := new([]TopupPackage) |
28
|
|
|
if err = json.Unmarshal(*response.Body, packages); err != nil { |
29
|
|
|
return nil, response, err |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return packages, response, nil |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Quote initializes the airtime topup transaction |
36
|
|
|
// |
37
|
|
|
// https://apidocs.smobilpay.com/s3papi/API-Reference.2066448558.html |
38
|
|
|
func (service *topupService) Quote(ctx context.Context, params *QuoteParams, options ...RequestOption) (*Quote, *Response, error) { |
39
|
|
|
request, err := service.client.newRequest(ctx, options, http.MethodPost, fmt.Sprintf("/quotestd"), map[string]string{ |
40
|
|
|
"payItemId": params.PayItemID, |
41
|
|
|
"amount": params.Amount, |
42
|
|
|
}) |
43
|
|
|
if err != nil { |
44
|
|
|
return nil, nil, err |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
response, err := service.client.do(request) |
48
|
|
|
if err != nil { |
49
|
|
|
return nil, response, err |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
packages := new(Quote) |
53
|
|
|
if err = json.Unmarshal(*response.Body, packages); err != nil { |
54
|
|
|
return nil, response, err |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return packages, response, nil |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Collect confirms the airtime topup transaction |
61
|
|
|
// |
62
|
|
|
// https://apidocs.smobilpay.com/s3papi/API-Reference.2066448558.html |
63
|
|
|
func (service *topupService) Collect(ctx context.Context, params *CollectParams, options ...RequestOption) (*map[string]interface{}, *Response, error) { |
64
|
|
|
request, err := service.client.newRequest(ctx, options, http.MethodPost, fmt.Sprintf("/collectstd"), params.toPayload()) |
65
|
|
|
if err != nil { |
66
|
|
|
return nil, nil, err |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
response, err := service.client.do(request) |
70
|
|
|
if err != nil { |
71
|
|
|
return nil, response, err |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
packages := new(map[string]interface{}) |
75
|
|
|
if err = json.Unmarshal(*response.Body, packages); err != nil { |
76
|
|
|
return nil, response, err |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return packages, response, nil |
80
|
|
|
} |
81
|
|
|
|