payments

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListProviders

func ListProviders() []string

ListProviders returns a list of registered payment provider identifiers.

func RegisterProvider

func RegisterProvider(provider PaymentProvider)

RegisterProvider registers a payment provider.

Types

type AP2Constraints

type AP2Constraints struct {
	MaxAmount         float64  `json:"max_amount"`
	Currency          string   `json:"currency"`
	MerchantAllowlist []string `json:"merchant_allowlist"`
}

type AP2MandateClaims

type AP2MandateClaims struct {
	Issuer            string     `json:"iss"`
	Subject           string     `json:"sub"`
	IssuedAt          int64      `json:"iat"`
	Expiration        int64      `json:"exp"`
	CredentialSubject AP2Subject `json:"credentialSubject"`
	VC                *struct {
		CredentialSubject AP2Subject `json:"credentialSubject"`
	} `json:"vc,omitempty"`
}

func VerifyAP2Mandate

func VerifyAP2Mandate(ctx context.Context, input AP2VerificationInput, amount float64, currency string, merchantHost string, userPublicKeyPEM string) (*AP2MandateClaims, error)

VerifyAP2Mandate cryptographically validates the user's mandate and the agent's assertion.

func (*AP2MandateClaims) GetSubject

func (c *AP2MandateClaims) GetSubject() AP2Subject

func (*AP2MandateClaims) VerifyConstraints

func (c *AP2MandateClaims) VerifyConstraints(amount float64, currency string, merchantHost string) error

type AP2Subject

type AP2Subject struct {
	AgentKey    JWK            `json:"agentKey"`
	Constraints AP2Constraints `json:"constraints"`
}

type AP2VerificationInput

type AP2VerificationInput struct {
	MandateJWT     string         `json:"mandate_jwt"`     // User-signed mandate VC (JWT)
	AgentAssertion string         `json:"agent_assertion"` // Agent-signed JWT asserting the transaction
	OrderID        string         `json:"order_id"`
	Provider       string         `json:"provider"` // "stripe", "razorpay", "mock"
	Payload        map[string]any `json:"payload"`  // Additional gateway payload (Stripe payment method token, etc.)
}

type AgentAssertionClaims

type AgentAssertionClaims struct {
	OrderID  string  `json:"order_id"`
	Amount   float64 `json:"amount"`
	Currency string  `json:"currency"`
	Nonce    string  `json:"nonce"`
	IssuedAt int64   `json:"iat"`
}

type Handlers

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

func NewHandlers

func NewHandlers(db *gorm.DB, rt mdk.Runtime) *Handlers

func (*Handlers) CreateIntent

func (h *Handlers) CreateIntent(ctx context.Context, orderID string, providerName string) (*PaymentTransaction, error)

CreateIntent registers a payment intent/order with the provider and GORM.

func (*Handlers) CreateIntentStep

func (h *Handlers) CreateIntentStep(sCtx mdk.StepContext) mdk.StepResult

CreateIntentStep wraps CreateIntent to mdk.StepHandler.

func (*Handlers) Refund

func (h *Handlers) Refund(ctx context.Context, transactionID string, amount float64) (string, error)

Refund processes refunds (used as Sage rollback compensations).

func (*Handlers) RefundStep

func (h *Handlers) RefundStep(sCtx mdk.StepContext) mdk.StepResult

RefundStep wraps Refund to mdk.StepHandler.

func (*Handlers) VerifyAP2

VerifyAP2 verifies an AP2 agent payment mandate and processes the charge via the selected gateway provider.

func (*Handlers) VerifyAP2Step

func (h *Handlers) VerifyAP2Step(sCtx mdk.StepContext) mdk.StepResult

VerifyAP2Step wraps VerifyAP2 to mdk.StepHandler.

func (*Handlers) VerifyPayment

func (h *Handlers) VerifyPayment(ctx context.Context, orderID string, providerName string, payload map[string]any) (*PaymentTransaction, error)

VerifyPayment confirms checkout signature/token values, updating order and transaction states.

func (*Handlers) VerifyPaymentStep

func (h *Handlers) VerifyPaymentStep(sCtx mdk.StepContext) mdk.StepResult

VerifyPaymentStep wraps VerifyPayment to mdk.StepHandler.

type JWK

type JWK struct {
	Kty string `json:"kty"`
	Crv string `json:"crv"`
	X   string `json:"x"`
	Y   string `json:"y"`
}

type MockProvider

type MockProvider struct{}

func NewMockProvider

func NewMockProvider() *MockProvider

func (*MockProvider) CreateIntent

func (m *MockProvider) CreateIntent(ctx context.Context, orderID string, amount float64, currency string) (*PaymentIntent, error)

func (*MockProvider) ID

func (m *MockProvider) ID() string

func (*MockProvider) Refund

func (m *MockProvider) Refund(ctx context.Context, transactionID string, amount float64) (string, error)

func (*MockProvider) VerifyPayment

func (m *MockProvider) VerifyPayment(ctx context.Context, payload map[string]any) (bool, error)

type Module

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

func NewModule

func NewModule() *Module

func (*Module) CreatePaymentIntent

func (m *Module) CreatePaymentIntent(ctx context.Context, orderID string, provider string) (*PaymentIntent, error)

func (*Module) FieldResolvers

func (m *Module) FieldResolvers() map[string]any

func (*Module) GetPaymentTransaction

func (m *Module) GetPaymentTransaction(ctx context.Context, id string) (*PaymentTransaction, error)

func (*Module) GetTransactionStep

func (m *Module) GetTransactionStep(sCtx mdk.StepContext) mdk.StepResult

func (*Module) ID

func (m *Module) ID() string

func (*Module) Init

func (m *Module) Init(ctx context.Context, rt mdk.Runtime) error

func (*Module) ListActivePaymentProviders

func (m *Module) ListActivePaymentProviders(ctx context.Context) ([]*PaymentProviderInfo, error)

func (*Module) ListOrderTransactions

func (m *Module) ListOrderTransactions(ctx context.Context, orderID string) ([]*PaymentTransaction, error)

func (*Module) ListProvidersStep

func (m *Module) ListProvidersStep(sCtx mdk.StepContext) mdk.StepResult

func (*Module) ListTransactionsStep

func (m *Module) ListTransactionsStep(sCtx mdk.StepContext) mdk.StepResult

func (*Module) Models

func (m *Module) Models() []any

func (*Module) Mutations

func (m *Module) Mutations() map[string]any

func (*Module) Queries

func (m *Module) Queries() map[string]any

func (*Module) Routes

func (m *Module) Routes() []mdk.Route

func (*Module) Shutdown

func (m *Module) Shutdown(ctx context.Context) error

func (*Module) VerifyPayment

func (m *Module) VerifyPayment(ctx context.Context, orderID string, provider string, payload map[string]any) (*PaymentVerificationResult, error)

func (*Module) VerifyPaymentAp2

func (m *Module) VerifyPaymentAp2(ctx context.Context, input AP2VerificationInput) (*PaymentVerificationResult, error)

type PaymentIntent

type PaymentIntent struct {
	ProviderTransactionID string            `json:"provider_transaction_id"`
	ClientSecret          string            `json:"client_secret,omitempty"` // For front-end SDKs
	Amount                float64           `json:"amount"`
	Currency              string            `json:"currency"`
	Status                string            `json:"status"`
	Metadata              map[string]string `json:"metadata,omitempty"`
}

PaymentIntent holds the response payload from the payment gateway setup phase.

type PaymentProvider

type PaymentProvider interface {
	ID() string

	// CreateIntent registers a payment with the provider.
	CreateIntent(ctx context.Context, orderID string, amount float64, currency string) (*PaymentIntent, error)

	// VerifyPayment confirms signatures or tokens from a checkout payload.
	VerifyPayment(ctx context.Context, payload map[string]any) (bool, error)

	// Refund processes a refund for a successful payment.
	Refund(ctx context.Context, transactionID string, amount float64) (string, error)
}

PaymentProvider defines the operations that each payment integration must implement.

func GetProvider

func GetProvider(id string) (PaymentProvider, bool)

GetProvider retrieves a payment provider by its identifier.

type PaymentProviderInfo

type PaymentProviderInfo struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Enabled bool   `json:"enabled"`
}

type PaymentTransaction

type PaymentTransaction struct {
	ID                    string            `gorm:"primaryKey" json:"id"`
	OrderID               string            `gorm:"index" json:"order_id"`
	Provider              string            `json:"provider"` // "stripe", "razorpay", "mock"
	ProviderTransactionID string            `gorm:"index" json:"provider_transaction_id"`
	Amount                float64           `json:"amount"`
	Currency              string            `json:"currency"`
	Status                TransactionStatus `gorm:"not null" json:"status"`
	ClientSecret          string            `json:"client_secret,omitempty"` // Stripe client_secret or Razorpay order_id
	Metadata              string            `json:"metadata,omitempty"`      // JSON metadata string
	CreatedAt             time.Time         `json:"created_at"`
	UpdatedAt             time.Time         `json:"updated_at"`
	DeletedAt             gorm.DeletedAt    `gorm:"index" json:"-"`
}

PaymentTransaction tracks payment gateway state changes linked to orders.

type PaymentVerificationResult

type PaymentVerificationResult struct {
	OrderID string `json:"orderId"`
	Status  string `json:"status"`
	Message string `json:"message"`
}

type RazorpayProvider

type RazorpayProvider struct {
	BaseURL    string       // Custom BaseURL for testing
	HTTPClient *http.Client // Custom HTTP client for testing
	// contains filtered or unexported fields
}

func NewRazorpayProvider

func NewRazorpayProvider(keyID, keySecret string) *RazorpayProvider

func (*RazorpayProvider) CreateIntent

func (r *RazorpayProvider) CreateIntent(ctx context.Context, orderID string, amount float64, currency string) (*PaymentIntent, error)

func (*RazorpayProvider) ID

func (r *RazorpayProvider) ID() string

func (*RazorpayProvider) Refund

func (r *RazorpayProvider) Refund(ctx context.Context, transactionID string, amount float64) (string, error)

func (*RazorpayProvider) VerifyPayment

func (r *RazorpayProvider) VerifyPayment(ctx context.Context, payload map[string]any) (bool, error)

func (*RazorpayProvider) WriteToHash

func (r *RazorpayProvider) WriteToHash(h interface {
	Write([]byte) (int, error)
}, data string) (int, error)

type StripeProvider

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

func NewStripeProvider

func NewStripeProvider(secretKey string) *StripeProvider

func (*StripeProvider) CreateIntent

func (s *StripeProvider) CreateIntent(ctx context.Context, orderID string, amount float64, currency string) (*PaymentIntent, error)

func (*StripeProvider) ID

func (s *StripeProvider) ID() string

func (*StripeProvider) Refund

func (s *StripeProvider) Refund(ctx context.Context, transactionID string, amount float64) (string, error)

func (*StripeProvider) VerifyPayment

func (s *StripeProvider) VerifyPayment(ctx context.Context, payload map[string]any) (bool, error)

type TransactionStatus

type TransactionStatus string
const (
	StatusPending   TransactionStatus = "PENDING"
	StatusSucceeded TransactionStatus = "SUCCEEDED"
	StatusFailed    TransactionStatus = "FAILED"
	StatusRefunded  TransactionStatus = "REFUNDED"
)

type WebhookHandlers

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

func NewWebhookHandlers

func NewWebhookHandlers(db *gorm.DB, rt mdk.Runtime) *WebhookHandlers

func (*WebhookHandlers) HandleRazorpay

func (wh *WebhookHandlers) HandleRazorpay(w http.ResponseWriter, r *http.Request)

func (*WebhookHandlers) HandleStripe

func (wh *WebhookHandlers) HandleStripe(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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