Passed
Push — main ( 53329a...1d4b72 )
by Acho
02:01
created

afrikpay.*billService.Pay   A

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
dl 0
loc 22
rs 9.65
c 0
b 0
f 0
nop 2
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
func (service *billService) billPayParamsToPayload(params BillPayParams) map[string]string {
67
	payload := map[string]string{
68
		"biller":        params.Biller.string(),
69
		"billid":        params.BillID,
70
		"mode":          params.Mode.String(),
71
		"agentid":       service.client.agentID,
72
		"agentplatform": service.client.agentPlatform,
73
		"agentpwd":      service.client.agentPassword,
74
		"hash":          service.client.hash(params.Biller.string(), params.BillID, PointerToString(params.Amount), service.client.apiKey),
75
	}
76
77
	if params.Amount != nil {
78
		payload["amount"] = PointerToString(params.Amount)
79
	}
80
	if params.Provider != nil {
81
		payload["provider"] = PointerToString(params.Provider)
82
	}
83
	if params.Account != nil {
84
		payload["account"] = PointerToString(params.Account)
85
	}
86
	if params.Mobile != nil {
87
		payload["mobile"] = PointerToString(params.Mobile)
88
	}
89
	if params.Code != nil {
90
		payload["code"] = PointerToString(params.Code)
91
	}
92
	if params.SMS != nil {
93
		payload["sms"] = params.SMS.string()
94
	}
95
	if params.ProcessingNumber != nil {
96
		payload["processingnumber"] = PointerToString(params.ProcessingNumber)
97
	}
98
99
	return payload
100
}
101