Skip to content

Add payment verification/settlement middleware to E2E test server #168

@holonbot

Description

@holonbot

Context

During review of PR #159, a reviewer pointed out that the test server endpoints (/api/protected, /api/extensions-echo) currently only return payment requirements but don't actually verify or settle payments.

Issue

The E2E test expects result.success and result.message from the server endpoints, but the server only returns payment requirements. There's missing middleware or handlers that should:

  1. Process payment headers from incoming requests
  2. Call the facilitator's verify method to validate payments
  3. Call the facilitator's settle method to execute settlements
  4. Return appropriate success/error responses

Proposed Solution

Implement proper payment verification and settlement in the test server:

  1. Add payment middleware to intercept requests with payment headers
  2. Integrate facilitator SDK to verify and settle payments
  3. Return proper responses based on verification/settlement results

Example Implementation Approach

// Add middleware to process payment headers
app.use('*', async (c, next) => {
  const paymentHeader = c.req.header('X-PAYMENT');
  if (paymentHeader) {
    // Verify payment
    const facilitator = createRouterSettlementFacilitator({
      signer: contracts.accounts.facilitator,
      allowedRouters: { "eip155:31337": [contracts.settlementRouter.address] },
      rpcUrls: { "eip155:31337": `http://localhost:${TEST_CONFIG.anvilPort}` },
      privateKey: FACILITATOR_PRIVATE_KEY
    });
    
    const result = await facilitator.verify(paymentPayload, paymentRequirements);
    if (!result.isValid) {
      return c.json({ success: false, error: result.invalidReason }, 402);
    }
    
    await facilitator.settle(paymentPayload, paymentRequirements);
    c.set('payment', result);
  }
  await next();
});

Impact

This would make the E2E tests more complete by actually verifying and settling payments rather than just returning payment requirements.

Related PR

Deferred from PR #159 comment #2652058521

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions