entitlements

package
v0.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

Stripe integration

To test webhooks locally:

brew install stripe/stripe-cli/stripe

stripe login and then use your associated credentials

stripe listen --forward-to localhost:17608/v1/stripe/webhook

stripe trigger payment_intent.succeeded

Unprocessed

If your webhook endpoint temporarily can’t process events, Stripe automatically resends the undelivered events to your endpoint for up to three days, increasing the time for your webhook endpoint to eventually receive and process all events.

stripe fixtures ./fixtures.json

stripe trigger payment_intent.succeeded --override payment_intent:amount=5000 --override payment_intent:currency=usd --add payment_intent:customer=cus_xxx

https://github.com/stripe/stripe-cli/tree/master/pkg/fixtures/triggers

Documentation

Overview

Package entitlements is a wrapper package for the entitlements service and a basic stripe integration

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFoundMultipleCustomers is returned when multiple customers are found
	ErrFoundMultipleCustomers = errors.New("found multiple customers with the same name")
	// ErrCustomerNotFound is returned when a customer is not found
	ErrCustomerNotFound = errors.New("customer not found")
	// ErrCustomerLookupFailed is returned when a customer lookup fails
	ErrCustomerLookupFailed = errors.New("failed to lookup customer")
	// ErrCustomerIDRequired is returned when a customer ID is required
	ErrCustomerIDRequired = errors.New("customer ID is required")
	// ErrMissingAPIKey is returned when the API key is missing
	ErrMissingAPIKey = errors.New("missing API key")
)

Functions

func GetOrganizationIDFromMetadata added in v0.7.4

func GetOrganizationIDFromMetadata(metadata map[string]string) string

GetOrganizationIDFromMetadata gets the organization ID from the metadata if it exists, otherwise returns an empty string

func GetOrganizationNameFromMetadata added in v0.7.4

func GetOrganizationNameFromMetadata(metadata map[string]string) string

GetOrganizationNameFromMetadata gets the organization name from the metadata if it exists, otherwise returns an empty string

func GetOrganizationSettingsIDFromMetadata added in v0.7.4

func GetOrganizationSettingsIDFromMetadata(metadata map[string]string) string

GetOrganizationSettingsIDFromMetadata gets the organization settings ID from the metadata if it exists, otherwise returns an empty string

func GetOrganizationSubscriptionIDFromMetadata added in v0.7.4

func GetOrganizationSubscriptionIDFromMetadata(metadata map[string]string) string

GetOrganizationSubscriptionIDFromMetadata gets the organization subscription ID from the metadata if it exists, otherwise returns an empty string

func GetUpdatedFields added in v0.7.4

func GetUpdatedFields(props map[string]interface{}, orgCustomer *OrganizationCustomer) (params *stripe.CustomerParams)

GetUpdatedFields checks for updates to billing information in the properties and returns a stripe.CustomerParams object with the updated information and a boolean indicating whether there are updates

func WithKeys added in v0.5.0

func WithKeys(keys map[string]string) func(*QueryBuilder)

WithKeys sets the keys for the query builder

func WithOperator added in v0.5.0

func WithOperator(operator string) func(*QueryBuilder)

WithOperator sets the operator for the query builder (AND or OR)

func WritePlansToYAML

func WritePlansToYAML(product []Product, filename string) error

WritePlansToYAML writes the []Product information into a YAML file.

func WriteTuplesToYaml added in v0.8.9

func WriteTuplesToYaml(tuples []TupleStruct, filename string) error

WriteTuplesToYaml writes the []TupleStruct information into a YAML file

Types

type BillingPortalSession added in v0.7.4

type BillingPortalSession struct {
	ManageSubscription string `json:"manage_subscription"`
	PaymentMethods     string `json:"payment_methods"`
	Cancellation       string `json:"cancellation"`
	HomePage           string `json:"home_page"`
}

BillingPortalSession holds the billing portal session information

type Checkout

type Checkout struct {
	ID  string `json:"id"`
	URL string `json:"url"`
}

Checkout holds the checkout information

type Config

type Config struct {
	// Enabled determines if the entitlements service is enabled
	Enabled bool `json:"enabled" koanf:"enabled" default:"false"`
	// PublicStripeKey is the key for the stripe service
	PublicStripeKey string `json:"publicStripeKey" koanf:"publicStripeKey" default:""`
	// PrivateStripeKey is the key for the stripe service
	PrivateStripeKey string `json:"privateStripeKey" koanf:"privateStripeKey" default:""`
	// StripeWebhookSecret is the secret for the stripe service
	StripeWebhookSecret string `json:"stripeWebhookSecret" koanf:"stripeWebhookSecret" default:""`
	// TrialSubscriptionPriceID is the price ID for the trial subscription
	TrialSubscriptionPriceID string `json:"trialSubscriptionPriceID" koanf:"trialSubscriptionPriceID" default:"price_1QKLyeBvxky1R7SvaZYGWyQb"`
	// PersonalOrgSubscriptionPriceID is the price ID for the personal org subscription
	PersonalOrgSubscriptionPriceID string `json:"personalOrgSubscriptionPriceID" koanf:"personalOrgSubscriptionPriceID" default:"price_1QycPyBvxky1R7Svz0gOWnNh"`
	// StripeWebhookURL is the URL for the stripe webhook
	StripeWebhookURL string `json:"stripeWebhookURL" koanf:"stripeWebhookURL" default:"https://api.openlane.com/v1/stripe/webhook"`
	// StripeBillingPortalSuccessURL
	StripeBillingPortalSuccessURL string `json:"stripeBillingPortalSuccessURL" koanf:"stripeBillingPortalSuccessURL" default:"https://console.openlane.com/billing"`
	// StripeCancellationReturnURL is the URL for the stripe cancellation return
	StripeCancellationReturnURL string `` /* 163-byte string literal not displayed */
	// SaaSPricingTiers are the pricing tiers for the SaaS product
	SaaSPricingTiers []PricingTier `json:"saasPricingTiers" koanf:"saasPricingTiers"`
	// Features are the features for the SaaS product
	Features []Feature `json:"features" koanf:"features"`
}

func NewConfig added in v0.5.0

func NewConfig(opts ...ConfigOpts) *Config

NewConfig creates a new entitlements config

type ConfigOpts added in v0.5.0

type ConfigOpts func(*Config)

func WithEnabled added in v0.5.0

func WithEnabled(enabled bool) ConfigOpts

WithEnabled sets the enabled field

func WithPersonalOrgSubscriptionPriceID added in v0.7.3

func WithPersonalOrgSubscriptionPriceID(personalOrgSubscriptionPriceID string) ConfigOpts

WithPersonalOrgSubscriptionPriceID sets the personal org subscription price ID

func WithPrivateStripeKey added in v0.5.0

func WithPrivateStripeKey(privateStripeKey string) ConfigOpts

WithPrivateStripeKey sets the private stripe key

func WithPublicStripeKey added in v0.5.0

func WithPublicStripeKey(publicStripeKey string) ConfigOpts

WithPublicStripeKey sets the public stripe key

func WithStripeBillingPortalSuccessURL added in v0.5.0

func WithStripeBillingPortalSuccessURL(stripeBillingPortalSuccessURL string) ConfigOpts

WithStripeBillingPortalSuccessURL sets the stripe billing portal success URL

func WithStripeCancellationReturnURL added in v0.7.4

func WithStripeCancellationReturnURL(stripeCancellationReturnURL string) ConfigOpts

WithStripeCancellationReturnURL sets the stripe cancellation return URL

func WithStripeWebhookSecret added in v0.5.0

func WithStripeWebhookSecret(stripeWebhookSecret string) ConfigOpts

WithStripeWebhookSecret sets the stripe webhook secret

func WithStripeWebhookURL added in v0.5.0

func WithStripeWebhookURL(stripeWebhookURL string) ConfigOpts

WithStripeWebhookURL sets the stripe webhook URL

func WithTrialSubscriptionPriceID

func WithTrialSubscriptionPriceID(trialSubscriptionPriceID string) ConfigOpts

WithTrialSubscriptionPriceID sets the trial subscription price ID

type ContactInfo added in v0.6.5

type ContactInfo struct {
	Email      string  `json:"email"`
	Phone      string  `json:"phone"`
	City       *string `form:"city"`
	Country    *string `form:"country"`
	Line1      *string `form:"line1"`
	Line2      *string `form:"line2"`
	PostalCode *string `form:"postal_code"`
	State      *string `form:"state"`
}

ContactInfo holds the contact information for the organization

type Customer

type Customer struct {
	ID             string `json:"customer_id" yaml:"customer_id"`
	Email          string `json:"email" yaml:"email"`
	Phone          string `json:"phone" yaml:"phone"`
	Address        string `json:"address" yaml:"address"`
	Plans          []Plan `json:"plans" yaml:"plans"`
	Metadata       map[string]string
	StripeParams   *stripe.CustomerParams
	StripeCustomer []stripe.Customer
}

Customer holds the customer information

type Feature

type Feature struct {
	ID        string `json:"id" yaml:"id"`
	Name      string `json:"name" yaml:"name"`
	Lookupkey string `json:"lookupkey" yaml:"lookupkey"`
}

Feature are part of a product

type OrganizationCustomer added in v0.5.0

type OrganizationCustomer struct {
	OrganizationID             string `json:"organization_id"`
	OrganizationSettingsID     string `json:"organization_settings_id"`
	StripeCustomerID           string `json:"stripe_customer_id"`
	OrganizationName           string `json:"organization_name"`
	PersonalOrg                bool   `json:"personal_org"`
	OrganizationSubscriptionID string `json:"organization_subscription_id"`
	StripeSubscriptionID       string `json:"stripe_subscription_id"`
	PaymentMethodAdded         bool   `json:"payment_method_added"`
	Features                   []string
	FeatureNames               []string

	Subscription
	ContactInfo
}

OrganizationCustomer is a struct which holds both internal organization infos and external stripe infos

func MapStripeCustomer added in v0.7.4

func MapStripeCustomer(c *stripe.Customer) *OrganizationCustomer

MapStripeCustomer maps a stripe customer to an organization customer this is used to convert the stripe customer object to our internal customer object we use the metadata to store the organization ID, settings ID, and subscription ID

func (*OrganizationCustomer) MapToStripeCustomer added in v0.5.0

func (o *OrganizationCustomer) MapToStripeCustomer() *stripe.CustomerParams

func (*OrganizationCustomer) Validate added in v0.5.0

func (o *OrganizationCustomer) Validate() error

Validate checks if the OrganizationCustomer contains necessary fields

type Plan

type Plan struct {
	ID                 string `json:"plan_id" yaml:"plan_id"`
	Product            string `json:"product_id" yaml:"product_id"`
	Price              string `json:"price_id" yaml:"price_id"`
	StartDate          int64  `json:"start_date" yaml:"start_date"`
	EndDate            int64  `json:"end_date" yaml:"end_date"`
	StripeParams       *stripe.SubscriptionParams
	StripeSubscription []stripe.Subscription
	Products           []Product
	Features           []Feature
	TrialEnd           int64
	Status             string
}

Plan is the recurring context that holds the payment information

type Price

type Price struct {
	ID           string              `json:"price_id" yaml:"price_id"`
	Price        float64             `json:"price" yaml:"price"`
	Currency     string              `json:"currency" yaml:"currency"`
	ProductID    string              `json:"product_id" yaml:"-"`
	ProductName  string              `json:"product_name" yaml:"product_name"`
	Interval     string              `json:"interval" yaml:"interval"`
	StripeParams *stripe.PriceParams `json:"stripe_params,omitempty" yaml:"stripe_params,omitempty"`
	StripePrice  []stripe.Price      `json:"stripe_price,omitempty" yaml:"stripe_price,omitempty"`
}

Price holds stripe price params and the associated Product

type PricingTier added in v0.8.9

type PricingTier struct {
	Name     string   `json:"name" koanf:"name"`
	ID       string   `json:"id" koanf:"id"`
	Price    int64    `json:"price" koanf:"price"`
	Features []string `json:"features" koanf:"features"` // List of feature IDs
}

PricingTier is the defined relationship between the product and price

type Product

type Product struct {
	ID            string                `json:"product_id" yaml:"product_id"`
	Name          string                `json:"name" yaml:"name"`
	Description   string                `json:"description,omitempty" yaml:"description,omitempty"`
	Features      []Feature             `json:"features" yaml:"features"`
	Prices        []Price               `json:"prices" yaml:"prices"`
	StripeParams  *stripe.ProductParams `json:"stripe_params,omitempty" yaml:"stripe_params,omitempty"`
	StripeProduct []stripe.Product      `json:"stripe_product,omitempty" yaml:"stripe_product,omitempty"`
}

Product holds what we'd more commply call a "tier"

type ProductFeature

type ProductFeature struct {
	FeatureID string `json:"feature_id" yaml:"feature_id"`
	ProductID string `json:"product_id"`
	Name      string `json:"name" yaml:"name"`
	Lookupkey string `json:"lookupkey" yaml:"lookupkey"`
}

ProductFeature is the defined relationship between the product and feature

type QueryBuilder added in v0.5.0

type QueryBuilder struct {
	// contains filtered or unexported fields
}

QueryBuilder is a struct that holds the query builder

func NewQueryBuilder added in v0.5.0

func NewQueryBuilder(options ...func(*QueryBuilder)) *QueryBuilder

NewQueryBuilder creates a new query builder with included options

func (*QueryBuilder) BuildQuery added in v0.5.0

func (qb *QueryBuilder) BuildQuery() []string

BuildQuery builds the query from the keys

type StripeClient

type StripeClient struct {
	// Client is the Stripe client and is used for accessing all subsequent stripe objects, e.g. products, prices, etc.
	Client *client.API

	// config is the configuration for the Stripe client
	Config Config
	// contains filtered or unexported fields
}

StripeClient is a client for the Stripe API

func NewStripeClient

func NewStripeClient(opts ...StripeOptions) (*StripeClient, error)

NewStripeClient creates a new Stripe client

func (*StripeClient) CancelSubscription

func (sc *StripeClient) CancelSubscription(id string, params *stripe.SubscriptionCancelParams) (*stripe.Subscription, error)

CancelSubscription cancels a subscription

func (*StripeClient) CancellationBillingPortalSession added in v0.7.4

func (sc *StripeClient) CancellationBillingPortalSession(subsID, custID string) (*BillingPortalSession, error)

CancellationBillingPortalSession generates a session in stripe's billing portal which allows the customer to cancel their subscription

func (*StripeClient) CreateBillingPortalPaymentMethods added in v0.7.4

func (sc *StripeClient) CreateBillingPortalPaymentMethods(custID string) (*BillingPortalSession, error)

CreateBillingPortalPaymentMethods generates a session in stripe's billing portal which allows the customer to add / update payment methods

func (*StripeClient) CreateBillingPortalUpdateSession

func (sc *StripeClient) CreateBillingPortalUpdateSession(subsID, custID string) (*BillingPortalSession, error)

CreateBillingPortalUpdateSession generates an update session in stripe's billing portal which displays the customers current subscription tier and allows them to upgrade or downgrade

func (*StripeClient) CreateCustomer

func (sc *StripeClient) CreateCustomer(c *OrganizationCustomer) (*stripe.Customer, error)

CreateCustomer creates a customer leveraging the openlane organization ID as the organization name, and the email provided as the billing email we assume that the billing email will be changed, so lookups are performed by the organization ID

func (*StripeClient) CreatePersonalOrgFreeTierSubs added in v0.7.3

func (sc *StripeClient) CreatePersonalOrgFreeTierSubs(customerID string) (*Subscription, error)

CreatePersonalOrgFreeTierSubs creates a subscription with the configured $0 price used for personal organizations only

func (*StripeClient) CreatePrice added in v0.8.9

func (sc *StripeClient) CreatePrice(productID string, unitAmount int64, currency, interval string) (*stripe.Price, error)

CreatePrice a price for a product in stripe

func (*StripeClient) CreateProduct added in v0.8.9

func (sc *StripeClient) CreateProduct(name, description string) (*stripe.Product, error)

CreateProduct creates a new product in Stripe

func (*StripeClient) CreateSubscription

func (sc *StripeClient) CreateSubscription(params *stripe.SubscriptionParams) (*stripe.Subscription, error)

CreateSubscription creates a new subscription

func (*StripeClient) CreateTrialSubscription

func (sc *StripeClient) CreateTrialSubscription(cust *stripe.Customer) (*Subscription, error)

CreateTrialSubscription creates a trial subscription with the configured price

func (*StripeClient) CreateTupleStruct added in v0.8.9

func (sc *StripeClient) CreateTupleStruct() []TupleStruct

func (*StripeClient) DeleteCustomer

func (sc *StripeClient) DeleteCustomer(id string) error

DeleteCustomer deletes a customer by ID from stripe

func (*StripeClient) DeleteProduct added in v0.8.9

func (sc *StripeClient) DeleteProduct(productID string) (*stripe.Product, error)

DeleteProduct deletes a product in Stripe

func (*StripeClient) FindOrCreateCustomer added in v0.6.7

func (sc *StripeClient) FindOrCreateCustomer(ctx context.Context, o *OrganizationCustomer) error

FindOrCreateCustomer attempts to lookup a customer by the organization ID which is set in both the name field attribute as well as in the object metadata field

func (*StripeClient) GetAllProductPricesMapped added in v0.8.9

func (sc *StripeClient) GetAllProductPricesMapped() []Product

GetAllProductPricesMapped retrieves all products and prices from stripe which are active

func (*StripeClient) GetCustomerByStripeID added in v0.5.0

func (sc *StripeClient) GetCustomerByStripeID(ctx context.Context, customerID string) (*stripe.Customer, error)

GetCustomerByStripeID gets a customer by ID

func (*StripeClient) GetFeaturesByProductID added in v0.8.9

func (sc *StripeClient) GetFeaturesByProductID(productID string) []ProductFeature

GetFeaturesByProductID retrieves all product features from stripe which are active and maps them into a []ProductFeature struct

func (*StripeClient) GetPrice added in v0.8.9

func (sc *StripeClient) GetPrice(priceID string) (*stripe.Price, error)

GetPrice retrieves a price from stripe by its ID

func (*StripeClient) GetPricesMapped added in v0.8.9

func (sc *StripeClient) GetPricesMapped() []Price

GetPricesMapped retrieves all prices from stripe which are active and maps them into a []Price struct

func (*StripeClient) GetProduct added in v0.8.9

func (sc *StripeClient) GetProduct(productID string) (*stripe.Product, error)

GetProduct retrieves a product by its ID

func (*StripeClient) GetProductByID added in v0.6.7

func (sc *StripeClient) GetProductByID(id string) (*stripe.Product, error)

GetProductByID gets a product by ID

func (*StripeClient) GetSubscriptionByID

func (sc *StripeClient) GetSubscriptionByID(id string) (*stripe.Subscription, error)

GetSubscriptionByID gets a subscription by ID

func (*StripeClient) ListOrCreateSubscriptions

func (sc *StripeClient) ListOrCreateSubscriptions(customerID string) (*Subscription, error)

ListStripeSubscriptions lists stripe subscriptions by customer

func (*StripeClient) ListPrices added in v0.8.9

func (sc *StripeClient) ListPrices() ([]*stripe.Price, error)

ListPrices retrieves all prices from stripe

func (*StripeClient) ListProducts added in v0.8.9

func (sc *StripeClient) ListProducts() ([]*stripe.Product, error)

ListProducts lists all products in Stripe

func (*StripeClient) MapStripeSubscription added in v0.7.4

func (sc *StripeClient) MapStripeSubscription(subs *stripe.Subscription) *Subscription

MapStripeSubscription maps a stripe.Subscription to a "internal" subscription struct

func (*StripeClient) SearchCustomers added in v0.5.0

func (sc *StripeClient) SearchCustomers(ctx context.Context, query string) (customers []*stripe.Customer, err error)

SearchCustomers searches for customers with a structured stripe query as input leverage QueryBuilder to construct more complex queries, otherwise see: https://docs.stripe.com/search#search-query-language

func (*StripeClient) UpdateCustomer

func (sc *StripeClient) UpdateCustomer(id string, params *stripe.CustomerParams) (*stripe.Customer, error)

UpdateCustomer updates a customer in stripe with the provided params and ID

func (*StripeClient) UpdateProduct added in v0.8.9

func (sc *StripeClient) UpdateProduct(productID, name, description string) (*stripe.Product, error)

UpdateProduct updates a product in Stripe

func (*StripeClient) UpdateSubscription

func (sc *StripeClient) UpdateSubscription(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error)

UpdateSubscription updates a subscription

type StripeOptions

type StripeOptions func(*StripeClient)

StripeOptions is a type for setting options on the Stripe client

func WithAPIKey

func WithAPIKey(apiKey string) StripeOptions

WithAPIKey sets the API key for the Stripe client

func WithBackends added in v0.7.4

func WithBackends(backends *stripe.Backends) StripeOptions

WithBackends sets the backends for the Stripe client

func WithConfig added in v0.5.0

func WithConfig(config Config) StripeOptions

WithConfig sets the config for the Stripe client

type Subscription

type Subscription struct {
	ID                 string `json:"plan_id" yaml:"plan_id"`
	ProductID          string `json:"product_id" yaml:"product_id"`
	PriceID            string `json:"price_id" yaml:"price_id"`
	StartDate          int64  `json:"start_date" yaml:"start_date"`
	EndDate            int64  `json:"end_date" yaml:"end_date"`
	StripeParams       *stripe.SubscriptionParams
	StripeSubscription []stripe.Subscription
	StripeProduct      []stripe.Product
	StripeFeature      []stripe.ProductFeature
	Products           []Product
	Features           []Feature
	Prices             []Price
	TrialEnd           int64
	Status             string
	StripeCustomerID   string
	OrganizationID     string
	DaysUntilDue       int64
	Metadata           map[string]string
}

Subscription is the recurring context that holds the payment information

type TupleStruct added in v0.8.9

type TupleStruct struct {
	User     string `yaml:"user"`
	Relation string `yaml:"relation"`
	Object   string `yaml:"object"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL