Passed
Push — main ( 53658a...2d22a4 )
by Acho
01:49
created

topup_service.go   A

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 55
dl 0
loc 105
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A smobilpay.*topupService.Collect 0 17 4
A smobilpay.*topupService.GetPackages 0 17 4
A smobilpay.*topupService.Quote 0 20 4
A smobilpay.*topupService.Verify 0 21 5
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
	var packages []*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, "/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) (*Transaction, *Response, error) {
64
	request, err := service.client.newRequest(ctx, options, http.MethodPost, "/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
	transaction := new(Transaction)
75
	if err = json.Unmarshal(*response.Body, transaction); err != nil {
76
		return nil, response, err
77
	}
78
79
	return transaction, response, nil
80
}
81
82
// Verify gets the current payment collection status
83
//
84
// https://apidocs.smobilpay.com/s3papi/API-Reference.2066448558.html
85
func (service *topupService) Verify(ctx context.Context, paymentTransactionNumber string, options ...RequestOption) (*Transaction, *Response, error) {
86
	request, err := service.client.newRequest(ctx, options, http.MethodGet, fmt.Sprintf("/verifytx?ptn=%s", paymentTransactionNumber), nil)
87
	if err != nil {
88
		return nil, nil, err
89
	}
90
91
	response, err := service.client.do(request)
92
	if err != nil {
93
		return nil, response, err
94
	}
95
96
	var transactions []Transaction
97
	if err = json.Unmarshal(*response.Body, &transactions); err != nil {
98
		return nil, response, err
99
	}
100
101
	if len(transactions) == 0 {
102
		return nil, response, fmt.Errorf("cannot verify transaction with payment transaction number [%s]", paymentTransactionNumber)
103
	}
104
105
	return &transactions[0], response, nil
106
}
107