-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
95 lines (54 loc) · 1.66 KB
/
token.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
package paytr
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strconv"
"github.com/gorilla/schema"
)
//NOTE: Basket data base64 encoding
func (p *Payment) BasketConfig(cart [][]any) {
cartbytes, _ := json.Marshal(cart)
p.Basket = base64.StdEncoding.EncodeToString(cartbytes)
}
//NOTE: Token generation for payment process
func (p *Payment) GenerateToken(merchantKey, merchantSalt string) string {
hashedStr := p.MerchantID + p.UserIP + p.MerchantOid + p.Email + strconv.Itoa(p.TotalAmount) + p.Basket +
strconv.Itoa(p.NoInstallment) + strconv.Itoa(p.MaxInstallment) + p.Currency + p.TestMode
paytr_token := hashedStr + merchantSalt
hmacToken := hmac.New(sha256.New, []byte(merchantKey))
hmacToken.Write([]byte(paytr_token))
p.PaytrToken = base64.StdEncoding.EncodeToString(hmacToken.Sum(nil))
p.MerchantKey = merchantKey
p.MerchantSalt = merchantSalt
return p.PaytrToken
}
//NOTE: Get Iframe to show payment page in client side
func (p *Payment) GetIframe() (PaytrResponse, error) {
var response PaytrResponse
formData := url.Values{}
encoder := schema.NewEncoder()
err := encoder.Encode(p, formData)
if err != nil {
log.Println("Error encode form data: ", err)
return response, err
}
res, err := http.PostForm("https://www.paytr.com/odeme/api/get-token", formData)
if err != nil {
log.Println("Error requesting payment: ", err)
return response, err
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
log.Println("Error reading body: ", err)
return response, err
}
json.Unmarshal(resBody, &response)
return response, nil
}