forked from evalphobia/go-iap
/
validator.go
119 lines (96 loc) · 3.56 KB
/
validator.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
package playstore
import (
"net/http"
"time"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
androidpublisher "google.golang.org/api/androidpublisher/v2"
)
const (
scope = "https://www.googleapis.com/auth/androidpublisher"
defaultTimeout = time.Second * 5
)
var timeout = defaultTimeout
// SetTimeout sets dial timeout duration
func SetTimeout(t time.Duration) {
timeout = t
}
// The IABClient type is an interface to verify purchase token
type IABClient interface {
VerifySubscription(string, string, string) (*androidpublisher.SubscriptionPurchase, error)
VerifyProduct(string, string, string) (*androidpublisher.ProductPurchase, error)
}
// The Client type implements VerifySubscription method
type Client struct {
httpClient *http.Client
}
// New returns http client which includes the credentials to access androidpublisher API.
// You should create a service account for your project at
// https://console.developers.google.com and download a JSON key file to set this argument.
func New(jsonKey []byte) (Client, error) {
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, &http.Client{
Timeout: timeout,
})
conf, err := google.JWTConfigFromJSON(jsonKey, scope)
return Client{conf.Client(ctx)}, err
}
func NewWithParams(key, email string) Client {
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, &http.Client{
Timeout: timeout,
})
conf := &jwt.Config{
Email: email,
PrivateKey: []byte(key),
Scopes: []string{scope},
TokenURL: google.JWTTokenURL,
}
return Client{conf.Client(ctx)}
}
// Verify retrieves product and subscription status from GooglePlay API
func (c *Client) Verify(packageName, productID, token string) (*IABResponse, error) {
resp, err := c.VerifyProduct(packageName, productID, token)
if err == nil {
return resp, nil
}
resp.SubscriptionPurchase, err = c.verifySubscription(packageName, productID, token)
return resp, err
}
// VerifySubscription retrieves product status from GooglePlay API
func (c *Client) VerifySubscription(packageName, productID, token string) (*IABResponse, error) {
result, err := c.verifySubscription(packageName, productID, token)
return &IABResponse{SubscriptionPurchase: result}, err
}
func (c *Client) verifySubscription(packageName, subscriptionID, token string) (*androidpublisher.SubscriptionPurchase, error) {
service, err := androidpublisher.New(c.httpClient)
if err != nil {
return nil, err
}
ps := androidpublisher.NewPurchasesSubscriptionsService(service)
result, err := ps.Get(packageName, subscriptionID, token).Do()
return result, err
}
// VerifyProduct retrieves product status from GooglePlay API
func (c *Client) VerifyProduct(packageName, productID, token string) (*IABResponse, error) {
result, err := c.verifyProduct(packageName, productID, token)
return &IABResponse{ProductPurchase: result}, err
}
func (c *Client) verifyProduct(packageName, productID, token string) (*androidpublisher.ProductPurchase, error) {
service, err := androidpublisher.New(c.httpClient)
if err != nil {
return nil, err
}
ps := androidpublisher.NewPurchasesProductsService(service)
result, err := ps.Get(packageName, productID, token).Do()
return result, err
}
// CancelSubscription cancels recurring payment of given subscription
func (c *Client) CancelSubscription(packageName, subscriptionID, token string) error {
service, err := androidpublisher.New(c.httpClient)
if err != nil {
return err
}
ps := androidpublisher.NewPurchasesSubscriptionsService(service)
return ps.Cancel(packageName, subscriptionID, token).Do()
}