-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceipt.go
130 lines (104 loc) · 3.97 KB
/
receipt.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
package iaphub
import (
"encoding/json"
"fmt"
"time"
)
// Context of the receipt post
type ReceiptContext string
// Status of the receipt
type ReceiptStatus string
// Webhook status of the receipt
type WebhookStatus string
const (
ReceiptContextRefresh ReceiptContext = "refresh"
ReceiptContextPurchase ReceiptContext = "purchase"
ReceiptContextRestore ReceiptContext = "restore"
ReceiptStatusProcessed ReceiptStatus = "processed"
ReceiptStatusProcessing ReceiptStatus = "processing"
ReceiptStatusDeferred ReceiptStatus = "deferred"
ReceiptStatusFailed ReceiptStatus = "failed"
ReceiptStatusInvalid ReceiptStatus = "invalid"
ReceiptStatusStale ReceiptStatus = "stale"
ReceiptStatusSuccess ReceiptStatus = "success"
WebhookStatusSuccess WebhookStatus = "success"
WebhookStatusFailed WebhookStatus = "failed"
)
type GetReceiptRequest struct {
ReceiptId string `json:"receiptId"`
}
func (c *Client) GetReceipt(request GetReceiptRequest) (Receipt, error) {
var receipt Receipt
if request.ReceiptId == "" {
return receipt, fmt.Errorf("required parameter \"receiptId\" is missing")
}
path := fmt.Sprintf(pathGetReceipt, c.appId, request.ReceiptId)
response, err := c.requestGet(path, map[string]string{})
if err != nil {
return receipt, err
}
err = json.Unmarshal(response, &receipt)
return receipt, err
}
type UpdateReceiptRequest struct {
UserId string
Env Env
Platform Platform
Token string
Sku string
Context ReceiptContext
ProrationMode ProrationMode
Upsert bool
}
func (c *Client) UpdateReceipt(request UpdateReceiptRequest) (ReceiptUpdate, error) {
var receiptUpdate ReceiptUpdate
if request.UserId == "" || request.Platform == "" || request.Token == "" || request.Context == "" {
return receiptUpdate, fmt.Errorf("one of the required parameters(\"userId\", \"platform\", \"token\", \"context\") is missing")
} else if request.Platform == PlatformAndroid && (request.ProrationMode == "" || request.Sku == "") {
return receiptUpdate, fmt.Errorf("one of the required parameters(\"sku\", \"prorationMode\") for Android platform are missing")
}
if request.Env == "" {
request.Env = c.env
}
path := fmt.Sprintf(pathUpdateReceipt, c.appId, request.UserId)
body, err := json.Marshal(request)
if err != nil {
return receiptUpdate, err
}
response, err := c.requestPost(path, map[string]string{}, body)
if err != nil {
return receiptUpdate, err
}
err = json.Unmarshal(response, &receiptUpdate)
return receiptUpdate, err
}
type Receipt struct {
Id string `json:"id"`
CreatedDate time.Time `json:"createdDate"`
ProcessCount int `json:"processCount"`
ProcessDate time.Time `json:"processedDate"`
RefreshDate time.Time `json:"refreshDate"`
UserId string `json:"user"`
Platform Platform `json:"platform"`
Status ReceiptStatus `json:"status"`
Token string `json:"token"`
Sku string `json:"sku"`
}
type ReceiptUpdate struct {
Status ReceiptStatus `json:"status"`
NewTransactions []Transaction `json:"newTransactions"`
OldTransactions []Transaction `json:"oldTransactions"`
}
type Transaction struct {
Id string `json:"id"`
Sku string `json:"sku"`
Purchase string `json:"purchase"`
PurchaseDate time.Time `json:"purchaseDate"`
Group string `json:"group"`
GroupName string `json:"groupName"`
ExpirationDate time.Time `json:"expirationDate"`
AutoResumeDate time.Time `json:"autoResumeDate"`
IsSubscriptionRenewable bool `json:"isSubscriptionRenewable"`
IsSubscriptionRetryPeriod bool `json:"IsSubscriptionRetryPeriod"`
SubscriptionPeriodType SubscriptionPeriodType `json:"subscriptionPeriodType"`
}