-
Notifications
You must be signed in to change notification settings - Fork 0
/
paytabs.go
106 lines (89 loc) · 2.42 KB
/
paytabs.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
package paytabs
import (
"bytes"
"encoding/json"
//"fmt"
"io/ioutil"
"net/http"
"net/url"
)
type payPageResponse struct {
Result string `json:"result,ommitempty"`
ResponseCode string `json:"response_code,ommitempty"`
PaymentURL string `json:"payment_url,ommitempty"`
}
type generalResponse struct {
Result string `json:"result,ommitempty"`
ResponseCode string `json:"response_code,ommitempty"`
}
type verifyResponse struct {
Result string `json:"result,ommitempty"`
ResponseCode string `json:"response_code,ommitempty"`
Amount string `json:"amount,ommitempty"`
PTInvoiceID string `json:"pt_invoice_id,ommitempty"`
Currency string `json:"currency,ommitempty"`
ReferenceNo string `json:"reference_no,ommitempty"`
TransactionID string `json:"transaction_id,ommitempty"`
}
/**
Create PayPage API
**/
func CretaePayPage(data map[string]string) (payPageResponse, error) {
location := "https://www.paytabs.com/apiv2/create_pay_page"
resp, err := sendRequest(location, data)
if err != nil {
return payPageResponse{}, err //Error Happened
}
reader := bytes.NewReader(resp)
_paypageResponse := payPageResponse{}
json.NewDecoder(reader).Decode(&_paypageResponse)
return _paypageResponse, nil
}
/**
Validate Secret key
**/
func ValidateSecretKey(data map[string]string) (generalResponse, error) {
location := "https://www.paytabs.com/apiv2/validate_secret_key"
resp, err := sendRequest(location, data)
if err != nil {
return generalResponse{}, err
}
reader := bytes.NewReader(resp)
validateResponse := generalResponse{}
json.NewDecoder(reader).Decode(&validateResponse)
return validateResponse, nil
}
func VerifyPayment(data map[string]string) (verifyResponse, error) {
location := "https://www.paytabs.com/apiv2/verify_payment"
resp, err := sendRequest(location, data)
if err != nil {
return verifyResponse{}, err
}
reader := bytes.NewReader(resp)
_verifyResponse := verifyResponse{}
json.NewDecoder(reader).Decode(&_verifyResponse)
return _verifyResponse, nil
}
/**
Post data to the server
**/
func sendRequest(link string, data map[string]string) ([]byte, error) {
post_data := url.Values{}
//Iterate the map
for key, val := range data {
post_data.Add(key, val)
}
resp, err := http.PostForm(
link,
post_data,
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}