-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.go
169 lines (138 loc) · 3.97 KB
/
checkout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Package hc is an api wrapper for hubtel's merchant account.Checkout API
// allows merchants to accept online payment for goods and services using
// mobile money and credit/debit cards.
package hc
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const (
// BaseURL merchant checkout api base url
BaseURL = "https://api.hubtel.com/v1/merchantaccount/onlinecheckout/"
// CreateInvoiceURL endpoit for creating checkout invoice
CreateInvoiceURL = BaseURL + "invoice/create"
// InvoiceStatusURL endpoint for retrieving status of checkout invoice
InvoiceStatusURL = BaseURL + "invoice/status/"
)
// Checkout represents hubtel online
// checkout page
type Checkout struct {
authKey string
Invoice *Invoice
Store *Store
Actions *Actions
CustomData *CustomData
}
type checkoutRequest struct {
Invoice *Invoice `json:"invoice,omitempty"`
Store *Store `json:"store,omitempty"`
Actions *Actions `json:"actions,omitempty"`
CustomData *CustomData `json:"custom_data,omitempty"`
}
type checkoutResponse struct {
ResponseCode string `json:"response_code,omitempty"`
ResponseText string `json:"response_text,omitempty"`
Description string `json:"description,omitempty"`
Token string `json:"token,omitempty"`
Invoice *Invoice `json:"invoice,omitempty"`
Actions *Actions `json:"actions,omitempty"`
CustomData *CustomData `json:"custom_data,omitempty"`
Status string `json:"status,omitempty"`
}
// Setup checkout with hubtel checkout
// api account credentials
func Setup(clientID, clientSecret string) (*Checkout, error) {
var ak string
var err error
if ak, err = genAuthKey(clientID, clientSecret); err != nil {
return nil, err
}
c := &Checkout{
authKey: ak,
Store: new(Store),
Actions: new(Actions),
Invoice: &Invoice{
Items: make(map[string]*Item),
Taxes: make(map[string]*Tax),
},
CustomData: &CustomData{
CustomData: make(map[string]interface{}),
},
}
return c, nil
}
// Create online checkout invoice
func (c Checkout) Create() (*checkoutResponse, error) {
body, err := c.genRequestBody()
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodPost, CreateInvoiceURL, bytes.NewBuffer(body))
req.Header.Add("Authorization", c.authKey)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Cache-Control", "no-cache")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
cr := new(checkoutResponse)
err = json.Unmarshal(body, cr)
if err != nil {
return nil, err
}
return cr, nil
}
// Status retrieve online checkout invoice status
func (c Checkout) Status(token string) (*checkoutResponse, error) {
url := InvoiceStatusURL + token
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Add("Authorization", c.authKey)
req.Header.Add("Cache-Control", "no-cache")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
cr := new(checkoutResponse)
err = json.Unmarshal(body, cr)
if err != nil {
return nil, err
}
return cr, nil
}
func (c Checkout) genRequestBody() ([]byte, error) {
body := &checkoutRequest{
Invoice: c.Invoice,
Store: c.Store,
Actions: c.Actions,
CustomData: c.CustomData,
}
return json.Marshal(body)
}
func genAuthKey(clientID, clientSecret string) (string, error) {
switch {
case clientID == "" && clientSecret == "":
return "", errors.New("configure client_id and client_secret")
case clientID == "":
return "", errors.New("configure client_id")
case clientSecret == "":
return "", errors.New("configure client_secret")
}
m := strings.Join([]string{clientID, clientSecret}, ":")
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(m))), nil
}