Zai Payment Ruby Library
A lightweight and extensible Ruby client for the Zai (AssemblyPay) API โ starting with secure OAuth2 authentication, and ready for Payments, Virtual Accounts, Webhooks, and more.
โจ Features
- ๐ OAuth2 Authentication - Client Credentials flow with automatic token management
- ๐ง Smart Token Caching - Auto-refresh before expiration, thread-safe storage
- ๐ฅ User Management - Create and manage payin (buyers) & payout (sellers) users
- ๐ฆ Item Management - Full CRUD for transactions/payments between buyers and sellers
- ๐ฆ Bank Account Management - Complete CRUD + validation for AU/UK bank accounts
- ๐ณ BPay Account Management - Manage BPay accounts for Australian bill payments
- ๐ผ Wallet Account Management - Show wallet accounts, check balances, and pay bills via BPay
- ๐ฆ Virtual Accounts - Complete virtual account management with PayTo and BPay support
- ๐ณ PayID Management - Create and manage PayIDs for Australian NPP payments
- ๐ซ Token Auth - Generate secure tokens for bank and card account data collection
- ๐ช Webhooks - Full CRUD + secure signature verification (HMAC SHA256)
- ๐งช Batch Transactions - Prelive-only endpoints for testing batch transaction flows
- โ๏ธ Environment-Aware - Seamless Pre-live / Production switching
- ๐งฑ Modular & Extensible - Clean resource-based architecture
- ๐งฐ Zero Heavy Dependencies - Lightweight, fast, and reliable
- ๐ฆ Production Ready - 97%+ test coverage, RuboCop compliant
๐งญ Installation
Add this line to your Gemfile:
gem 'zai_payment'Then install
bundle installโ๏ธ Configuration
# config/initializers/zai_payment.rb
ZaiPayment.configure do |c|
c.environment = Rails.env.production? ? :production : :prelive
c.client_id = ENV.fetch("ZAI_CLIENT_ID")
c.client_secret = ENV.fetch("ZAI_CLIENT_SECRET")
c.scope = ENV.fetch("ZAI_OAUTH_SCOPE")
# Optional: Configure timeout settings (defaults shown)
c.timeout = 30 # General request timeout in seconds
c.open_timeout = 10 # Connection open timeout in seconds
c.read_timeout = 30 # Read timeout in seconds
end๐ Quick Start
Authentication
Get an OAuth2 token with automatic caching and refresh:
# Simple one-liner (recommended)
token = ZaiPayment.token
# Or with full control (advanced)
config = ZaiPayment::Config.new
config.environment = :prelive
config.client_id = 'your_client_id'
config.client_secret = 'your_client_secret'
config.scope = 'your_scope'
token_provider = ZaiPayment::Auth::TokenProvider.new(config: config)
token = token_provider.bearer_tokenThe gem handles OAuth2 Client Credentials flow automatically - tokens are cached and refreshed before expiration.
๐ Complete Authentication Guide - Two approaches, examples, and best practices
Users
Manage payin (buyer) and payout (seller/merchant) users.
๐ Documentation:
- ๐ User Management Guide - Complete guide for payin and payout users
- ๐ก User Examples - Real-world usage patterns and Rails integration
- ๐ Zai: Onboarding a Payin User
- ๐ Zai: Onboarding a Payout User
Items
Manage transactions/payments between buyers and sellers.
๐ Documentation:
- ๐ Item Management Guide - Complete guide for creating and managing items
- ๐ก Item Examples - Real-world usage patterns and complete workflows
- ๐ Zai: Items API Reference
Bank Accounts
Manage bank accounts for Australian and UK users, with routing number validation.
๐ Documentation:
- ๐ Bank Account Management Guide - Complete guide for bank accounts
- ๐ก Bank Account Examples - Real-world patterns and integration
- ๐ Zai: Bank Accounts API Reference
BPay Accounts
Manage BPay accounts for Australian bill payments.
๐ Documentation:
- ๐ BPay Account Management Guide - Complete guide for BPay accounts
- ๐ก BPay Account Examples - Real-world patterns and bill payment workflows
- ๐ Zai: BPay Accounts API Reference
Wallet Accounts
Manage wallet accounts, check balances, and pay bills via BPay.
๐ Documentation:
- ๐ Wallet Account Management Guide - Complete guide for wallet accounts
- ๐ก Wallet Account Examples - Real-world patterns and payment workflows
- ๐ Zai: Wallet Accounts API Reference
Quick Example:
wallet_accounts = ZaiPayment::Resources::WalletAccount.new
# Check wallet balance
response = wallet_accounts.show('wallet_account_id')
balance = response.data['balance'] # in cents
puts "Balance: $#{balance / 100.0}"
# Pay a bill from wallet to BPay account
payment_response = wallet_accounts.pay_bill(
'wallet_account_id',
account_id: 'bpay_account_id',
amount: 17300, # $173.00 in cents
reference_id: 'bill_nov_2024'
)
if payment_response.success?
disbursement = payment_response.data
puts "Payment successful: #{disbursement['id']}"
puts "State: #{disbursement['state']}"
endVirtual Accounts
Manage virtual accounts with support for AKA names and Confirmation of Payee (CoP) lookups.
๐ Documentation:
- ๐ Virtual Account Management Guide - Complete guide for virtual accounts
- ๐ก Virtual Account Examples - Real-world patterns and workflows
- ๐ Zai: Virtual Accounts API Reference
PayID
Register and manage PayIDs (EMAIL type) for Australian NPP (New Payments Platform) payments.
๐ Documentation:
- ๐ PayID Management Guide - Complete guide for PayID registration
- ๐ก PayID Examples - Real-world patterns and workflows
Token Auth
Generate secure tokens for collecting bank and card account information.
๐ Documentation:
- ๐ก Token Auth Examples - Complete integration guide with PromisePay.js
- ๐ Zai: Generate Token API Reference
Webhooks
Manage webhook endpoints with secure signature verification.
๐ Documentation:
- ๐ Webhook Examples & Complete Guide - Full CRUD operations and patterns
- ๐ Security Quick Start - 5-minute webhook security setup
- ๐๏ธ Architecture & Implementation - Detailed technical documentation
- ๐ Signature Verification Details - Security implementation specs
Batch Transactions (Prelive Only)
Simulate batch transaction processing for testing in the prelive environment.
๐ Documentation:
- ๐ Batch Transaction Guide - Complete guide and method reference
- ๐ก Batch Transaction Examples - Testing workflows and webhook simulation
- โ ๏ธ Note: These endpoints are only available in prelive environment
Quick Example:
# Export pending transactions to batched state
export_response = ZaiPayment.batch_transactions.export_transactions
batch_id = export_response.data.first['batch_id']
transaction_ids = export_response.data.map { |t| t['id'] }
# Move to bank_processing state
ZaiPayment.batch_transactions.process_to_bank_processing(
batch_id,
exported_ids: transaction_ids
)
# Complete processing (triggers webhooks)
ZaiPayment.batch_transactions.process_to_successful(
batch_id,
exported_ids: transaction_ids
)Error Handling
The gem provides specific error classes for different scenarios:
begin
response = ZaiPayment.webhooks.create(
url: 'https://example.com/webhook',
object_type: 'transactions'
)
rescue ZaiPayment::Errors::ValidationError => e
# Handle validation errors (400, 422)
puts "Validation error: #{e.message}"
rescue ZaiPayment::Errors::UnauthorizedError => e
# Handle authentication errors (401)
puts "Authentication failed: #{e.message}"
rescue ZaiPayment::Errors::NotFoundError => e
# Handle not found errors (404)
puts "Resource not found: #{e.message}"
rescue ZaiPayment::Errors::ApiError => e
# Handle other API errors
puts "API error: #{e.message}"
end๐งฉ Roadmap
| Area | Description | Status |
|---|---|---|
| โ Authentication | OAuth2 Client Credentials flow | Done |
| โ Webhooks | CRUD for webhook endpoints | Done |
| โ Users | Manage PayIn / PayOut users | Done |
| โ Items | Transactions/payments (CRUD) | Done |
| โ Bank Accounts | AU/UK bank accounts + validation | Done |
| โ BPay Accounts | Manage BPay accounts | Done |
| โ Wallet Accounts | Show, check balance, pay bills | Done |
| โ Token Auth | Generate bank/card tokens | Done |
| โ Batch Transactions (Prelive) | Simulate batch processing flows | Done |
| โ Payments | Single and recurring payments | Done |
| โ Virtual Accounts | Manage virtual accounts & PayTo | Done |
| โ PayID | Create and manage PayIDs | Done |
๐งช Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
Running Tests
bundle exec rspecCode Quality
This project uses RuboCop for linting. Run it with:
bundle exec rubocopInteractive Console
For development and testing, use the interactive console:
bin/consoleThis will load the gem and all its dependencies, allowing you to experiment with the API in a REPL environment.
๐งพ Versioning
This gem follows Semantic Versioning.
See changelog.md for release history.
๐ค Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Sentia/zai-payment. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
๐ชช License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the ZaiPayment project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
๐ Documentation
Getting Started
- Authentication Guide - Two approaches to getting tokens, automatic management
- User Management Guide - Managing payin and payout users
- Item Management Guide - Creating and managing transactions/payments
- Bank Account Guide - Managing bank accounts for AU/UK users
- BPay Account Guide - Managing BPay accounts for Australian bill payments
- Wallet Account Guide - Managing wallet accounts, checking balances, and paying bills
- Virtual Account Guide - Managing virtual accounts with PayTo and BPay support
- PayID Guide - Creating and managing PayIDs for Australian NPP payments
- Webhook Examples - Complete webhook usage guide
- Documentation Index - Full documentation navigation
Examples & Patterns
- User Examples - Real-world user management patterns
- Item Examples - Transaction and payment workflows
- Bank Account Examples - Bank account integration patterns
- BPay Account Examples - BPay account integration patterns
- Wallet Account Examples - Wallet account and bill payment workflows
- Virtual Account Examples - Virtual account management and PayTo workflows
- PayID Examples - PayID creation and management workflows
- Token Auth Examples - Secure token generation and integration
- Webhook Examples - Webhook integration patterns
- Batch Transaction Examples - Testing batch transaction flows (prelive only)
Technical Guides
- Webhook Architecture - Technical implementation details
- Architecture Overview - System architecture and design
- Direct API Usage Guide - ๐ฅ How to call unimplemented APIs directly
Security
- Webhook Security Quick Start - 5-minute setup guide
- Signature Verification - Implementation details