Passed
Push — main ( 455ab2...9ca7b6 )
by Acho
02:07
created

client.*airtimeService.Status   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 client
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
)
8
9
// airtimeService is the API client for the `/` endpoint
10
type airtimeService service
11
12
// Transfer is intended for communication / Internet credit transfer operations to telephone numbers.
13
//
14
// API Docs: https://developer.afrikpay.com/documentation/airtime/v2/
15
func (service *airtimeService) Transfer(ctx context.Context, params AirtimeTransferParams) (*AirtimeResponse, *Response, error) {
16
	request, err := service.client.newRequest(ctx, http.MethodPost, "/api/airtime/v2/", map[string]string{
17
		"operator":      params.Operator,
18
		"reference":     params.PhoneNumber,
19
		"amount":        params.Amount,
20
		"mode":          params.Mode.String(),
21
		"purchaseref":   params.PurchaseReference,
22
		"agentid":       service.client.agentID,
23
		"agentplatform": service.client.agentPlatform,
24
		"agentpwd":      service.client.agentPassword,
25
		"hash":          service.client.hash(params.Operator, params.PhoneNumber, params.Amount, service.client.apiKey),
26
	})
27
	if err != nil {
28
		return nil, nil, err
29
	}
30
31
	response, err := service.client.do(request)
32
	if err != nil {
33
		return nil, response, err
34
	}
35
36
	status := new(AirtimeResponse)
37
	if err = json.Unmarshal(*response.Body, status); err != nil {
38
		return nil, response, err
39
	}
40
41
	return status, response, nil
42
}
43
44
// Status is intended for getting the status of an airtime transaction
45
//
46
// API Docs: https://developer.afrikpay.com/documentation/airtime/status/v2/
47
func (service *airtimeService) Status(ctx context.Context, transactionID string) (*AirtimeResponse, *Response, error) {
48
	request, err := service.client.newRequest(ctx, http.MethodPost, "/api/airtime/status/v2/", map[string]string{
49
		"processingnumber": transactionID,
50
		"agentid":          service.client.agentID,
51
		"agentplatform":    service.client.agentPlatform,
52
		"hash":             service.client.hash(transactionID, service.client.apiKey),
53
	})
54
	if err != nil {
55
		return nil, nil, err
56
	}
57
58
	response, err := service.client.do(request)
59
	if err != nil {
60
		return nil, response, err
61
	}
62
63
	status := new(AirtimeResponse)
64
	if err = json.Unmarshal(*response.Body, status); err != nil {
65
		return nil, response, err
66
	}
67
68
	return status, response, nil
69
}
70