-
Notifications
You must be signed in to change notification settings - Fork 0
/
gumroad.go
58 lines (52 loc) · 1.6 KB
/
gumroad.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
package license
import (
"encoding/json"
"net/http"
"net/url"
"strings"
)
func Gumroad(productUrl, key string) (Activation, error) {
lic := Activation{
Active: false,
}
form := url.Values{
"product_permalink": {productUrl},
"license_key": {key},
}
resp, err := http.PostForm("https://api.gumroad.com/v2/licenses/verify", form)
if err != nil {
return lic, err
}
r := &gumroadResponse{}
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return lic, err
}
var variants []string = []string{}
if r.Purchase.Variants != "" {
variants = strings.Split(r.Purchase.Variants[1:len(r.Purchase.Variants)-1], ",")
}
return Activation{
Active: r.Success && !(r.Purchase.Refunded || r.Purchase.Chargebacked),
Context: map[string]interface{}{
"variants": variants,
"response": r,
},
}, nil
}
type gumroadResponse struct {
Success bool `json:"success"`
Uses int64 `json:"uses"`
Purchase struct {
id string
ProductName string `json:"product_name"`
CreatedAt string `json:"created_at"`
FullName string `json:"full_name"`
Variants string `json:"variants"`
Refunded bool `json:"refunded"`
Chargebacked bool `json:"chargebacked"`
SubscriptionCancelledAt string `json:"subscription_cancelled_at"`
SubscriptionFailedAt string `json:"subscription_failed_at"`
CustomFields []interface{} `json:"custom_fields"`
Email string `json:"email"`
} `json:"purchase"`
}