The Batch Transaction resource provides methods to simulate batch transaction processing in the prelive environment. These endpoints are designed for testing and allow you to move transactions through different states of the batch processing lifecycle.
Important: These endpoints are only available when the gem is configured for the :prelive environment. Attempting to use them in production will raise a ConfigurationError.
Use the batch transaction endpoints when you need to:
- Test the full lifecycle of batch transaction processing
- Simulate bank processing states in your test environment
- Trigger batch transaction webhooks for testing webhook handlers
- Verify your application's handling of different batch transaction states
Ensure your configuration uses the prelive environment:
ZaiPayment.configure do |config|
config.environment = :prelive # Required!
config.client_id = 'your_client_id'
config.client_secret = 'your_client_secret'
config.scope = 'your_scope'
endAccess the batch transaction resource through the main module:
batch_transactions = ZaiPayment.batch_transactionsIn prelive, batch transactions go through these states:
- pending → Initial state when a batch transaction is created
- batched → Transactions grouped into a batch (via
export_transactions) - bank_processing (state 12700) → Batch is being processed by the bank
- successful (state 12000) → Processing complete, webhook triggered
Moves all pending batch transactions to the "batched" state and returns them with their assigned batch_id.
Signature:
export_transactions() → ResponseReturns:
Responseobject with transactions array- Each transaction includes:
id,batch_id,status,type,type_method
Raises:
ConfigurationError- if not in prelive environment
Example:
response = ZaiPayment.batch_transactions.export_transactions
response.data
# => [
# {
# "id" => "439970a2-e0a1-418e-aecf-6b519c115c55",
# "batch_id" => "dabcfd50-bf5a-0138-7b40-0a58a9feac03",
# "status" => "batched",
# "type" => "payment_funding",
# "type_method" => "credit_card"
# }
# ]Note: Store the returned batch_id - you'll need it for the next steps. Transaction IDs may be cleared after 24 hours in prelive.
Updates the state of one or more batch transactions within a batch.
Signature:
update_transaction_states(batch_id, exported_ids:, state:) → ResponseParameters:
batch_id(String) - The batch ID fromexport_transactionsresponseexported_ids(Array) - Array of transaction IDs to updatestate(Integer) - Target state code:12700(bank_processing) or12000(successful)
Returns:
Responseobject withaggregated_jobs_uuid,msg, anderrorsarray
Raises:
ConfigurationError- if not in prelive environmentValidationError- if batch_id is blankValidationError- if exported_ids is nil, empty, not an array, or contains nil/empty valuesValidationError- if state is not 12700 or 12000
Example:
response = ZaiPayment.batch_transactions.update_transaction_states(
'batch_id',
exported_ids: ['439970a2-e0a1-418e-aecf-6b519c115c55'],
state: 12700
)
response.body
# => {
# "aggregated_jobs_uuid" => "c1cbc502-9754-42fd-9731-2330ddd7a41f",
# "msg" => "1 jobs have been sent to the queue.",
# "errors" => []
# }Convenience method to move transactions to the "bank_processing" state (12700).
Signature:
process_to_bank_processing(batch_id, exported_ids:) → ResponseParameters:
batch_id(String) - The batch ID fromexport_transactionsresponseexported_ids(Array) - Array of transaction IDs to update
Returns:
Responseobject withaggregated_jobs_uuid,msg, anderrors(12700)
Raises:
- Same as
update_transaction_states
Example:
response = ZaiPayment.batch_transactions.process_to_bank_processing(
'batch_id',
exported_ids: ['transaction_id_1', 'transaction_id_2']
)Note: This is equivalent to calling update_transaction_states with state: 12700.
Convenience method to move transactions to the "successful" state (12000). This triggers batch transaction webhooks.
Signature:
process_to_successful(batch_id, exported_ids:) → ResponseParameters:
batch_id(String) - The batch ID fromexport_transactionsresponseexported_ids(Array) - Array of transaction IDs to update
Returns:
Responseobject withaggregated_jobs_uuid,msg, anderrors(12000)
Raises:
- Same as
update_transaction_states
Example:
response = ZaiPayment.batch_transactions.process_to_successful(
'batch_id',
exported_ids: ['transaction_id_1']
)
# This will trigger a batch_transactions webhookNote: This is equivalent to calling update_transaction_states with state: 12000.
Here's the typical workflow for testing batch transactions in prelive:
# Step 1: Export pending transactions
export_response = ZaiPayment.batch_transactions.export_transactions
batch_id = export_response.data.first['batch_id']
transaction_ids = export_response.data.map { |t| t['id'] }
puts "Exported #{transaction_ids.length} transactions to batch #{batch_id}"
# Step 2: Move to bank_processing state
processing_response = ZaiPayment.batch_transactions.process_to_bank_processing(
batch_id,
exported_ids: transaction_ids
)
puts "Transactions moved to bank_processing (12700)"
# Step 3: Move to successful state (triggers webhook)
success_response = ZaiPayment.batch_transactions.process_to_successful(
batch_id,
exported_ids: transaction_ids
)
puts "Transactions completed successfully (12000)"
puts "Webhook should now be triggered"| Code | State | Description |
|---|---|---|
| 12700 | bank_processing | Transactions are being processed by the bank |
| 12000 | successful | Final state - processing complete, webhooks triggered |
Raised when attempting to use batch transaction endpoints outside of prelive:
ZaiPayment.configure do |config|
config.environment = :production # Wrong environment!
end
begin
ZaiPayment.batch_transactions.export_transactions
rescue ZaiPayment::Errors::ConfigurationError => e
puts e.message
# => "Batch transaction endpoints are only available in prelive environment.
# Current environment: production"
endRaised when parameters are invalid:
# Blank batch_id
ZaiPayment.batch_transactions.process_to_bank_processing(
'',
exported_ids: ['id']
)
# => ValidationError: batch_id is required and cannot be blank
# Empty exported_ids
ZaiPayment.batch_transactions.process_to_bank_processing(
'batch_id',
exported_ids: []
)
# => ValidationError: exported_ids is required and must be a non-empty array
# exported_ids contains nil
ZaiPayment.batch_transactions.process_to_bank_processing(
'batch_id',
exported_ids: ['valid_id', nil]
)
# => ValidationError: exported_ids cannot contain nil or empty values
# Invalid state code
ZaiPayment.batch_transactions.update_transaction_states(
'batch_id',
exported_ids: ['id'],
state: 99999
)
# => ValidationError: state must be 12700 (bank_processing) or 12000 (successful), got: 99999The process_to_successful method triggers batch transaction webhooks, allowing you to test your webhook handlers:
# Set up webhook handler to receive batch_transactions events
# Then complete the batch transaction flow:
export_response = ZaiPayment.batch_transactions.export_transactions
batch_id = export_response.data.first['batch_id']
transaction_ids = export_response.data.map { |t| t['id'] }
# Process through states
ZaiPayment.batch_transactions.process_to_bank_processing(batch_id, exported_ids: transaction_ids)
ZaiPayment.batch_transactions.process_to_successful(batch_id, exported_ids: transaction_ids)
# Your webhook handler should now receive the batch_transactions webhookAfter exporting and processing transactions, you can query them using the item's transaction endpoints:
# List transactions for an item
item_id = 'your_item_id'
response = ZaiPayment.items.list_transactions(item_id)
# List batch transactions for an item
response = ZaiPayment.items.list_batch_transactions(item_id)You can also query batch transactions directly via the API using cURL:
curl https://test.api.promisepay.com/batch_transactions/dabcfd50-bf5a-0138-7b40-0a58a9feac03 \
-H "Authorization: Bearer YOUR_TOKEN"- Prelive Only: These endpoints only work in prelive environment
- 24-Hour Limit: Transaction IDs may be cleared after 24 hours in prelive
- Webhook Trigger: Only the successful state (12000) triggers webhooks
- Batch ID Required: You must save the
batch_idfromexport_transactionsfor subsequent calls - Array Support: You can process multiple transactions at once by passing multiple IDs
- Items - For creating items that generate transactions
- Webhooks - For handling batch transaction webhooks
- Webhook Security - For securing your webhook endpoints
For more details on the underlying API endpoints, see the Zai API Documentation.