Skip to content

Sentia/zai-payment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Zai Payment Ruby Library

GitHub License Code of Conduct Gem Version GitHub release Gem CI Endpoint Badge GitHub top language Documentation Contributing

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_token

The 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:

Items

Manage transactions/payments between buyers and sellers.

πŸ“š Documentation:

Bank Accounts

Manage bank accounts for Australian and UK users, with routing number validation.

πŸ“š Documentation:

BPay Accounts

Manage BPay accounts for Australian bill payments.

πŸ“š Documentation:

Wallet Accounts

Manage wallet accounts, check balances, and pay bills via BPay.

πŸ“š Documentation:

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']}"
end

Virtual Accounts

Manage virtual accounts with support for AKA names and Confirmation of Payee (CoP) lookups.

πŸ“š Documentation:

PayID

Register and manage PayIDs (EMAIL type) for Australian NPP (New Payments Platform) payments.

πŸ“š Documentation:

Token Auth

Generate secure tokens for collecting bank and card account information.

πŸ“š Documentation:

Webhooks

Manage webhook endpoints with secure signature verification.

πŸ“š Documentation:

Batch Transactions (Prelive Only)

Simulate batch transaction processing for testing in the prelive environment.

πŸ“š Documentation:

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 rspec

Code Quality

This project uses RuboCop for linting. Run it with:

bundle exec rubocop

Interactive Console

For development and testing, use the interactive console:

bin/console

This 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

Examples & Patterns

Technical Guides

Security

External Resources

About

Ruby library for the Zai Payment API.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages