Skip to content

Commit be646dc

Browse files
committed
✅ feat: enhance payment processing with agent ID handling
- Updated `PaymentEngine` to retrieve and handle agent IDs dynamically, ensuring proper feedback posting for successful settlements. - Added `DemoMode` option to `SettlerConfig` for configuration flexibility during testing. - Refactored agent ID resolution in middleware for improved robustness by using dynamic byte conversion from the recovered address instead of static placeholders.
1 parent 5168e28 commit be646dc

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

internal/service/payment_engine.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"context"
55
"fmt"
6+
"math/big"
67
"time"
78

89
"github.com/google/uuid"
@@ -31,13 +32,14 @@ func (e *PaymentEngine) CloseSettlementLoop(ctx context.Context, invoiceID strin
3132
return err
3233
}
3334

34-
if success && e.registry != nil {
35-
// In a real scenario, we would retrieve the AgentID associated with the invoice.
36-
// For now, we'll simulate posting a positive feedback if it was an agentic payment.
35+
if success && e.registry != nil && invoice.AgentID != "" {
3736
fmt.Printf("🏆 ERC-8004: Closing settlement loop for invoice %s. Posting reputation update...\n", invoiceID)
3837

39-
// Placeholder: Assume agent ID 42
40-
agentID := big.NewInt(42)
38+
agentID, ok := new(big.Int).SetString(invoice.AgentID, 10)
39+
if !ok {
40+
// Fallback to byte conversion if string conversion fails
41+
agentID = new(big.Int).SetBytes([]byte(invoice.AgentID))
42+
}
4143
score := big.NewInt(1) // +1 point for successful settlement
4244
_ = e.registry.PostFeedback(ctx, agentID, score, []string{"payment", "settled"}, "")
4345
}

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type SettlerConfig struct {
1414
AgentID string `json:"agent_id"`
1515
CasperFacilitatorURL string `json:"casper_facilitator_url"`
1616
CasperFacilitatorToken string `json:"casper_facilitator_token"`
17+
DemoMode bool `json:"demo_mode"`
1718
}
1819

1920
func GetConfigPath() (string, error) {

pkg/x402/middleware.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,8 @@ func (m *Middleware) Handler(next http.Handler) http.Handler {
138138
if err == nil && recovered.Hex() != "" {
139139
// 4a. ERC-8004 Trust Checks
140140
if m.config.Registry != nil {
141-
// For this demo, we assume the agent ID is passed or mapped from the recovered address.
142-
// In reality, we'd lookup the tokenId owned by 'recovered'.
143-
// Placeholder: assume tokenId 42
144-
agentID := big.NewInt(42)
141+
// Resolve agentID dynamically from recovered address bytes
142+
agentID := new(big.Int).SetBytes(recovered.Bytes())
145143

146144
rep, err := m.config.Registry.GetReputation(r.Context(), agentID)
147145
if err == nil && m.config.MinReputation != nil {

0 commit comments

Comments
 (0)