This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Ruby gem that provides a client library for the Midtrans Payment Gateway API. The gem wraps various Midtrans API endpoints (payment transactions, virtual accounts, disbursements, merchant management, etc.) with a clean Ruby interface.
rake spec # Run all tests
bundle exec rspec spec/path/to/file_spec.rb # Run specific test file
bundle exec rspec spec/path/to/file_spec.rb:LINE # Run specific test at linebin/console # IRB console with the gem loadedbundle exec rake install # Install gem locally
bundle exec rake release # Release new version (tags, pushes to rubygems)The gem follows a layered architecture pattern:
-
Client Layer (
lib/midtrans_api/client.rb)- Main entry point that users interact with
- Manages Faraday HTTP connection with Basic Auth
- Provides convenience methods that return API instances (e.g.,
client.gopay_charge,client.merchant) - Supports three API environments via configuration: production, sandbox, and partner API
- HTTP methods:
get(),post(),patch()
-
API Layer (
lib/midtrans_api/api/*)- Each API endpoint has its own class that inherits from
MidtransApi::Api::Base - Organized by feature domain (e.g.,
Merchant::Create,Gopay::Charge,Transaction::Expire) - API classes define the endpoint
PATHconstant and HTTP method (e.g.,post(),patch()) - Responsible for making HTTP requests via the client and returning Model instances
- Each API endpoint has its own class that inherits from
-
Model Layer (
lib/midtrans_api/model/*)- Each API response is mapped to a Model class that inherits from
MidtransApi::Model::Base - Models use
resource_attributesmacro to define accessible response fields - Base class handles initialization from API response hash via
assign_attributes - Models implement
resolve_params_attr()for key transformation (typically identity transform)
- Each API response is mapped to a Model class that inherits from
-
Configuration (
lib/midtrans_api/configure.rb)- Manages API credentials (client_key, server_key)
- Environment selection (sandbox vs production vs partner API)
- Optional features: notification URL override, logging, timeout settings
To add a new Midtrans API endpoint:
-
Create API class at
lib/midtrans_api/api/{domain}/{action}.rb:class MyApi < MidtransApi::Api::Base PATH = 'endpoint/path' def post(params, additional_headers_params) response = client.post(PATH, params, { 'X-CUSTOM-HEADER': additional_headers_params }) MidtransApi::Model::Domain::MyApi.new(response) end end
-
Create Model class at
lib/midtrans_api/model/{domain}/{action}.rb:class MyApi < MidtransApi::Model::Base resource_attributes :field1, :field2, :field3 def resolve_params_attr(attr) attr.to_s # or custom transformation end end
-
Register in Client (
lib/midtrans_api/client.rb):- Add
requirestatements at the top for both API and Model files - Add convenience method to instantiate the API class:
def my_api @my_api ||= MidtransApi::Api::Domain::MyApi.new(self) end
- If using new HTTP verb, add method (e.g.,
put(),delete())
- Add
-
Create test file at
spec/lib/midtrans_api/api/{domain}/{action}_spec.rb:- Use WebMock to stub HTTP requests
- Test that correct model instance is returned
- Test that proper headers are sent
- Follow pattern from existing specs (e.g.,
merchant/create_spec.rb)
- Custom errors defined in
lib/midtrans_api/errors.rb - Middleware
HandleResponseExceptionintercepts HTTP errors and raises appropriate exceptions - See Midtrans API documentation for status codes
- Uses Faraday middleware for logging
FaradayLogFormatterprovides custom log formattingJsonMaskerandUrlMaskerutilities mask sensitive data in logs- Configure via
filtered_logsandmask_paramsclient options