-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaystore.go
133 lines (108 loc) · 4.54 KB
/
playstore.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
package playstore
import (
"context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/androidpublisher/v3"
"google.golang.org/api/option"
"net/http"
"os"
)
type options struct {
client *http.Client
}
type Option func(opts *options)
func WithClient(client *http.Client) Option {
return func(opts *options) {
if client != nil {
opts.client = client
}
}
}
type Client struct {
products *androidpublisher.PurchasesProductsService
subscriptions *androidpublisher.PurchasesSubscriptionsService
subscriptionsV2 *androidpublisher.PurchasesSubscriptionsv2Service
voidedPurchases *androidpublisher.PurchasesVoidedpurchasesService
}
func New(jsonKey []byte, opts ...Option) (*Client, error) {
var nOpt = &options{
client: http.DefaultClient,
}
for _, opt := range opts {
if opt != nil {
opt(nOpt)
}
}
var ctx = context.WithValue(context.Background(), oauth2.HTTPClient, nOpt.client)
var conf, err = google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)
if err != nil {
return nil, err
}
var val = conf.Client(ctx).Transport.(*oauth2.Transport)
if _, err = val.Source.Token(); err != nil {
return nil, err
}
service, err := androidpublisher.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
if err != nil {
return nil, err
}
var products = androidpublisher.NewPurchasesProductsService(service)
var subscriptions = androidpublisher.NewPurchasesSubscriptionsService(service)
var subscriptionsV2 = androidpublisher.NewPurchasesSubscriptionsv2Service(service)
var voidedPurchases = androidpublisher.NewPurchasesVoidedpurchasesService(service)
return &Client{
products: products,
subscriptions: subscriptions,
subscriptionsV2: subscriptionsV2,
voidedPurchases: voidedPurchases,
}, nil
}
func NewWithJSONKeyFile(filename string, opts ...Option) (*Client, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return New(data, opts...)
}
func (this *Client) ProductsService() *androidpublisher.PurchasesProductsService {
return this.products
}
func (this *Client) SubscriptionsService() *androidpublisher.PurchasesSubscriptionsService {
return this.subscriptions
}
func (this *Client) SubscriptionsV2Service() *androidpublisher.PurchasesSubscriptionsv2Service {
return this.subscriptionsV2
}
func (this *Client) VoidedPurchasesService() *androidpublisher.PurchasesVoidedpurchasesService {
return this.voidedPurchases
}
func (this *Client) VerifyProduct(ctx context.Context, packageName, productId, token string) (*androidpublisher.ProductPurchase, error) {
return this.products.Get(packageName, productId, token).Context(ctx).Do()
}
func (this *Client) AcknowledgeProduct(ctx context.Context, packageName, productId, token, developerPayload string) error {
var param = &androidpublisher.ProductPurchasesAcknowledgeRequest{DeveloperPayload: developerPayload}
return this.products.Acknowledge(packageName, productId, token, param).Context(ctx).Do()
}
func (this *Client) ConsumeProduct(ctx context.Context, packageName, productId, token string) error {
return this.products.Consume(packageName, productId, token).Context(ctx).Do()
}
func (this *Client) VerifySubscription(ctx context.Context, packageName, subscriptionId, token string) (*androidpublisher.SubscriptionPurchase, error) {
return this.subscriptions.Get(packageName, subscriptionId, token).Context(ctx).Do()
}
func (this *Client) VerifySubscriptionV2(ctx context.Context, packageName, token string) (*androidpublisher.SubscriptionPurchaseV2, error) {
return this.subscriptionsV2.Get(packageName, token).Context(ctx).Do()
}
func (this *Client) AcknowledgeSubscription(ctx context.Context, packageName, subscriptionId, token, developerPayload string) error {
var param = &androidpublisher.SubscriptionPurchasesAcknowledgeRequest{DeveloperPayload: developerPayload}
return this.subscriptions.Acknowledge(packageName, subscriptionId, token, param).Context(ctx).Do()
}
func (this *Client) CancelSubscription(ctx context.Context, packageName string, subscriptionId string, token string) error {
return this.subscriptions.Cancel(packageName, subscriptionId, token).Context(ctx).Do()
}
func (this *Client) RefundSubscription(ctx context.Context, packageName string, subscriptionId string, token string) error {
return this.subscriptions.Refund(packageName, subscriptionId, token).Context(ctx).Do()
}
func (this *Client) RevokeSubscription(ctx context.Context, packageName string, subscriptionId string, token string) error {
return this.subscriptions.Revoke(packageName, subscriptionId, token).Context(ctx).Do()
}