Skip to content

Payments Controller

Extensive Documentation: Payment Controller and PaymentQueryHandler

Overview

The Payment Controller is a central component in managing payment-related activities in the application. It includes creating, updating, retrieving, and deleting payment records. A crucial aspect of this controller is its ability to handle complex queries for payment retrieval, facilitated by the PaymentQueryHandler.

Functions in Payment Controller

  1. getAllPayments: Retrieves a paginated list of payments with options for filtering and sorting. It leverages PaymentQueryHandler to process the query parameters.

    • Pagination: Limits the number of payments displayed per page and enables navigation across different pages.
    • Filters: Allows filtering payments by status (pending, complete, failed), recurrence, currency (USD, XCD), and amount ranges.
    • Sorting: Enables sorting the payments by due dates or payment dates in either ascending or descending order.
  2. createUserPayment: Creates a new payment record for a specific user. Before creation, the input data is validated against newPaymentSchema to ensure data integrity.

  3. getUserPayments: Fetches payments associated with a specific user, incorporating query handling for pagination, filtering, and sorting.

  4. findPayment: Retrieves details of a single payment using its unique identifier (paymentId).

  5. updatePaymentAfterCheckout: Specifically designed for post-checkout processes, this function updates payment details like transaction ID and billing information.

  6. updatePayment: Allows updating an existing payment’s details. It utilizes editPaymentSchema for input validation.

  7. deletePayment: Removes a payment record based on its unique identifier.

PaymentQueryHandler Class

  • Purpose: PaymentQueryHandler is a utility class that handles the complexity of building queries for fetching payments. It encapsulates logic for filtering and sorting, making the getAllPayments and getUserPayments functions more manageable and readable.

  • Structure:

    class PaymentQueryHandler {
    constructor(queryParams) {
    this.queryParams = queryParams;
    this.query = {};
    this.sort = {};
    }
    // Methods for filtering and sorting...
    buildQuery() {
    // Calls each filtering method and assembles the final query
    return this;
    }
    }
  • Methods:

    • filterByStatus: Filters payments by their status (pending, complete, etc.).
    • filterByIsRecurring: Filters payments based on whether they are recurring.
    • filterByCurrency: Filters payments by currency.
    • filterByAmountRange: Filters payments within specified amount ranges.
    • sortByDate: Sorts payments based on date fields.
    • buildQuery: Calls individual filter methods to construct the final query object.

Example Usage:

const queryHandler = new PaymentQueryHandler(filters).buildQuery();
const payments = await PaymentModel.find(queryHandler.query)
.populate({
path: "user_id",
select: "first_name last_name email company companyAddress phone -_id",
})
.sort(queryHandler.sort)
.limit(limit)
.skip(offset);

In this example, PaymentQueryHandler processes the query parameters and builds a MongoDB query. The Payment Controller then uses this query to fetch the relevant payments from the database.

Importance:

  • Modularity and Reusability: By encapsulating query logic within PaymentQueryHandler, the controller functions remain clean and focused on their primary responsibilities. This separation of concerns enhances code readability and maintenance.
  • Flexibility: The Payment Controller can handle a wide range of user queries, making it versatile for different user needs.
  • Scalability: The use of pagination and efficient querying mechanisms ensures that the system can handle large datasets effectively.

Overall, the Payment Controller and PaymentQueryHandler work together to provide a robust, efficient, and user-friendly system for managing payment transactions within the application.