Home Projects Portfolio Dashboard Export PDF Log in
C# .NET MySQL

Implementing Registration and Payment Flows in SistemaClubDeportivo2

Introduction

In the development of SistemaClubDeportivo2, a management platform for athletic organizations, we recently tackled the implementation of a streamlined activity registration and payment process. Handling user enrollment alongside financial transactions requires a robust, atomic approach to ensure data integrity.

The Problem

Previously, managing registrations often involved disconnected steps. A user might successfully sign up for an activity, but if the payment process failed or experienced latency, the system state would become inconsistent. We needed to ensure that the registration record and the payment status were handled as a single, reliable transaction to prevent orphaned registrations or unpaid access.

The Solution: Transactional Logic

To resolve this, we implemented a service layer that wraps the enrollment and payment steps within a database transaction. Using .NET and MySQL, we ensure that both the record insertion and the financial state update succeed or fail together.

public async Task ProcessEnrollment(int userId, int activityId, decimal amount)
{
    using var transaction = await _dbContext.Database.BeginTransactionAsync();
    try
    {
        var registration = new ActivityRegistration { UserId = userId, ActivityId = activityId };
        _dbContext.Registrations.Add(registration);

        var payment = new Payment { UserId = userId, Amount = amount, Status = "Paid" };
        _dbContext.Payments.Add(payment);

        await _dbContext.SaveChangesAsync();
        await transaction.CommitAsync();
    }
    catch
    {
        await transaction.RollbackAsync();
        throw;
    }
}

This pattern acts like a "two-key safe deposit box." Both keys (the registration and the payment) must be turned simultaneously for the safe to open. If one action fails, the transaction is rolled back, and the database remains in its original state.

Results

By centralizing this logic, we achieved:

  1. Consistency: No more registrations without confirmed payments.
  2. Reliability: Failures in the payment gateway do not leave incomplete registration data in the database.
  3. Maintainability: The logic is encapsulated in a single service method rather than scattered across controllers.

Key Insight

Always treat financial state changes and associated user actions as atomic units. When building systems where money changes hands, the "all or nothing" approach is the best way to safeguard your data integrity and provide a predictable user experience.


Generated with Gitvlg.com

Implementing Registration and Payment Flows in SistemaClubDeportivo2
Daniela Homobono

Daniela Homobono

Author

Share: