-
Notifications
You must be signed in to change notification settings - Fork 14
/
stripe.go
57 lines (47 loc) · 1.84 KB
/
stripe.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
package stripe
import (
"context"
"github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/checkout/session"
"github.com/stripe/stripe-go/v72/webhook"
)
type Stripe struct {
ctx context.Context
environment string
webhookSecrets map[string]string
}
func NewStripe(token string, webhookSecrets map[string]string, environment string) *Stripe {
stripe.Key = token
return &Stripe{
ctx: context.Background(),
environment: environment,
webhookSecrets: webhookSecrets,
}
}
func (s *Stripe) NewCheckoutSession(paymentMethods []string, mode, successURL, CancelURL string, lineItems []*stripe.CheckoutSessionLineItemParams, discounts []*stripe.CheckoutSessionDiscountParams) *stripe.CheckoutSession {
params := &stripe.CheckoutSessionParams{
PaymentMethodTypes: stripe.StringSlice(paymentMethods),
LineItems: lineItems,
Mode: stripe.String(mode),
SuccessURL: stripe.String(successURL),
CancelURL: stripe.String(CancelURL),
Discounts: discounts,
}
checkoutSession, err := session.New(params)
if err != nil {
panic("failed creating new session for stripe checkout" + err.Error())
}
return checkoutSession
}
func (s *Stripe) ConstructWebhookEvent(reqBody []byte, signature string, webhookKey string) (stripe.Event, error) {
secret, ok := s.webhookSecrets[webhookKey]
if !ok {
panic("stripe webhook secret [" + webhookKey + "] not found")
}
event, err := webhook.ConstructEvent(reqBody, signature, secret)
return event, err
}
type IStripe interface {
ConstructWebhookEvent(reqBody []byte, signature string, webhookKey string) (stripe.Event, error)
NewCheckoutSession(paymentMethods []string, mode, successURL, CancelURL string, lineItems []*stripe.CheckoutSessionLineItemParams, discounts []*stripe.CheckoutSessionDiscountParams) *stripe.CheckoutSession
}