-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.go
156 lines (115 loc) · 3.45 KB
/
payment.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
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type PayPal struct {
Token string `json:"access_token"`
Last_Update string `json:"update_time"`
Status string `json:"status"`
}
func CheckPayment(orderID string, c *gin.Context) (int64, error) {
_, err := DB["payments"].InsertOne(ctx, bson.D{{Key: "_id", Value: orderID}})
if err != nil {
return 0, errors.New("0")
}
token, err := getPayPalToken()
if err != nil {
return 0, errors.New("0")
}
isOrderCompleted := checkPayPalOrder(orderID, token)
if !isOrderCompleted {
return 0, errors.New("0")
}
premium := updatePremium(c)
return premium, nil
}
func updatePremium(c *gin.Context) int64 {
id, _ := c.Cookie("id")
idInt, _ := strconv.Atoi(id)
var user bson.M
opts := options.FindOne().SetProjection(bson.M{"_id": 0, "premium": 1})
DB["users"].FindOne(ctx, bson.M{"_id": idInt}, opts).Decode(&user)
premium := user["premium"].(int64)
now := time.Now().Unix()
if premium > 0 && premium > now {
expires := int64(premium + int64(1*60*60*24*30))
DB["users"].UpdateOne(ctx, bson.M{"_id": idInt}, bson.D{
{Key: "$set", Value: bson.D{{Key: "premium", Value: expires}}},
})
c.SetSameSite(http.SameSiteNoneMode)
c.SetCookie("premium", "premium", int(premium-now+(1*60*60*24*30)), "/", "."+config.SELF_DOMAIN_NAME, true, true)
return expires
} else {
expires := int64(now + int64(1*60*60*24*30))
DB["users"].UpdateOne(ctx, bson.M{"_id": idInt}, bson.D{
{Key: "$set", Value: bson.D{{Key: "premium", Value: expires}}},
})
c.SetSameSite(http.SameSiteNoneMode)
c.SetCookie("premium", "premium", 1*60*60*24*30, "/", "."+config.SELF_DOMAIN_NAME, true, true)
return expires
}
}
func getPayPalToken() (string, error) {
client := &http.Client{}
data := strings.NewReader(`grant_type=client_credentials`)
req, err := http.NewRequest("POST", "https://api-m.paypal.com/v1/oauth2/token", data)
if err != nil {
return "", errors.New("0")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth("AQWOBzexECxYSIUxzjRH1YplNI_bxuc6Vzxa2uRWltHZzgOAjR23hTbEPXVm82UkF8Ebvj-QSQHo3pbH", "EGqQToUYR77NM_fZQaNPKTFJ8YDtFCsgraxE9GlOFlf3YI653tUsvSJ27yZGPlGXFgEqnAfyjiM34QlM")
resp, err := client.Do(req)
if err != nil {
return "", errors.New("0")
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.New("0")
}
paypal := PayPal{}
json.Unmarshal([]byte(bodyText), &paypal)
return paypal.Token, nil
}
func checkPayPalOrder(orderID, token string) bool {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.paypal.com/v2/checkout/orders/"+orderID, nil)
if err != nil {
return false
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
return false
}
paypal := PayPal{}
json.Unmarshal([]byte(bodyText), &paypal)
if paypal.Status != "COMPLETED" {
return false
}
if !checkPayPalOrderTime(paypal.Last_Update) {
return false
}
return true
}
func checkPayPalOrderTime(last_update string) bool {
date, _ := time.Parse(time.RFC3339, last_update)
update := date.Unix()
minus := time.Now().Unix() - update
return minus <= 3600
}