-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct_billing.go
98 lines (90 loc) · 3.49 KB
/
direct_billing.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
package simpay
import (
"crypto"
"encoding/json"
"fmt"
"strings"
)
type DirectBilling struct {
restClient
}
func NewDirectBilling(apiKey, simPassword string) DirectBilling {
return DirectBilling{
restClient: newRestClient(apiKey, simPassword),
}
}
func (d DirectBilling) GetServiceList(page, limit uint) (DirectBillingServiceListResponse, error) {
endpoint := fmt.Sprintf("/directbilling?page=%v&limit=%v", page, limit)
response, err := d.restClient.sendGetRequest(endpoint)
if err != nil {
return DirectBillingServiceListResponse{}, err
}
var serviceList DirectBillingServiceListResponse
return serviceList, json.Unmarshal(response, &serviceList)
}
func (d DirectBilling) GetServiceDetails(serviceId uint) (DirectBillingServiceDetailsResponse, error) {
endpoint := fmt.Sprintf("/directbilling/%v", serviceId)
response, err := d.restClient.sendGetRequest(endpoint)
if err != nil {
return DirectBillingServiceDetailsResponse{}, err
}
var serviceDetails DirectBillingServiceDetailsResponse
return serviceDetails, json.Unmarshal(response, &serviceDetails)
}
func (d DirectBilling) CalculateCommission(serviceId uint, amount float32) (CalculateCommissionResponse, error) {
endpoint := fmt.Sprintf("/directbilling/%v/calculate?amount=%f", serviceId, amount)
response, err := d.restClient.sendGetRequest(endpoint)
if err != nil {
return CalculateCommissionResponse{}, err
}
var commissionResponse CalculateCommissionResponse
return commissionResponse, json.Unmarshal(response, &commissionResponse)
}
func (d DirectBilling) GetTransactionList(serviceId, page, limit uint) (DirectBillingTransactionListResponse, error) {
endpoint := fmt.Sprintf("/directbilling/%v/transactions?page=%v&limit=%v", serviceId, page, limit)
response, err := d.restClient.sendGetRequest(endpoint)
if err != nil {
return DirectBillingTransactionListResponse{}, err
}
var transactionList DirectBillingTransactionListResponse
return transactionList, json.Unmarshal(response, &transactionList)
}
func (d DirectBilling) GetTransactionDetails(serviceId uint, transactionId string) (DirectBillingTransactionDetailsResponse, error) {
endpoint := fmt.Sprintf("/directbilling/%v/transactions/%v", serviceId, transactionId)
response, err := d.restClient.sendGetRequest(endpoint)
if err != nil {
return DirectBillingTransactionDetailsResponse{}, err
}
var transactionDetails DirectBillingTransactionDetailsResponse
return transactionDetails, json.Unmarshal(response, &transactionDetails)
}
func (d DirectBilling) GenerateTransaction(serviceId uint, request GenerateTransactionRequest) (DirectBillingGenerateTransactionResponse, error) {
endpoint := fmt.Sprintf("/directbilling/%d/transactions", serviceId)
response, err := d.restClient.sendPostRequest(endpoint, request)
if err != nil {
return DirectBillingGenerateTransactionResponse{}, err
}
var transactionResponse DirectBillingGenerateTransactionResponse
return transactionResponse, json.Unmarshal(response, &transactionResponse)
}
func CheckSignature(key, transactionJson string) bool {
var n DirectBillingTransactionNotification
err := json.Unmarshal([]byte(transactionJson), &n)
if err != nil {
return false
}
fields := []string{
fmt.Sprintf("%v", n.Id),
n.Status,
fmt.Sprintf("%f", n.Values.Net),
fmt.Sprintf("%f", n.Values.Gross),
fmt.Sprintf("%f", n.Values.Partner),
n.Returns.Complete,
n.Returns.Failure,
n.NumberFrom,
fmt.Sprintf("%v", n.Provider),
n.Signature,
key,
}
return string(crypto.SHA256.New().Sum([]byte(strings.Join(fields, "|")))) == n.Signature
}