zai_payment 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,450 @@
1
+ # Batch Transactions (Prelive Only)
2
+
3
+ ## Overview
4
+
5
+ The batch transaction endpoints allow you to work with batch transactions in both prelive and production environments. The prelive-specific endpoints are used for testing and simulation.
6
+
7
+ ### Available Operations
8
+
9
+ 1. **List Batch Transactions** - Query and filter batch transactions (all environments)
10
+ 2. **Show Batch Transaction** - Get a single batch transaction by ID (all environments)
11
+ 3. **Export Transactions** - Export pending transactions to batched state (prelive only)
12
+ 4. **Update Transaction States** - Move transactions through processing states (prelive only)
13
+
14
+ ## Configuration
15
+
16
+ First, ensure your configuration is set to use the prelive environment:
17
+
18
+ ```ruby
19
+ ZaiPayment.configure do |config|
20
+ config.environment = :prelive # Required for batch transaction endpoints
21
+ config.client_id = 'your_client_id'
22
+ config.client_secret = 'your_client_secret'
23
+ config.scope = 'your_scope'
24
+ end
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### List Batch Transactions
30
+
31
+ List and filter batch transactions. Available in all environments.
32
+
33
+ ```ruby
34
+ # List all batch transactions
35
+ response = ZaiPayment.batch_transactions.list
36
+
37
+ response.data
38
+ # => [
39
+ # {
40
+ # "id" => 12484,
41
+ # "status" => 12200,
42
+ # "reference_id" => "7190770-1-2908",
43
+ # "type" => "disbursement",
44
+ # "type_method" => "direct_credit",
45
+ # "state" => "batched",
46
+ # ...
47
+ # }
48
+ # ]
49
+
50
+ # Filter by transaction type
51
+ response = ZaiPayment.batch_transactions.list(
52
+ transaction_type: 'disbursement',
53
+ limit: 50
54
+ )
55
+
56
+ # Filter by direction and date range
57
+ response = ZaiPayment.batch_transactions.list(
58
+ direction: 'credit',
59
+ created_after: '2024-01-01T00:00:00Z',
60
+ created_before: '2024-12-31T23:59:59Z'
61
+ )
62
+
63
+ # Filter by batch ID
64
+ response = ZaiPayment.batch_transactions.list(
65
+ batch_id: '241',
66
+ limit: 100
67
+ )
68
+
69
+ # Available filters:
70
+ # - limit: number of records (default: 10, max: 200)
71
+ # - offset: pagination offset (default: 0)
72
+ # - account_id: filter by account ID
73
+ # - batch_id: filter by batch ID
74
+ # - item_id: filter by item ID
75
+ # - transaction_type: payment, refund, disbursement, fee, deposit, withdrawal
76
+ # - transaction_type_method: credit_card, npp, bpay, wallet_account_transfer, wire_transfer, misc
77
+ # - direction: debit or credit
78
+ # - created_before: ISO 8601 date/time
79
+ # - created_after: ISO 8601 date/time
80
+ # - disbursement_bank: bank used for disbursing
81
+ # - processing_bank: bank used for processing
82
+ ```
83
+
84
+ ### Show Batch Transaction
85
+
86
+ Get a single batch transaction using its ID. You can use either the UUID or numeric ID.
87
+
88
+ ```ruby
89
+ # Get by UUID
90
+ response = ZaiPayment.batch_transactions.show('90c1418b-f4f4-413e-a4ba-f29c334e7f55')
91
+
92
+ response.data
93
+ # => {
94
+ # "created_at" => "2020-05-05T12:26:59.112Z",
95
+ # "updated_at" => "2020-05-05T12:31:03.389Z",
96
+ # "id" => 13143,
97
+ # "reference_id" => "7190770-1-2908",
98
+ # "uuid" => "90c1418b-f4f4-413e-a4ba-f29c334e7f55",
99
+ # "account_external" => {
100
+ # "account_type_id" => 9100,
101
+ # "currency" => { "code" => "AUD" }
102
+ # },
103
+ # "user_email" => "assemblybuyer71391895@assemblypayments.com",
104
+ # "first_name" => "Buyer",
105
+ # "last_name" => "Last Name",
106
+ # "legal_entity_id" => "5f46763e-fb7a-4feb-9820-93a5703ddd17",
107
+ # "user_external_id" => "buyer-71391895",
108
+ # "phone" => "+1588681395",
109
+ # "marketplace" => {
110
+ # "name" => "platform1556505750",
111
+ # "uuid" => "041e8015-5101-44f1-9b7d-6a206603f29f"
112
+ # },
113
+ # "account_type" => 9100,
114
+ # "type" => "payment",
115
+ # "type_method" => "direct_debit",
116
+ # "batch_id" => 245,
117
+ # "reference" => "platform1556505750",
118
+ # "state" => "successful",
119
+ # "status" => 12000,
120
+ # "user_id" => "1ea8d380-70f9-0138-ba3b-0a58a9feac06",
121
+ # "account_id" => "26065be0-70f9-0138-9abc-0a58a9feac06",
122
+ # "from_user_name" => "Neol1604984243 Calangi",
123
+ # "from_user_id" => 1604984243,
124
+ # "amount" => 109,
125
+ # "currency" => "AUD",
126
+ # "debit_credit" => "debit",
127
+ # "description" => "Debit of $1.09 from Bank Account for Credit of $1.09 to Item",
128
+ # "related" => {
129
+ # "account_to" => {
130
+ # "id" => "edf4e4ef-0d78-48db-85e0-0af04c5dc2d6",
131
+ # "account_type" => "item",
132
+ # "user_id" => "5b9fe350-4c56-0137-e134-0242ac110002"
133
+ # }
134
+ # },
135
+ # "links" => {
136
+ # "self" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55",
137
+ # "users" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/users",
138
+ # "fees" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/fees",
139
+ # "wallet_accounts" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/wallet_accounts",
140
+ # "card_accounts" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/card_accounts",
141
+ # "paypal_accounts" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/paypal_accounts",
142
+ # "bank_accounts" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/bank_accounts",
143
+ # "items" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/items",
144
+ # "marketplace" => "/batch_transactions/90c1418b-f4f4-413e-a4ba-f29c334e7f55/marketplaces"
145
+ # }
146
+ # }
147
+
148
+ # Get by numeric ID
149
+ response = ZaiPayment.batch_transactions.show('13143')
150
+ response.data['id'] # => 13143
151
+
152
+ # Check transaction state
153
+ response = ZaiPayment.batch_transactions.show('90c1418b-f4f4-413e-a4ba-f29c334e7f55')
154
+ response.data['state'] # => "successful"
155
+ response.data['status'] # => 12000
156
+ ```
157
+
158
+ ### Step 1: Export Transactions (Prelive Only)
159
+
160
+ Call the `GET /batch_transactions/export_transactions` API which moves all pending batch_transactions into batched state. This will return all the batch_transactions that have moved from pending to batched, including their `batch_id` which you'll need for the next steps.
161
+
162
+ ```ruby
163
+ # Export all pending transactions to batched state
164
+ response = ZaiPayment.batch_transactions.export_transactions
165
+
166
+ # The response contains transactions with their batch_id
167
+ response.data
168
+ # => [
169
+ # {
170
+ # "id" => "439970a2-e0a1-418e-aecf-6b519c115c55",
171
+ # "batch_id" => "dabcfd50-bf5a-0138-7b40-0a58a9feac03",
172
+ # "status" => "batched",
173
+ # "type" => "payment_funding",
174
+ # "type_method" => "credit_card"
175
+ # }
176
+ # ]
177
+
178
+ # Store the batch_id and transaction id for next steps
179
+ batch_id = response.data.first['batch_id']
180
+ transaction_id = response.data.first['id']
181
+ ```
182
+
183
+ **Important:** When you simulate a payment journey in Pre-live, the response including the transaction `id` will be cleared after a day. If you need to retrieve this `id`, you can call the List Transactions API, input a limited number of results depending on how much time has passed, and then find it from that list of transactions.
184
+
185
+ ### Step 2: Move to Bank Processing State
186
+
187
+ Move one or more batch_transactions into `bank_processing` state (state code: `12700`). You need to pass the `batch_id` from step 1.
188
+
189
+ ```ruby
190
+ # Using the general method with state code
191
+ response = ZaiPayment.batch_transactions.update_transaction_states(
192
+ batch_id,
193
+ exported_ids: [transaction_id],
194
+ state: 12700
195
+ )
196
+
197
+ # Or use the convenience method
198
+ response = ZaiPayment.batch_transactions.process_to_bank_processing(
199
+ batch_id,
200
+ exported_ids: [transaction_id]
201
+ )
202
+
203
+ response.body
204
+ # => {
205
+ # "aggregated_jobs_uuid" => "c1cbc502-9754-42fd-9731-2330ddd7a41f",
206
+ # "msg" => "1 jobs have been sent to the queue.",
207
+ # "errors" => []
208
+ # }
209
+ ```
210
+
211
+ ### Step 3: Move to Successful State
212
+
213
+ Move one or more batch_transactions to the final state of processing being `successful` (state code: `12000`). This simulates the completion of the card funding payment processing and will trigger a `batch_transactions` webhook.
214
+
215
+ ```ruby
216
+ # Using the general method with state code
217
+ response = ZaiPayment.batch_transactions.update_transaction_states(
218
+ batch_id,
219
+ exported_ids: [transaction_id],
220
+ state: 12000
221
+ )
222
+
223
+ # Or use the convenience method
224
+ response = ZaiPayment.batch_transactions.process_to_successful(
225
+ batch_id,
226
+ exported_ids: [transaction_id]
227
+ )
228
+
229
+ response.body
230
+ # => {
231
+ # "aggregated_jobs_uuid" => "c1cbc502-9754-42fd-9731-2330ddd7a41f",
232
+ # "msg" => "1 jobs have been sent to the queue.",
233
+ # "errors" => []
234
+ # }
235
+ ```
236
+
237
+ ### Processing Multiple Transactions
238
+
239
+ You can process multiple transactions at once by passing an array of transaction IDs:
240
+
241
+ ```ruby
242
+ # Export transactions first
243
+ export_response = ZaiPayment.batch_transactions.export_transactions
244
+ batch_id = export_response.data.first['batch_id']
245
+ transaction_ids = export_response.data.map { |t| t['id'] }
246
+
247
+ # Process all to bank_processing
248
+ ZaiPayment.batch_transactions.process_to_bank_processing(
249
+ batch_id,
250
+ exported_ids: transaction_ids
251
+ )
252
+
253
+ # Then process all to successful
254
+ ZaiPayment.batch_transactions.process_to_successful(
255
+ batch_id,
256
+ exported_ids: transaction_ids
257
+ )
258
+ ```
259
+
260
+ ## Complete Example
261
+
262
+ Here's a complete example of the full workflow:
263
+
264
+ ```ruby
265
+ # Configure the client for prelive
266
+ ZaiPayment.configure do |config|
267
+ config.environment = :prelive
268
+ config.client_id = ENV['ZAI_CLIENT_ID']
269
+ config.client_secret = ENV['ZAI_CLIENT_SECRET']
270
+ config.scope = ENV['ZAI_SCOPE']
271
+ end
272
+
273
+ # Step 1: Export pending transactions to batched state
274
+ export_response = ZaiPayment.batch_transactions.export_transactions
275
+
276
+ if export_response.success?
277
+ puts "Exported #{export_response.data.length} transactions"
278
+
279
+ # Get batch_id and transaction ids
280
+ batch_id = export_response.data.first['batch_id']
281
+ transaction_ids = export_response.data.map { |t| t['id'] }
282
+
283
+ puts "Batch ID: #{batch_id}"
284
+ puts "Transaction IDs: #{transaction_ids.join(', ')}"
285
+
286
+ # Step 2: Move to bank_processing state
287
+ processing_response = ZaiPayment.batch_transactions.process_to_bank_processing(
288
+ batch_id,
289
+ exported_ids: transaction_ids
290
+ )
291
+
292
+ if processing_response.success?
293
+ puts "Moved to bank_processing state (12700)"
294
+
295
+ # Step 3: Move to successful state (triggers webhook)
296
+ success_response = ZaiPayment.batch_transactions.process_to_successful(
297
+ batch_id,
298
+ exported_ids: transaction_ids
299
+ )
300
+
301
+ if success_response.success?
302
+ puts "Moved to successful state (12000)"
303
+ puts "Webhook should be triggered for these transactions"
304
+ else
305
+ puts "Error moving to successful state: #{success_response.body}"
306
+ end
307
+ else
308
+ puts "Error moving to bank_processing: #{processing_response.body}"
309
+ end
310
+ else
311
+ puts "Error exporting transactions: #{export_response.body}"
312
+ end
313
+ ```
314
+
315
+ ## Querying Batch Transactions
316
+
317
+ If you need more information on the item, transactions, or batch_transaction, you can use the `show` method:
318
+
319
+ ```ruby
320
+ # Query a specific batch transaction by UUID
321
+ response = ZaiPayment.batch_transactions.show('90c1418b-f4f4-413e-a4ba-f29c334e7f55')
322
+
323
+ # Access transaction details
324
+ response.data['state'] # => "successful"
325
+ response.data['amount'] # => 109
326
+ response.data['currency'] # => "AUD"
327
+ response.data['type'] # => "payment"
328
+ response.data['type_method'] # => "direct_debit"
329
+
330
+ # Or query by numeric ID
331
+ response = ZaiPayment.batch_transactions.show('13143')
332
+ ```
333
+
334
+ ## State Codes
335
+
336
+ - **12700**: `bank_processing` - Transactions are being processed by the bank
337
+ - **12000**: `successful` - Final state, processing complete, triggers webhook
338
+
339
+ ## Error Handling
340
+
341
+ The batch transaction endpoints will raise errors in the following cases:
342
+
343
+ ### ConfigurationError
344
+
345
+ Raised when attempting to use batch transaction endpoints in production environment:
346
+
347
+ ```ruby
348
+ begin
349
+ ZaiPayment.batch_transactions.export_transactions
350
+ rescue ZaiPayment::Errors::ConfigurationError => e
351
+ puts e.message
352
+ # => "Batch transaction endpoints are only available in prelive environment. Current environment: production"
353
+ end
354
+ ```
355
+
356
+ ### ValidationError
357
+
358
+ Raised when parameters are invalid:
359
+
360
+ ```ruby
361
+ # Empty batch_id
362
+ ZaiPayment.batch_transactions.process_to_bank_processing('', exported_ids: ['id'])
363
+ # => ValidationError: batch_id is required and cannot be blank
364
+
365
+ # Empty exported_ids array
366
+ ZaiPayment.batch_transactions.process_to_bank_processing('batch_id', exported_ids: [])
367
+ # => ValidationError: exported_ids is required and must be a non-empty array
368
+
369
+ # Invalid state code
370
+ ZaiPayment.batch_transactions.update_transaction_states('batch_id', exported_ids: ['id'], state: 99999)
371
+ # => ValidationError: state must be 12700 (bank_processing) or 12000 (successful), got: 99999
372
+ ```
373
+
374
+ ## Available Methods
375
+
376
+ ### `list(**options)`
377
+
378
+ List and filter batch transactions.
379
+
380
+ - **Parameters:**
381
+ - `limit` (Integer): Number of records to return (default: 10, max: 200)
382
+ - `offset` (Integer): Number of records to skip (default: 0)
383
+ - `account_id` (String): Filter by account ID
384
+ - `batch_id` (String): Filter by batch ID
385
+ - `item_id` (String): Filter by item ID
386
+ - `transaction_type` (String): payment, refund, disbursement, fee, deposit, withdrawal
387
+ - `transaction_type_method` (String): credit_card, npp, bpay, wallet_account_transfer, wire_transfer, misc
388
+ - `direction` (String): debit or credit
389
+ - `created_before` (String): ISO 8601 date/time
390
+ - `created_after` (String): ISO 8601 date/time
391
+ - `disbursement_bank` (String): Bank used for disbursing
392
+ - `processing_bank` (String): Bank used for processing
393
+ - **Returns:** Response with batch_transactions array
394
+ - **Available in:** All environments
395
+
396
+ ### `show(id)`
397
+
398
+ Get a single batch transaction using its ID.
399
+
400
+ - **Parameters:**
401
+ - `id` (String): The batch transaction ID (UUID or numeric ID)
402
+ - **Returns:** Response with batch_transactions object
403
+ - **Raises:** ValidationError if id is blank
404
+ - **Available in:** All environments
405
+
406
+ ### `export_transactions`
407
+
408
+ Exports all pending batch transactions to batched state.
409
+
410
+ - **Returns:** Response with transactions array containing batch_id
411
+ - **Raises:** ConfigurationError if not in prelive environment
412
+
413
+ ### `update_transaction_states(batch_id, exported_ids:, state:)`
414
+
415
+ Updates batch transaction states to a specific state code.
416
+
417
+ - **Parameters:**
418
+ - `batch_id` (String): The batch ID from export_transactions
419
+ - `exported_ids` (Array<String>): Array of transaction IDs to update
420
+ - `state` (Integer): Target state code (12700 or 12000)
421
+ - **Returns:** Response with exported_ids and state
422
+ - **Raises:** ConfigurationError, ValidationError
423
+
424
+ ### `process_to_bank_processing(batch_id, exported_ids:)`
425
+
426
+ Convenience method to move transactions to bank_processing state (12700).
427
+
428
+ - **Parameters:**
429
+ - `batch_id` (String): The batch ID from export_transactions
430
+ - `exported_ids` (Array<String>): Array of transaction IDs to update
431
+ - **Returns:** Response with exported_ids and state
432
+ - **Raises:** ConfigurationError, ValidationError
433
+
434
+ ### `process_to_successful(batch_id, exported_ids:)`
435
+
436
+ Convenience method to move transactions to successful state (12000).
437
+
438
+ - **Parameters:**
439
+ - `batch_id` (String): The batch ID from export_transactions
440
+ - `exported_ids` (Array<String>): Array of transaction IDs to update
441
+ - **Returns:** Response with exported_ids and state
442
+ - **Raises:** ConfigurationError, ValidationError
443
+
444
+ ## Notes
445
+
446
+ - These endpoints are **only available in the prelive environment**
447
+ - Transaction IDs from prelive simulations may be cleared after a day
448
+ - The successful state (12000) triggers batch_transactions webhooks
449
+ - You must store the `batch_id` from the export_transactions response to use in subsequent calls
450
+