forked from Nixius/authelia
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package stripe
|
|
|
|
import (
|
|
"git.nixc.us/a250/ss-atlas/internal/config"
|
|
stripego "github.com/stripe/stripe-go/v84"
|
|
portalsession "github.com/stripe/stripe-go/v84/billingportal/session"
|
|
checkoutsession "github.com/stripe/stripe-go/v84/checkout/session"
|
|
"github.com/stripe/stripe-go/v84/subscription"
|
|
)
|
|
|
|
type Client struct {
|
|
cfg *config.Config
|
|
}
|
|
|
|
func New(cfg *config.Config) *Client {
|
|
stripego.Key = cfg.StripeSecretKey
|
|
return &Client{cfg: cfg}
|
|
}
|
|
|
|
func (c *Client) CreateCheckoutSession(email string) (*stripego.CheckoutSession, error) {
|
|
params := &stripego.CheckoutSessionParams{
|
|
Mode: stripego.String(string(stripego.CheckoutSessionModeSubscription)),
|
|
LineItems: []*stripego.CheckoutSessionLineItemParams{
|
|
{
|
|
Price: stripego.String(c.cfg.StripePriceID),
|
|
Quantity: stripego.Int64(1),
|
|
},
|
|
},
|
|
CustomerEmail: stripego.String(email),
|
|
SuccessURL: stripego.String(c.cfg.AppURL + "/success?session_id={CHECKOUT_SESSION_ID}"),
|
|
CancelURL: stripego.String(c.cfg.AppURL + "/"),
|
|
}
|
|
return checkoutsession.New(params)
|
|
}
|
|
|
|
func (c *Client) CreatePortalSession(customerID string) (*stripego.BillingPortalSession, error) {
|
|
params := &stripego.BillingPortalSessionParams{
|
|
Customer: stripego.String(customerID),
|
|
ReturnURL: stripego.String(c.cfg.AppURL + "/dashboard"),
|
|
}
|
|
return portalsession.New(params)
|
|
}
|
|
|
|
func (c *Client) GetCheckoutSession(sessionID string) (*stripego.CheckoutSession, error) {
|
|
params := &stripego.CheckoutSessionParams{}
|
|
params.AddExpand("customer")
|
|
params.AddExpand("subscription")
|
|
return checkoutsession.Get(sessionID, params)
|
|
}
|
|
|
|
func (c *Client) GetSubscription(subID string) (*stripego.Subscription, error) {
|
|
return subscription.Get(subID, nil)
|
|
}
|
|
|
|
func (c *Client) WebhookSecret() string {
|
|
return c.cfg.StripeWebhookSecret
|
|
}
|