Passed
Push — main ( 79b9ab...e52106 )
by Acho
01:45
created

smobilpay.*topupService.GetPackages   A

Complexity

Conditions 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nop 3
dl 0
loc 17
rs 9.85
c 0
b 0
f 0
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