-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathstripe.go
More file actions
158 lines (134 loc) · 4.48 KB
/
Copy pathstripe.go
File metadata and controls
158 lines (134 loc) · 4.48 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package payment
import (
"context"
"errors"
"time"
"github.com/rilldata/rill/admin/billing"
"github.com/rilldata/rill/admin/database"
"github.com/rilldata/rill/admin/jobs"
"github.com/rilldata/rill/runtime/pkg/httputil"
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/billingportal/session"
checkoutsession "github.com/stripe/stripe-go/v79/checkout/session"
"github.com/stripe/stripe-go/v79/customer"
"go.uber.org/zap"
)
var _ Provider = &Stripe{}
type Stripe struct {
logger *zap.Logger
webhookSecret string
}
func NewStripe(logger *zap.Logger, stripeKey, stripeWebhookSecret string) *Stripe {
stripe.Key = stripeKey
return &Stripe{
logger: logger,
webhookSecret: stripeWebhookSecret,
}
}
func (s *Stripe) CreateCustomer(ctx context.Context, organization *database.Organization) (*Customer, error) {
// Create a new customer
params := &stripe.CustomerParams{
Email: stripe.String(billing.Email(organization)),
Name: stripe.String(organization.ID),
}
c, err := customer.New(params)
if err != nil {
return nil, err
}
return getPaymentCustomerFromStripeCustomer(c), nil
}
func (s *Stripe) FindCustomer(ctx context.Context, customerID string) (*Customer, error) {
c, err := customer.Get(customerID, nil)
if err != nil {
var stripeErr *stripe.Error
if errors.As(err, &stripeErr) && stripeErr.Code == stripe.ErrorCodeResourceMissing {
return nil, billing.ErrNotFound
}
return nil, err
}
return getPaymentCustomerFromStripeCustomer(c), nil
}
func (s *Stripe) FindCustomerForOrg(ctx context.Context, organization *database.Organization) (*Customer, error) {
searchStart := organization.CreatedOn.Add(-5 * time.Minute) // search 5 minutes before the org creation time
searchEnd := organization.CreatedOn.Add(501 * time.Minute) // search 15 minutes after the org creation time
params := &stripe.CustomerListParams{
Email: stripe.String(billing.Email(organization)),
CreatedRange: &stripe.RangeQueryParams{
GreaterThanOrEqual: searchStart.Unix(),
LesserThanOrEqual: searchEnd.Unix(),
},
}
i := customer.List(params)
for i.Next() {
c := i.Customer()
if c.Name == organization.ID {
return getPaymentCustomerFromStripeCustomer(c), nil
}
}
return nil, billing.ErrNotFound
}
func (s *Stripe) UpdateCustomerEmail(ctx context.Context, customerID, email string) error {
params := &stripe.CustomerParams{
Email: stripe.String(email),
}
_, err := customer.Update(customerID, params)
return err
}
func (s *Stripe) DeleteCustomer(ctx context.Context, customerID string) error {
_, err := customer.Del(customerID, nil)
return err
}
func (s *Stripe) GetBillingPortalURL(ctx context.Context, customerID, returnURL string) (string, error) {
params := &stripe.BillingPortalSessionParams{
Customer: stripe.String(customerID),
ReturnURL: stripe.String(returnURL),
}
sess, err := session.New(params)
if err != nil {
return "", err
}
return sess.URL, nil
}
func (s *Stripe) CreateCheckoutSession(ctx context.Context, opts *CheckoutSessionOptions) (*CheckoutSession, error) {
params := &stripe.CheckoutSessionParams{
Customer: stripe.String(opts.CustomerID),
Mode: stripe.String(string(stripe.CheckoutSessionModeSetup)),
// Setup mode allows collecting payment method without charging
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
}),
SuccessURL: stripe.String(opts.SuccessURL),
CancelURL: stripe.String(opts.CancelURL),
// Collect billing address
BillingAddressCollection: stripe.String(string(stripe.CheckoutSessionBillingAddressCollectionRequired)),
// Update customer with the collected payment method
PaymentMethodCollection: stripe.String(string(stripe.CheckoutSessionPaymentMethodCollectionAlways)),
}
sess, err := checkoutsession.New(params)
if err != nil {
return nil, err
}
return &CheckoutSession{
URL: sess.URL,
}, nil
}
func (s *Stripe) WebhookHandlerFunc(ctx context.Context, jc jobs.Client) httputil.Handler {
if s.webhookSecret == "" {
return nil
}
sw := &stripeWebhook{stripe: s, jobs: jc}
return sw.handleWebhook
}
func getPaymentCustomerFromStripeCustomer(c *stripe.Customer) *Customer {
i := customer.ListPaymentMethods(&stripe.CustomerListPaymentMethodsParams{
Customer: stripe.String(c.ID),
})
return &Customer{
ID: c.ID,
Name: c.Name,
Email: c.Email,
HasPaymentMethod: i.Next(),
HasBillableAddress: c.Address != nil,
TaxExempt: c.Address != nil && c.Address.Country != "US" && c.Address.Country != "CA",
}
}