-
Notifications
You must be signed in to change notification settings - Fork 1
/
r_claim_paddle.go
243 lines (206 loc) · 6.21 KB
/
r_claim_paddle.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/shopspring/decimal"
"github.com/livereload/api.livereload.com/licensecode"
"github.com/livereload/api.livereload.com/model"
)
const messageTemplate = `Subject: License via STORE - PRICE CURRENCY
NAME <EMAIL>
LICENSE_CODE
Store: STORE
Price: PRICE
Earnings: EARNINGS
Gross: GROSS
Unclaimed license codes: UNCLAIMED
Order ID: ORDER_ID
Transaction: TXN
Country: COUNTRY
Currency: CURRENCY
Quantity: QUANTITY
Tax: TAX
Processor Fee: PROCESSOR_FEE
Coupon: COUPON_TEXT
Coupon Savings: COUPON_SAVINGS
---
RAW
`
type paddleClaimRequest struct {
Txn string `json:"txn"`
Qty string `json:"quantity"`
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
Passthrough string `json:"passthrough"`
ProductID string `json:"p_product_id"`
OrderID string `json:"p_order_id"`
Country string `json:"p_country"`
Coupon string `json:"p_coupon"`
CouponSavings string `json:"p_coupon_savings"`
Currency string `json:"p_currency"`
Earnings string `json:"p_earnings"`
PaddleFee string `json:"p_paddle_fee"`
Price string `json:"p_price"`
Quantity string `json:"p_quantity"`
SaleGross string `json:"p_sale_gross"`
TaxAmount string `json:"p_tax_amount"`
UsedPriceOverride string `json:"p_used_price_override"`
Signature string `json:"p_signature"`
}
func claimLicenseForPaddle(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
sendErrorMessage(w, http.StatusMethodNotAllowed, "")
return
}
ctype, _ := parseRequestContentType(r)
if ctype != "application/x-www-form-urlencoded" {
sendErrorFmt(w, http.StatusBadRequest, "application/x-www-form-urlencoded Content-Type required, got %v", ctype)
return
}
err := r.ParseForm()
if err != nil {
sendErrorMessage(w, http.StatusBadRequest, "Failed to parse the form payload")
return
}
err = verifyToken(r.PostForm.Get("token"), paddleToken)
if err != nil {
sendError(w, err)
return
}
raw := map[string]string{}
var pairs []string
for k, vv := range r.PostForm {
if k == "token" {
continue
}
for _, v := range vv {
raw[k] = v
pairs = append(pairs, fmt.Sprintf("%v = %v", k, v))
}
}
rawJSON, err := json.Marshal(raw)
if err != nil {
sendErrorMessage(w, http.StatusInternalServerError, "Failed to marshal JSON")
return
}
var rq paddleClaimRequest
err = json.Unmarshal(rawJSON, &rq)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "Failed to decode POST payload as JSON: %v", err)
return
}
if rq.ProductID != "489469" {
sendErrorMessage(w, http.StatusBadRequest, "Invalid p_product_id")
return
}
earningsMap := map[string]string{}
if rq.Earnings != "" {
err = json.Unmarshal([]byte(rq.Earnings), &earningsMap)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "Failed to decode payload.p_earnings as JSON: %v", err)
return
}
}
earningsString := earningsMap["128"]
claim := &model.Claim{
Store: "Paddle",
Qty: 1,
Txn: rq.Txn,
FullName: rq.Name,
Email: rq.Email,
Message: rq.Message,
Raw: string(rawJSON),
OrderID: rq.OrderID,
Country: rq.Country,
Currency: rq.Currency,
Coupon: rq.Coupon,
}
claim.Price, err = decimal.NewFromString(rq.Price)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "p_price is not a decimal number: %v", rq.Price)
return
}
claim.SaleGross, err = decimal.NewFromString(rq.SaleGross)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "p_sale_gross is not a decimal number: %v", rq.SaleGross)
return
}
claim.SaleTax, err = decimal.NewFromString(rq.TaxAmount)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "p_tax_amount is not a decimal number: %v", rq.TaxAmount)
return
}
claim.ProcessorFee, err = decimal.NewFromString(rq.PaddleFee)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "p_paddle_fee is not a decimal number: %v", rq.PaddleFee)
return
}
claim.Earnings, err = decimal.NewFromString(earningsString)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, `p_earnings["128"] is not a decimal number: %v`, earningsString)
return
}
claim.CouponSavings, err = decimal.NewFromString(rq.CouponSavings)
if err != nil {
sendErrorFmt(w, http.StatusBadRequest, "p_coupon_savings is not a decimal number: %v", rq.CouponSavings)
return
}
product := "LR"
version := "2"
typ := licensecode.TypeIndividual
var code string
if false {
code = "LR2A-X92VM-MGI6H-KT5XM-KRD52-FIDXF-CQG3F-PUPE3"
} else {
code, err = model.ClaimLicense(db, product, version, typ, claim)
if err != nil {
sendError(w, err)
return
}
}
unclaimed, err := model.CountUnclaimedLicenses(db, product, version, typ)
if err != nil {
unclaimed = -1
log.Printf("ERROR: Failed to obtain the number of unclaimed licenses: %v", err)
}
params := map[string]string{
"STORE": claim.Store,
"PRICE": claim.Price.StringFixed(2),
"NAME": claim.FullName,
"EMAIL": claim.Email,
"LICENSE_CODE": code,
"ORDER_ID": claim.OrderID,
"TXN": claim.Txn,
"EARNINGS": claim.Earnings.StringFixed(4),
"GROSS": claim.SaleGross.StringFixed(4),
"TAX": claim.SaleTax.StringFixed(4),
"PROCESSOR_FEE": claim.ProcessorFee.StringFixed(4),
"COUNTRY": claim.Country,
"CURRENCY": claim.Currency,
"QUANTITY": strconv.Itoa(claim.Qty),
"COUPON_TEXT": claim.Coupon,
"COUPON_SAVINGS": claim.CouponSavings.StringFixed(4),
"UNCLAIMED": strconv.Itoa(unclaimed),
"RAW": strings.Join(pairs, "\n"),
}
subject, text := applyEmailTemplate(messageTemplate, params)
if claim.Coupon != "" {
subject = fmt.Sprintf("%s (%s)", subject, claim.Coupon)
}
if false {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Subject: %s\n\n%s", subject, text)
return
}
err = sendEmail("LiveReload Bot <[email protected]>", "Andrey Tarantsov <[email protected]>", fmt.Sprintf("%s <%s>", claim.FullName, claim.Email), subject, text, "license-admin-nf", true)
if err != nil {
log.Printf("ERROR: Failed to send email: %v", err)
}
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s\n", code)
}