/**
* @file client_hooks.go
* @author nich.xbt
* @copyright (c) 2026 nirholas
* @license MIT
* @repository universal-crypto-mcp
* @version 0.14.9.3
* @checksum 1493814938
*/
package x402
import (
"context"
)
// ============================================================================
// Client Hook Context Types
// ============================================================================
// PaymentCreationContext contains information passed to payment creation hooks
// Uses view interfaces for version-agnostic hooks
type PaymentCreationContext struct {
Ctx context.Context
Version int // V1 or V2
SelectedRequirements PaymentRequirementsView
}
// PaymentCreatedContext contains payment creation result and context
type PaymentCreatedContext struct {
PaymentCreationContext
Payload PaymentPayloadView
}
// PaymentCreationFailureContext contains payment creation failure and context
type PaymentCreationFailureContext struct {
PaymentCreationContext
Error error
}
// ============================================================================
// Client Hook Result Types
// ============================================================================
// BeforePaymentCreationHookResult represents the result of a "before payment creation" hook
// If Abort is true, the payment creation will be aborted with the given Reason
type BeforePaymentCreationHookResult struct {
Abort bool
Reason string
}
// PaymentCreationFailureHookResult represents the result of a payment creation failure hook
// If Recovered is true, the hook has recovered from the failure with the given payload
type PaymentCreationFailureHookResult struct {
Recovered bool
Payload PaymentPayloadView
}
// ============================================================================
// Client Hook Function Types
// ============================================================================
// BeforePaymentCreationHook is called before payment payload creation
// If it returns a result with Abort=true, payment creation will be aborted
// and an error will be returned with the provided reason
type BeforePaymentCreationHook func(PaymentCreationContext) (*BeforePaymentCreationHookResult, error)
// AfterPaymentCreationHook is called after successful payment payload creation
// Any error returned will be logged but will not affect the payment creation result
type AfterPaymentCreationHook func(PaymentCreatedContext) error
// OnPaymentCreationFailureHook is called when payment payload creation fails
// If it returns a result with Recovered=true, the provided PaymentPayload
// will be returned instead of the error
type OnPaymentCreationFailureHook func(PaymentCreationFailureContext) (*PaymentCreationFailureHookResult, error)
// ============================================================================
// Client Hook Registration Options
// ============================================================================
// WithBeforePaymentCreationHook registers a hook to execute before payment creation
func WithBeforePaymentCreationHook(hook BeforePaymentCreationHook) ClientOption {
return func(c *x402Client) {
c.beforePaymentCreationHooks = append(c.beforePaymentCreationHooks, hook)
}
}
// WithAfterPaymentCreationHook registers a hook to execute after successful payment creation
func WithAfterPaymentCreationHook(hook AfterPaymentCreationHook) ClientOption {
return func(c *x402Client) {
c.afterPaymentCreationHooks = append(c.afterPaymentCreationHooks, hook)
}
}
// WithOnPaymentCreationFailureHook registers a hook to execute when payment creation fails
func WithOnPaymentCreationFailureHook(hook OnPaymentCreationFailureHook) ClientOption {
return func(c *x402Client) {
c.onPaymentCreationFailureHooks = append(c.onPaymentCreationFailureHooks, hook)
}
}
/* EOF - n1ch0las | 0x6E696368 */