The Wallet Transaction System is a Ruby on Rails API application designed to handle wallet-based financial transactions. It provides endpoints for managing wallets, creating different types of transactions (credit, debit, and transfer), and ensuring secure access through JWT authentication.
- JWT Authentication: Secure authentication with JSON Web Tokens (JWT).
- Polymorphic Wallet Model: Each user can have a wallet with a polymorphic association to allow for flexibility.
- Transaction Types: Supports multiple transaction types such as credit, debit, and transfer.
- Balance Calculation: Calculates wallet balance based on transactions.
- Validation: Ensures transactions are correctly validated based on type (e.g., debit transactions ensure sufficient funds).
-
Clone the Repository:
git clone <repository-url> cd wallet_transaction_system
-
Install Dependencies: Make sure you have Ruby and Bundler installed, then run:
bundle install
-
Database Setup: Set up the database by running the migrations:
rails db:create rails db:migrate
-
Environment Variables:
- Set the
SECRET_KEY_BASE
for JWT encoding/decoding. You can generate one with:rails secret
- Add this key to your environment variables, e.g., in
.env
:SECRET_KEY_BASE=<your-secret-key>
- Set the
-
Run the Server: Start the Rails server:
rails server
-
Register:
POST /register
- Parameters:
username
,password
- Returns: User information upon successful registration.
- Parameters:
-
Login:
POST /sessions
- Parameters:
username
,password
- Returns: JWT token on successful login.
- Parameters:
-
List Transactions:
GET /wallet_transactions
- Requires JWT token.
- Returns: List of all transactions for the authenticated user’s wallet.
-
Create Transaction:
POST /wallet_transactions
- Parameters:
amount
: Decimaltransaction_type
: String (one ofcredit
,debit
,transfer
)target_wallet_id
(for transfers)
- Requires JWT token.
- Returns: Success or error message based on transaction validation.
- Parameters:
-
List Available Stocks:
GET /stocks
- Requires JWT token.
- Returns: List of available stocks.
-
Purchase Stock:
POST /stocks/purchase
- Parameters:
stock_id
: ID of the stockquantity
: Number of shares to purchase
- Requires JWT token.
- Returns: Confirmation of purchase or error message if validation fails.
- Parameters:
- Fields:
username
: Unique identifier for the user.password_digest
: Encrypted password (handled viahas_secure_password
).
- Polymorphic association with User.
- Methods:
transaction_balance
: Returns the total balance based on transactions.
- Fields:
amount
: Decimaltransaction_type
: Enum (credit
,debit
,transfer
)
- Validations:
- Ensures
amount
is non-zero. - Custom validations for positive/negative amounts based on
transaction_type
.
- Ensures
- CreditTransaction: Ensures
amount
is positive. - DebitTransaction: Ensures
amount
is negative and user has sufficient balance. - TransferTransaction: Checks for valid target wallet and balance.
-
Run All Tests:
rails test
-
Controller Tests: Ensure the API endpoints work as expected.
-
Model Tests: Test validations, associations, and custom logic.
- Register a user to create an account.
- Authenticate to obtain a JWT token.
- Use the JWT token in the
Authorization
header for all subsequent requests. - Create transactions by specifying an
amount
andtransaction_type
. For transfer transactions, specify thetarget_wallet_id
.
This project is open-source under the MIT License.