Passed
Push — main ( 1d4b72...0c6738 )
by Acho
01:55
created

afrikpay.*billService.Amount   A

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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