forked from coinbase/x402
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_hooks.go
More file actions
158 lines (132 loc) · 5.89 KB
/
server_hooks.go
File metadata and controls
158 lines (132 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package x402
import (
"context"
)
// ============================================================================
// Resource Server Hook Context Types
// ============================================================================
// VerifyContext contains information passed to verify hooks
// Uses view interfaces for version-agnostic hooks
// PayloadBytes and RequirementsBytes provide escape hatch for extensions (e.g., Bazaar)
type VerifyContext struct {
Ctx context.Context
Payload PaymentPayloadView
Requirements PaymentRequirementsView
PayloadBytes []byte // Raw bytes for extensions needing full data
RequirementsBytes []byte // Raw bytes for extensions needing full data
}
// VerifyResultContext contains verify operation result and context
type VerifyResultContext struct {
VerifyContext
Result *VerifyResponse
}
// VerifyFailureContext contains verify operation failure and context
type VerifyFailureContext struct {
VerifyContext
Error error
}
// SettleContext contains information passed to settle hooks
// Uses view interfaces for version-agnostic hooks
// PayloadBytes and RequirementsBytes provide escape hatch for extensions (e.g., Bazaar)
type SettleContext struct {
Ctx context.Context
Payload PaymentPayloadView
Requirements PaymentRequirementsView
PayloadBytes []byte // Raw bytes for extensions needing full data
RequirementsBytes []byte // Raw bytes for extensions needing full data
}
// SettleResultContext contains settle operation result and context
type SettleResultContext struct {
SettleContext
Result *SettleResponse
}
// SettleFailureContext contains settle operation failure and context
type SettleFailureContext struct {
SettleContext
Error error
}
// ============================================================================
// Resource Server Hook Result Types
// ============================================================================
// BeforeHookResult represents the result of a "before" hook
// If Abort is true, the operation will be aborted with the given Reason
type BeforeHookResult struct {
Abort bool
Reason string
Message string
}
// VerifyFailureHookResult represents the result of a verify failure hook
// If Recovered is true, the hook has recovered from the failure with the given result
type VerifyFailureHookResult struct {
Recovered bool
Result *VerifyResponse
}
// SettleFailureHookResult represents the result of a settle failure hook
type SettleFailureHookResult struct {
Recovered bool
Result *SettleResponse
}
// ============================================================================
// Resource Server Hook Function Types
// ============================================================================
// BeforeVerifyHook is called before payment verification
// If it returns a result with Abort=true, verification will be skipped
// and an invalid VerifyResponse will be returned with the provided reason
type BeforeVerifyHook func(VerifyContext) (*BeforeHookResult, error)
// AfterVerifyHook is called after successful payment verification
// Any error returned will be logged but will not affect the verification result
type AfterVerifyHook func(VerifyResultContext) error
// OnVerifyFailureHook is called when payment verification fails
// If it returns a result with Recovered=true, the provided VerifyResponse
// will be returned instead of the error
type OnVerifyFailureHook func(VerifyFailureContext) (*VerifyFailureHookResult, error)
// BeforeSettleHook is called before payment settlement
// If it returns a result with Abort=true, settlement will be aborted
// and an error will be returned with the provided reason
type BeforeSettleHook func(SettleContext) (*BeforeHookResult, error)
// AfterSettleHook is called after successful payment settlement
// Any error returned will be logged but will not affect the settlement result
type AfterSettleHook func(SettleResultContext) error
// OnSettleFailureHook is called when payment settlement fails
// If it returns a result with Recovered=true, the provided SettleResponse
// will be returned instead of the error
type OnSettleFailureHook func(SettleFailureContext) (*SettleFailureHookResult, error)
// ============================================================================
// Resource Server Hook Registration Options
// ============================================================================
// WithBeforeVerifyHook registers a hook to execute before payment verification
func WithBeforeVerifyHook(hook BeforeVerifyHook) ResourceServerOption {
return func(s *x402ResourceServer) {
s.beforeVerifyHooks = append(s.beforeVerifyHooks, hook)
}
}
// WithAfterVerifyHook registers a hook to execute after successful payment verification
func WithAfterVerifyHook(hook AfterVerifyHook) ResourceServerOption {
return func(s *x402ResourceServer) {
s.afterVerifyHooks = append(s.afterVerifyHooks, hook)
}
}
// WithOnVerifyFailureHook registers a hook to execute when payment verification fails
func WithOnVerifyFailureHook(hook OnVerifyFailureHook) ResourceServerOption {
return func(s *x402ResourceServer) {
s.onVerifyFailureHooks = append(s.onVerifyFailureHooks, hook)
}
}
// WithBeforeSettleHook registers a hook to execute before payment settlement
func WithBeforeSettleHook(hook BeforeSettleHook) ResourceServerOption {
return func(s *x402ResourceServer) {
s.beforeSettleHooks = append(s.beforeSettleHooks, hook)
}
}
// WithAfterSettleHook registers a hook to execute after successful payment settlement
func WithAfterSettleHook(hook AfterSettleHook) ResourceServerOption {
return func(s *x402ResourceServer) {
s.afterSettleHooks = append(s.afterSettleHooks, hook)
}
}
// WithOnSettleFailureHook registers a hook to execute when payment settlement fails
func WithOnSettleFailureHook(hook OnSettleFailureHook) ResourceServerOption {
return func(s *x402ResourceServer) {
s.onSettleFailureHooks = append(s.onSettleFailureHooks, hook)
}
}