forked from openmeterio/openmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
126 lines (109 loc) · 3.21 KB
/
report.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
package main
import (
"context"
"fmt"
"time"
openmeter "github.com/openmeterio/openmeter/api/client/go"
stripe "github.com/stripe/stripe-go/v74"
subscription "github.com/stripe/stripe-go/v74/subscription"
usagerecord "github.com/stripe/stripe-go/v74/usagerecord"
)
type Report struct {
ctx context.Context
openmeter *openmeter.Client
from time.Time
to time.Time
}
func NewReport(ctx context.Context, om *openmeter.Client, duration time.Duration) Report {
// We round down period to closest windows as OpenMeter aggregates usage in windows.
// Usage occuring between rounded down date and now will be attributed to the next billing period.
to := time.Now().Truncate(duration)
from := to.Add(time.Duration(-1) * duration)
report := Report{
ctx: context.Background(),
openmeter: om,
from: from,
to: to,
}
return report
}
func (r *Report) Run() error {
status := "active"
i := subscription.List(&stripe.SubscriptionListParams{
Status: &status,
})
if i.Err() != nil {
return i.Err()
}
for i.Next() {
s := i.Subscription()
// Skip subscriptions that started after `to`.
if s.CurrentPeriodStart > r.to.Unix() {
continue
}
err := r.reportUsage(s)
if err != nil {
return err
}
}
return nil
}
func (r *Report) reportUsage(subscription *stripe.Subscription) error {
for _, item := range subscription.Items.Data {
// Skip subscription item if it's not metered
if item.Price.Recurring.UsageType != "metered" {
return nil
}
meterId := item.Metadata["om_meter_id"]
// Skip subscription item if doesn't have corresponding OpenMeter meter
if meterId == "" {
return nil
}
// Query usage from OpenMeter for billing period
resp, err := r.openmeter.GetValuesByMeterId(r.ctx, meterId, &openmeter.GetValuesByMeterIdParams{
// If you don't report usage events via Stripe Customer ID you need to map Stripe IDs to your internal ID
Subject: &subscription.Customer.ID,
From: &r.from,
To: &r.to,
})
if err != nil {
return err
}
payload, err := openmeter.ParseGetValuesByMeterIdResponse(resp)
if err != nil {
return err
}
// TODO (pmarton): switch to OpenMeter aggregate API
var total float64 = 0
for _, value := range payload.JSON200.Data {
total += value.Value
}
// Debug log
fmt.Printf(
"stripe_customer: %s, stripe_price: %s, meter: %s, total_usage: %f, from: %s, to: %s\n",
subscription.Customer.ID,
item.Price.ID,
meterId,
total,
r.from.String(),
r.to.String(),
)
// Report usage to Stripe
// We use action=set so even if this webhook get called multiple time
// we still end up with only one usage record for the same period.
// We report on `CurrentPeriodStart` because it's going to be the
// same between Webhook calls and we add one as Stripe doesn't allow
// to report usage on the exact start and end time of the subscription.
recordParams := &stripe.UsageRecordParams{
Action: stripe.String("set"),
Quantity: stripe.Int64(int64(total)),
SubscriptionItem: stripe.String(item.ID),
Timestamp: stripe.Int64(subscription.CurrentPeriodStart + 1),
}
_, err = usagerecord.New(recordParams)
if err != nil {
return err
}
}
return nil
}