-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.go
More file actions
70 lines (65 loc) · 2.61 KB
/
method.go
File metadata and controls
70 lines (65 loc) · 2.61 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
package mppx
import "context"
// ServerMethod is a payment method that verifies credentials on the server side.
// Implement this interface to add a custom payment method to your server.
//
// Example usage:
//
// type MyChargeMethod struct {
// recipient string
// currency string
// }
//
// func (m *MyChargeMethod) Name() string { return "mymethod" }
// func (m *MyChargeMethod) Intent() string { return "charge" }
// func (m *MyChargeMethod) DefaultRequest() map[string]any {
// return map[string]any{"currency": m.currency, "recipient": m.recipient}
// }
// func (m *MyChargeMethod) Verify(ctx context.Context, cred Credential, req map[string]any) (Receipt, error) {
// // verify payment on-chain, via API, etc.
// return NewReceipt(ReceiptParams{Method: m.Name(), Reference: txHash}), nil
// }
type ServerMethod interface {
// Name returns the payment method identifier (e.g. "tempo", "stripe").
Name() string
// Intent returns the intent type (e.g. "charge", "session").
Intent() string
// DefaultRequest returns default request parameters merged with call-site options.
// Return nil if no defaults.
DefaultRequest() map[string]any
// Verify verifies a payment credential and returns a Receipt.
// Return a *PaymentError for payment-specific failures (will be serialized to RFC 9457).
// Return any other error for internal failures (wrapped in VerificationFailedError).
Verify(ctx context.Context, cred Credential, req map[string]any) (Receipt, error)
}
// ClientMethod is a payment method that creates credentials on the client side.
// Implement this interface to add a custom payment method to your client.
type ClientMethod interface {
// Name returns the payment method identifier.
Name() string
// Intent returns the intent type.
Intent() string
// CreateCredential creates a payment credential from a challenge.
// Returns the serialized credential string suitable for an Authorization header.
CreateCredential(ctx context.Context, challenge Challenge) (string, error)
}
// MethodKey returns the canonical "name/intent" key for a ServerMethod.
func MethodKey(m ServerMethod) string {
return m.Name() + "/" + m.Intent()
}
// MergeRequest merges method defaults with call-site request parameters.
// Call-site values take precedence over defaults.
func MergeRequest(method ServerMethod, callSiteRequest map[string]any) map[string]any {
defaults := method.DefaultRequest()
if len(defaults) == 0 {
return callSiteRequest
}
merged := make(map[string]any, len(defaults)+len(callSiteRequest))
for k, v := range defaults {
merged[k] = v
}
for k, v := range callSiteRequest {
merged[k] = v
}
return merged
}