forked from openmeterio/openmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
70 lines (62 loc) · 2.04 KB
/
setup.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
package main
import (
"fmt"
stripe "github.com/stripe/stripe-go/v74"
stripeCustomer "github.com/stripe/stripe-go/v74/customer"
stripePrice "github.com/stripe/stripe-go/v74/price"
stripeProduct "github.com/stripe/stripe-go/v74/product"
stripeSubscription "github.com/stripe/stripe-go/v74/subscription"
)
// Meter in your config, we use it to map our price to this meter
var meterId = "m1"
func SetupStripe() error {
// Create a Stripe Product
product, err := stripeProduct.New(&stripe.ProductParams{
Name: stripe.String("Execution Duration"),
})
if err != nil {
return err
}
fmt.Printf("Stripe product created: https://dashboard.stripe.com/test/products/%s\n", product.ID)
// Create a metered Stripe Price
price, err := stripePrice.New(&stripe.PriceParams{
Product: &product.ID,
// The meter ID this price belongs to
Currency: stripe.String(string(stripe.CurrencyUSD)),
Recurring: &stripe.PriceRecurringParams{
Interval: stripe.String(string(stripe.PlanIntervalMonth)),
UsageType: stripe.String(string(stripe.PlanUsageTypeMetered)),
},
BillingScheme: stripe.String(string(stripe.PlanBillingSchemePerUnit)),
UnitAmount: stripe.Int64(10), // cents
})
if err != nil {
return err
}
fmt.Printf("Stripe price created: https://dashboard.stripe.com/test/prices/%s\n", price.ID)
// Create a Stripe customer
customer, err := stripeCustomer.New(&stripe.CustomerParams{
Name: stripe.String("My Awesome Customer"),
})
if err != nil {
return err
}
fmt.Printf("Stripe customer created: https://dashboard.stripe.com/test/customers/%s\n", customer.ID)
// Start a new Stripe subscription for customer with the price created above
subscription, err := stripeSubscription.New(&stripe.SubscriptionParams{
Customer: &customer.ID,
Items: []*stripe.SubscriptionItemsParams{
{
Price: &price.ID,
Metadata: map[string]string{
"om_meter_id": meterId,
},
},
},
})
if err != nil {
return err
}
fmt.Printf("Stripe subscription created: https://dashboard.stripe.com/test/subscriptions/%s\n", subscription.ID)
return nil
}