Lesson 129 – Standardizing Escrow API Responses with Response Builders


Objective

Refactor the Escrow API client to eliminate duplicated response arrays by introducing reusable response builder methods.

Instead of manually constructing arrays in every public method, the client will centralize response creation through dedicated helper methods.


Why this refactoring?

After Lesson 128, every public method returns a structure similar to:

array(
    'success'      => true,
    'message'      => '',
    'data'         => array(),
    'reference'    => '',
    'status'       => '',
    'endpoint'     => '',
    'response_code'=> 200,
);

This structure is repeated throughout the class.

Although functional, it introduces unnecessary duplication and increases maintenance effort.


Goals

By the end of this lesson we will:

  • Remove duplicated response arrays.
  • Introduce reusable helper methods.
  • Standardize success responses.
  • Standardize error responses.
  • Improve readability.
  • Reduce future maintenance.

New helper methods

Introduce two new private methods.

success_response()

Responsible for constructing successful API responses.

Example responsibilities:

  • success flag
  • message
  • data
  • reference
  • status
  • endpoint
  • response code

error_response()

Responsible for constructing error responses.

Example responsibilities:

  • success = false
  • error message
  • endpoint
  • response code
  • optional error payload

Refactoring send_request()

Instead of manually returning arrays such as:

return array(
    'success' => false,
    ...
);

the method becomes

return $this->error_response(
    ...
);

Likewise,

return $this->success_response(
    ...
);

Benefits

This provides several advantages.

Single source of truth

Every response follows exactly the same structure.


Easier maintenance

If we later decide to add:

provider
timestamp
request_id
duration

only two helper methods require updating.


Cleaner code

Large repetitive array blocks disappear.

The public methods become easier to understand because they focus on business logic instead of formatting arrays.


Preparing for future lessons

Lesson 129 also prepares the client for future functionality.

Upcoming lessons will introduce:

  • Provider synchronization
  • Escrow transaction lifecycle
  • Webhook handling
  • Audit logging
  • Retry mechanisms

Having standardized response builders greatly simplifies those additions.


Expected outcome

After completing Lesson 129:

  • Every public API method returns standardized responses.
  • Response formatting exists in one place.
  • The Escrow client becomes smaller, cleaner, and easier to extend.

Implementation roadmap

We will:

  1. Create success_response().
  2. Create error_response().
  3. Refactor send_request().
  4. Refactor simulation responses.
  5. Refactor all public API methods.
  6. Verify backward compatibility.

I think this is a natural continuation of Lesson 128. Lesson 128 established how the client communicates with external services; Lesson 129 refines how those communications are represented internally, making the codebase more maintainable before moving on to real Escrow transaction payloads and synchronization.

Leave a Reply

Your email address will not be published. Required fields are marked *