payment

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: AGPL-3.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLocalhostURL added in v0.1.1

func IsLocalhostURL(baseURL string) bool

IsLocalhostURL returns true if the URL's host is a local address. Stripe cannot deliver webhooks to localhost. Only the host counts: a public domain such as localhost-licenses.example.com, or "localhost" in the path or query, is not local.

Types

type StripeHandler

type StripeHandler struct {
	Store         *store.Store
	WebhookSecret string // initial value from config (backward compat)
	BaseURL       string
	Email         *service.EmailService
	WebhookSvc    *service.WebhookService
	// Livemode is the environment this handler is configured for.
	// Every inbound webhook event whose Livemode differs is rejected
	// with 400 — guards against cross-environment delivery (test
	// secret leaking + replay into prod, or vice versa).
	Livemode bool

	// ChangeKey signs subscription change previews (see
	// PreviewSubscriptionChange); every replica must hold the same one.
	ChangeKey []byte
	// contains filtered or unexported fields
}

func (*StripeHandler) CancelForAdmin added in v0.1.16

func (h *StripeHandler) CancelForAdmin(ctx context.Context, lic *model.License) (reverted bool, err error)

CancelForAdmin ends a licence's Stripe subscription for the admin "cancel and revoke" action. A licence an upgrade moved onto that subscription goes back to its free plan instead (reverted true), as a full refund in Stripe would have it; any other is left for the caller to revoke. The subscription is cancelled first either way, and one Stripe has already ended is fine. An error means it may still be billing, so the caller must change nothing.

func (*StripeHandler) CancelSubscription

func (h *StripeHandler) CancelSubscription(c *gin.Context)

func (*StripeHandler) ChangePlan

func (h *StripeHandler) ChangePlan(c *gin.Context)

ChangePlan moves a subscription to another plan, keeping its seats, without a preview: the API form of ChangeSubscription. It is charged the same way (see subscription_change.go): a dearer plan applies once the difference is paid, so status may be pending_payment, with the invoice to pay at hosted_invoice_url, or processing when Stripe's answer did not come back.

There is no proration switch. It was the customer's to set, and "none" let one move to a dearer plan for the rest of a period without paying the difference, then move back before renewal.

func (*StripeHandler) ChangeSubscription added in v0.1.17

func (h *StripeHandler) ChangeSubscription(c *gin.Context)

ChangeSubscription makes a change the customer previewed, exactly as the preview's token says: its target, and its proration date, so Stripe charges what was shown. The token is checked against the subscription as it is now: if the price or seats it started from moved, or it is over an hour old, the customer is asked to look again rather than charged an amount they were not shown.

POST /portal/subscription/change { license_id, preview_token }

→ { status: applied | pending_payment | processing, ... }

func (*StripeHandler) CheckoutByPlan added in v0.1.1

func (h *StripeHandler) CheckoutByPlan(c *gin.Context)

CheckoutByPlan handles GET /pay/:checkout_id — looks up plan by checkout_id, creates a Stripe Checkout Session, and redirects to Stripe.

func (*StripeHandler) CreatePortalSession

func (h *StripeHandler) CreatePortalSession(c *gin.Context)

CreatePortalSession creates a Stripe billing portal session for the user to manage payment methods.

func (*StripeHandler) EndGraceExpiredUpgrade added in v0.1.16

func (h *StripeHandler) EndGraceExpiredUpgrade(ctx context.Context, lic *model.License, _ time.Time) (bool, error)

EndGraceExpiredUpgrade is the grace sweep's hook, for a licence whose grace has run out (see endLapsedUpgrade). The sweep's cutoff is from the plan as it read it; the licence read again is judged by its plan as it is now, as the sweep's own write is (Store.ExpireLicenseAfterGrace), so grace raised meanwhile keeps it.

func (*StripeHandler) EndPastDueUpgrade added in v0.1.16

func (h *StripeHandler) EndPastDueUpgrade(ctx context.Context, lic *model.License, threshold time.Time) (bool, error)

EndPastDueUpgrade is the dunning sweep's hook, for a licence past due since before threshold (see endLapsedUpgrade).

func (*StripeHandler) FollowSubscription added in v0.1.20

func (h *StripeHandler) FollowSubscription(ctx context.Context, lic *model.License) (func(context.Context) error, error)

FollowSubscription is for an operator lifting a suspension from a licence on a Stripe subscription. While the licence was suspended, the subscription's writes were refused (see store.ErrLicenseRestricted): a renewal paid, a pause, a payment that failed. Lifting the suspension hands the licence back to the subscription as Stripe has it now.

It reads the subscription before the suspension is lifted, so a Stripe that cannot answer leaves the licence suspended, and returns what applies it once lifted: a fresh read, taken then so that a change whose event was refused in between is not missed, or this one should that read fail.

func (*StripeHandler) GetWebhookSecret added in v0.1.1

func (h *StripeHandler) GetWebhookSecret() string

GetWebhookSecret returns the current webhook signing secret (thread-safe).

func (*StripeHandler) ListInvoices

func (h *StripeHandler) ListInvoices(c *gin.Context)

ListInvoices returns invoice history for a license's Stripe customer.

func (*StripeHandler) PendingSubscriptionChange added in v0.1.17

func (h *StripeHandler) PendingSubscriptionChange(c *gin.Context)

PendingSubscriptionChange says whether a change waits for payment and where the customer pays for it.

POST /portal/subscription/pending { license_id }

→ { status: none | pending_payment, hosted_invoice_url?, expires_at?, quantity?, plan_name? }

func (*StripeHandler) PreviewSubscriptionChange added in v0.1.17

func (h *StripeHandler) PreviewSubscriptionChange(c *gin.Context)

PreviewSubscriptionChange shows what a change would cost: what is charged now (a credit when negative), and how much of the customer's Stripe balance would pay for it. Stripe's preview leaves the balance out, so it is read separately. Nothing changes in Stripe; a licence found out of line with its subscription is brought into line first.

POST /portal/subscription/preview { license_id, price_id?, quantity? }

func (*StripeHandler) RenewUpdates added in v0.1.6

func (h *StripeHandler) RenewUpdates(c *gin.Context)

RenewUpdates starts a one-time checkout that extends the maintenance period of one of the caller's perpetual licenses.

POST /portal/updates/renew { license_id } → { url }

func (*StripeHandler) RetireReplacedEndpoint added in v0.1.5

func (h *StripeHandler) RetireReplacedEndpoint(ctx context.Context)

RetireReplacedEndpoint deletes a replaced endpoint and forgets its secret once the drain period has passed. Cheap when nothing is pending; called at startup and from the periodic sync.

func (*StripeHandler) SetPreviousWebhookSecret added in v0.1.5

func (h *StripeHandler) SetPreviousWebhookSecret(secret string, until time.Time)

SetPreviousWebhookSecret keeps an older signing secret valid until `until`, so deliveries still queued for a replaced endpoint verify.

func (*StripeHandler) SetWebhookSecret added in v0.1.1

func (h *StripeHandler) SetWebhookSecret(secret string)

SetWebhookSecret updates the webhook signing secret (thread-safe).

func (*StripeHandler) SetupWebhookEndpoint added in v0.1.1

func (h *StripeHandler) SetupWebhookEndpoint(ctx context.Context)

SetupWebhookEndpoint ensures a Stripe webhook endpoint is configured. It runs synchronously on first attempt, then retries in the background if it fails.

func (*StripeHandler) StartUpgrade added in v0.1.16

func (h *StripeHandler) StartUpgrade(c *gin.Context)

StartUpgrade starts a checkout that moves one of the caller's licences to a paid plan. A checkout opened earlier for the licence is expired first: two paid checkouts would leave the second subscription billing with nothing to apply it to.

POST /portal/licenses/upgrade { license_id, plan_id } → { url }

func (*StripeHandler) SubscriptionEnded added in v0.1.7

func (h *StripeHandler) SubscriptionEnded(ctx context.Context, subscriptionID string) (bool, error)

SubscriptionEnded reports whether Stripe is finished with a subscription: cancelled, never completed, or no longer there at all. The admin API asks before letting a licence off a Stripe subscription, because no local state answers it — "canceled" is also what an unpaid subscription reads as, and paying its invoice brings it back.

func (*StripeHandler) SyncCancelStates added in v0.1.12

func (h *StripeHandler) SyncCancelStates(ctx context.Context)

SyncCancelStates records, for Stripe subscriptions whose cancel state was never recorded, whether each renews or ends at the period end: subscriptions from before the state was kept, and every new one, whose state is read here once its licence exists (see fulfillCheckout). Until a licence is synced, the reminders send it neither "renews tomorrow" nor "expiring" (see store.cancelStateSQL).

func (*StripeHandler) SyncLapsedTermHolds added in v0.1.17

func (h *StripeHandler) SyncLapsedTermHolds(ctx context.Context)

SyncLapsedTermHolds settles downgrades whose outcome was never known: once a licence's hold runs out (see store.LowerLicenseTerms), its subscription is read and applied, so a licence lowered for a change Stripe never made gets the terms it pays for back without waiting for an event that may never come. A read applied settles the hold, whatever it wrote (a subscription that has ended holds nothing). A check that cannot finish keeps the record, and is tried again later, behind the others.

func (*StripeHandler) SyncPendingCheckouts added in v0.1.5

func (h *StripeHandler) SyncPendingCheckouts(ctx context.Context)

SyncPendingCheckouts re-checks sessions that completed before their delayed payment (bank debit, voucher) settled. Those can pay hours or days later, outside the window SyncRecentCheckouts scans, so a lost async_payment_succeeded event would otherwise leave a paying customer without a license. Only sessions this server saw complete unpaid are polled; each is dropped once fulfilled or expired.

func (*StripeHandler) SyncRecentCheckouts added in v0.1.1

func (h *StripeHandler) SyncRecentCheckouts(ctx context.Context)

SyncRecentCheckouts scans Stripe for completed checkout sessions in the last interval and creates licenses for any that were missed by webhooks.

func (*StripeHandler) UpgradableLicenses added in v0.1.16

func (h *StripeHandler) UpgradableLicenses(ctx context.Context, email string, ls []*model.License) (map[string]bool, error)

UpgradableLicenses reports which of the licences email owns can be upgraded right now: Stripe is configured, the licence qualifies, and its product has a plan to upgrade to. The portal shows the Upgrade button from this; StartUpgrade checks again regardless.

func (*StripeHandler) VerifyCheckoutSession added in v0.1.1

func (h *StripeHandler) VerifyCheckoutSession(c *gin.Context)

VerifyCheckoutSession handles GET /api/v1/checkout/verify?session_id=xxx Called by the success page to verify payment and create license (webhook fallback).

func (*StripeHandler) Webhook

func (h *StripeHandler) Webhook(c *gin.Context)

Jump to

Keyboard shortcuts

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