|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + |
| 13 | + "github.com/rabbitprincess/x402-facilitator/types" |
| 14 | +) |
| 15 | + |
| 16 | +type Client struct { |
| 17 | + BaseURL *url.URL |
| 18 | + HTTPClient *http.Client |
| 19 | + CreateAuthHeader func() (map[string]map[string]string, error) |
| 20 | +} |
| 21 | + |
| 22 | +func NewClient(baseURL string) (*Client, error) { |
| 23 | + parsed, err := url.Parse(baseURL) |
| 24 | + if err != nil { |
| 25 | + return nil, fmt.Errorf("invalid base URL: %w", err) |
| 26 | + } |
| 27 | + return &Client{ |
| 28 | + BaseURL: parsed, |
| 29 | + HTTPClient: http.DefaultClient, |
| 30 | + }, nil |
| 31 | +} |
| 32 | + |
| 33 | +// Supported fetches the list of supported schemes. |
| 34 | +func (c *Client) Supported(ctx context.Context) ([]types.SupportedKind, error) { |
| 35 | + var result []types.SupportedKind |
| 36 | + if err := c.doRequest(ctx, http.MethodGet, "/supported", nil, "", &result); err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + return result, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (c *Client) Verify(ctx context.Context, payload *types.PaymentPayload, req *types.PaymentRequirements) (*types.PaymentVerifyResponse, error) { |
| 43 | + payloadJson, err := json.Marshal(payload) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("marshal payment payload: %w", err) |
| 46 | + } |
| 47 | + header := base64.StdEncoding.EncodeToString(payloadJson) |
| 48 | + |
| 49 | + body := types.PaymentVerifyRequest{ |
| 50 | + X402Version: int(types.X402VersionV1), |
| 51 | + PaymentHeader: header, |
| 52 | + PaymentRequirements: *req, |
| 53 | + } |
| 54 | + |
| 55 | + var resp types.PaymentVerifyResponse |
| 56 | + if err := c.doRequest(ctx, http.MethodPost, "/verify", body, "verify", &resp); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + return &resp, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Settle sends a payment settlement request. |
| 63 | +func (c *Client) Settle(ctx context.Context, payload *types.PaymentPayload, req *types.PaymentRequirements) (*types.PaymentSettleResponse, error) { |
| 64 | + payloadJson, err := json.Marshal(payload) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("marshal payment payload: %w", err) |
| 67 | + } |
| 68 | + header := base64.StdEncoding.EncodeToString(payloadJson) |
| 69 | + |
| 70 | + body := types.PaymentSettleRequest{ |
| 71 | + X402Version: int(types.X402VersionV1), |
| 72 | + PaymentHeader: header, |
| 73 | + PaymentRequirements: *req, |
| 74 | + } |
| 75 | + |
| 76 | + var resp types.PaymentSettleResponse |
| 77 | + if err := c.doRequest(ctx, http.MethodPost, "/settle", body, "settle", &resp); err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + return &resp, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (c *Client) doRequest(ctx context.Context, method, path string, body any, authKey string, out any) error { |
| 84 | + // Build URL |
| 85 | + u := c.BaseURL.ResolveReference(&url.URL{Path: path}) |
| 86 | + |
| 87 | + // Prepare body |
| 88 | + var reader io.Reader |
| 89 | + if body != nil { |
| 90 | + payload, err := json.Marshal(body) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("marshal request body: %w", err) |
| 93 | + } |
| 94 | + reader = bytes.NewReader(payload) |
| 95 | + } |
| 96 | + |
| 97 | + // Create HTTP request |
| 98 | + req, err := http.NewRequestWithContext(ctx, method, u.String(), reader) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if body != nil { |
| 103 | + req.Header.Set("Content-Type", "application/json") |
| 104 | + } |
| 105 | + |
| 106 | + if authKey != "" && c.CreateAuthHeader != nil { |
| 107 | + hdrs, err := c.CreateAuthHeader() |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("create auth headers: %w", err) |
| 110 | + } |
| 111 | + if section, ok := hdrs[authKey]; ok { |
| 112 | + for k, v := range section { |
| 113 | + req.Header.Set(k, v) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Execute |
| 119 | + resp, err := c.HTTPClient.Do(req) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + defer resp.Body.Close() |
| 124 | + |
| 125 | + if resp.StatusCode != http.StatusOK { |
| 126 | + data, _ := io.ReadAll(resp.Body) |
| 127 | + return fmt.Errorf("%s %s failed: status %d, body: %s", method, path, resp.StatusCode, string(data)) |
| 128 | + } |
| 129 | + |
| 130 | + if out != nil { |
| 131 | + if err := json.NewDecoder(resp.Body).Decode(out); err != nil { |
| 132 | + return fmt.Errorf("decode %s response: %w", path, err) |
| 133 | + } |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
0 commit comments