Documentation
¶
Overview ¶
Package contracts defines the payment-gateway contract and the shared value types every gateway and the manager operate on. It has no dependencies on the concrete gateways, so both the gateways and the root cashier package depend on it without cycles.
Index ¶
- Variables
- type CancelParams
- type Charge
- type ChargeParams
- type Customer
- type CustomerGateway
- type CustomerParams
- type IAPEntitlement
- type IAPPlatform
- type IAPVerifier
- type PaymentGateway
- type PaymentProof
- type ReceiptParams
- type Refund
- type RefundGateway
- type RefundParams
- type RefundStatus
- type StoreNotification
- type Subscription
- type SubscriptionGateway
- type SubscriptionParams
- type SubscriptionPauser
- type SubscriptionStatus
- type SubscriptionSwapper
- type SwapParams
- type WebhookEvent
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.New("cashier: operation not supported by this gateway")
ErrUnsupported is returned by a gateway for an operation it doesn't implement (e.g. VerifyPayment on a redirect-only gateway).
Functions ¶
This section is empty.
Types ¶
type CancelParams ¶ added in v1.6.0
type CancelParams struct {
// AtPeriodEnd keeps access until the paid period runs out rather than
// revoking immediately — the usual, humane default for a cancellation.
AtPeriodEnd bool
// Comment is recorded with the gateway where supported.
Comment string
}
CancelParams tunes how a subscription ends.
type Charge ¶
type Charge struct {
Gateway string // "stripe" | "razorpay" | …
ID string // Checkout session (Stripe) or order (Razorpay)
RedirectURL string // set for hosted-checkout gateways; empty for widgets
Amount int64
Currency string
Raw map[string]any // decoded gateway response
}
Charge is the result of creating a payment.
type ChargeParams ¶
type ChargeParams struct {
Amount int64 // smallest currency unit (paise for INR, cents for USD)
Currency string // "INR", "USD", …
Mode string // "subscription" | "payment" (Stripe). Default "payment".
PriceID string // Stripe Price (price_…) for recurring billing.
CustomerID string
CustomerEmail string
SuccessURL string // redirect (hosted-checkout) gateways
CancelURL string
Reference string // your order id (Razorpay receipt / correlation)
Metadata map[string]string
}
ChargeParams is a gateway-agnostic request to start a payment. Gateways use different subsets:
- Stripe (hosted checkout): Mode + PriceID (or Amount/Currency) + URLs → a redirect URL.
- Razorpay (widget): Amount + Currency + Reference → an order id the frontend opens.
type CustomerGateway ¶ added in v1.6.0
type CustomerGateway interface {
CreateCustomer(ctx context.Context, p CustomerParams) (*Customer, error)
GetCustomer(ctx context.Context, id string) (*Customer, error)
}
CustomerGateway is implemented by gateways with a persistent customer object.
type CustomerParams ¶ added in v1.6.0
type CustomerParams struct {
Subject string // your user id
Email string
Name string
Phone string
Metadata map[string]string
}
CustomerParams creates or updates a gateway customer. A stable customer is what lets a subscription and a saved card outlive a single checkout.
type IAPEntitlement ¶ added in v1.6.0
type IAPEntitlement struct {
Platform IAPPlatform
ProductID string
Subject string
TransactionID string
// OriginalTransactionID ties every renewal of one subscription together;
// it is the stable key to mirror against, not the per-renewal id.
OriginalTransactionID string
Subscription bool
Active bool
ExpiresAt *time.Time
// PriceMicros is the transaction value in millionths of a currency unit
// (1_000_000 = one dollar), and Currency its ISO code. Metering bills on
// this, so a store that does not report a price yields zero and is treated
// as non-billable rather than guessed at.
PriceMicros int64
Currency string
// AutoRenewing is the store's current intent; a subscription can be active
// now yet set not to renew.
AutoRenewing bool
// Environment is "production" or "sandbox"; a sandbox receipt reaching a
// production server is the classic test-purchase-as-real bug.
Environment string
Raw map[string]any
}
Entitlement is the verified result of a receipt check: what the store confirms the user owns, and until when for a subscription.
type IAPPlatform ¶ added in v1.6.0
type IAPPlatform string
IAPPlatform distinguishes the two stores.
const ( PlatformApple IAPPlatform = "apple" PlatformGoogle IAPPlatform = "google" )
type IAPVerifier ¶ added in v1.6.0
type IAPVerifier interface {
Platform() IAPPlatform
// VerifyReceipt checks a client-supplied purchase against the store and
// returns the entitlement the store actually vouches for.
VerifyReceipt(ctx context.Context, p ReceiptParams) (*IAPEntitlement, error)
// ParseNotification verifies and decodes a server-to-server notification.
ParseNotification(payload []byte) (*StoreNotification, error)
}
IAPVerifier verifies purchases and store notifications for one platform.
type PaymentGateway ¶
type PaymentGateway interface {
Name() string
CreateCharge(ctx context.Context, p ChargeParams) (*Charge, error)
VerifyPayment(ctx context.Context, proof PaymentProof) (bool, error)
VerifyWebhook(payload []byte, headers http.Header) (*WebhookEvent, error)
}
PaymentGateway is the contract every payment provider implements.
type PaymentProof ¶
PaymentProof is a client-returned proof of a completed payment, verified server-side (primarily Razorpay: order_id + payment_id + signature).
type ReceiptParams ¶ added in v1.6.0
type ReceiptParams struct {
Platform IAPPlatform
// ProductID is the store product the app reports buying; verification must
// confirm the signed payload names the same product.
ProductID string
Subject string // your user id
// Apple: the JWS transaction from StoreKit 2 (or a legacy base64 receipt).
// Google: the purchase token returned by the Play Billing library.
Token string
// Google needs the product kind to pick the right API; Apple does not.
Subscription bool
}
ReceiptParams is a client-supplied pointer to a purchase, to be verified server-side against the store.
type Refund ¶ added in v1.6.0
type Refund struct {
Gateway string
ID string
PaymentID string
Amount int64
Currency string
Status RefundStatus
Raw map[string]any
}
Refund is a processed refund reduced to what Nimbus records.
type RefundGateway ¶ added in v1.6.0
type RefundGateway interface {
Refund(ctx context.Context, p RefundParams) (*Refund, error)
}
RefundGateway is implemented by gateways that can reverse a payment.
type RefundParams ¶ added in v1.6.0
type RefundParams struct {
PaymentID string
Amount int64 // smallest unit; 0 → full refund
Currency string
Reason string // "requested_by_customer" | "duplicate" | "fraudulent" | provider-specific
Idempotency string
Metadata map[string]string
}
RefundParams requests a refund. A zero Amount refunds the whole payment.
type RefundStatus ¶ added in v1.6.0
type RefundStatus string
RefundStatus is the canonical state of a refund.
const ( RefundPending RefundStatus = "pending" RefundSucceeded RefundStatus = "succeeded" RefundFailed RefundStatus = "failed" )
type StoreNotification ¶ added in v1.6.0
type StoreNotification struct {
Platform IAPPlatform
Type string // canonical: "renewed" | "canceled" | "expired" | "refunded" | "grace_period" | provider-specific
ProductID string
OriginalTransactionID string
ExpiresAt *time.Time
Raw []byte
}
StoreNotification is a verified server-to-server notification from a store (Apple App Store Server Notifications V2, Google Real-time Developer Notifications), reduced to a canonical shape.
type Subscription ¶ added in v1.6.0
type Subscription struct {
Gateway string
ID string // gateway subscription id
Status SubscriptionStatus // canonical
PlanID string
CustomerID string
Subject string
CurrentPeriodEnd *time.Time
TrialEnd *time.Time
CancelAt *time.Time // set when scheduled to cancel at period end
CanceledAt *time.Time
EndedAt *time.Time
// AuthURL is a redirect the customer must complete to authorise the mandate
// (e-mandate / SCA). Empty when the subscription is active immediately.
AuthURL string
Raw map[string]any
}
Subscription is the gateway's subscription reduced to what Nimbus mirrors.
type SubscriptionGateway ¶ added in v1.6.0
type SubscriptionGateway interface {
CreateSubscription(ctx context.Context, p SubscriptionParams) (*Subscription, error)
GetSubscription(ctx context.Context, id string) (*Subscription, error)
CancelSubscription(ctx context.Context, id string, p CancelParams) (*Subscription, error)
}
SubscriptionGateway is implemented by gateways that run recurring billing.
type SubscriptionParams ¶ added in v1.6.0
type SubscriptionParams struct {
PlanID string // Stripe price_… / Razorpay plan_… / provider plan ref
CustomerID string // gateway customer id, when the gateway needs one
Subject string // your user id, mirrored onto the local record
Quantity int64
TrialDays int
TrialEnd *time.Time
CouponCode string
Metadata map[string]string
Idempotency string // caller-supplied key; gateways that support it dedupe on it
// TotalCycles bounds a fixed-length subscription (0 = until cancelled).
TotalCycles int
// NotifyURL / ReturnURL for gateways that authenticate the mandate via a
// redirect (Indian e-mandate flows).
NotifyURL string
ReturnURL string
}
SubscriptionParams is a gateway-agnostic request to start a subscription.
Gateways use different subsets, the same way ChargeParams does: Stripe needs a PriceID, Razorpay a PlanID and often a pre-created customer, and both accept a trial and metadata.
type SubscriptionPauser ¶ added in v1.6.0
type SubscriptionPauser interface {
PauseSubscription(ctx context.Context, id string) (*Subscription, error)
ResumeSubscription(ctx context.Context, id string) (*Subscription, error)
}
SubscriptionPauser is optional pause/resume support (Stripe, Razorpay).
type SubscriptionStatus ¶ added in v1.6.0
type SubscriptionStatus string
SubscriptionStatus is the canonical local view of a subscription, mapped from each gateway's own vocabulary so access checks read the same everywhere.
const ( SubActive SubscriptionStatus = "active" SubTrialing SubscriptionStatus = "trialing" SubPastDue SubscriptionStatus = "past_due" SubPaused SubscriptionStatus = "paused" SubCanceled SubscriptionStatus = "canceled" SubExpired SubscriptionStatus = "expired" SubPending SubscriptionStatus = "pending" // created, awaiting first payment/authentication SubUnknown SubscriptionStatus = "unknown" )
func (SubscriptionStatus) Grants ¶ added in v1.6.0
func (s SubscriptionStatus) Grants() bool
Grants reports whether a status currently entitles the subject to access.
type SubscriptionSwapper ¶ added in v1.6.0
type SubscriptionSwapper interface {
SwapSubscription(ctx context.Context, id string, p SwapParams) (*Subscription, error)
}
SubscriptionSwapper is the optional slice of subscription support for changing plans mid-term; not every gateway allows it.
type SwapParams ¶ added in v1.6.0
type SwapParams struct {
NewPlanID string
Quantity int64
// Prorate bills or credits the difference immediately. Off defers the new
// price to the next cycle.
Prorate bool
}
SwapParams changes a subscription's plan.