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,340 @@
1
+ # Batch Transactions (Prelive Only)
2
+
3
+ ## Overview
4
+
5
+ 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.
6
+
7
+ **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`.
8
+
9
+ ## When to Use
10
+
11
+ Use the batch transaction endpoints when you need to:
12
+
13
+ 1. Test the full lifecycle of batch transaction processing
14
+ 2. Simulate bank processing states in your test environment
15
+ 3. Trigger batch transaction webhooks for testing webhook handlers
16
+ 4. Verify your application's handling of different batch transaction states
17
+
18
+ ## Authentication & Configuration
19
+
20
+ Ensure your configuration uses the prelive environment:
21
+
22
+ ```ruby
23
+ ZaiPayment.configure do |config|
24
+ config.environment = :prelive # Required!
25
+ config.client_id = 'your_client_id'
26
+ config.client_secret = 'your_client_secret'
27
+ config.scope = 'your_scope'
28
+ end
29
+ ```
30
+
31
+ ## Resource Access
32
+
33
+ Access the batch transaction resource through the main module:
34
+
35
+ ```ruby
36
+ batch_transactions = ZaiPayment.batch_transactions
37
+ ```
38
+
39
+ ## The Batch Transaction Lifecycle
40
+
41
+ In prelive, batch transactions go through these states:
42
+
43
+ 1. **pending** → Initial state when a batch transaction is created
44
+ 2. **batched** → Transactions grouped into a batch (via `export_transactions`)
45
+ 3. **bank_processing** (state 12700) → Batch is being processed by the bank
46
+ 4. **successful** (state 12000) → Processing complete, webhook triggered
47
+
48
+ ## Method Reference
49
+
50
+ ### `export_transactions`
51
+
52
+ Moves all pending batch transactions to the "batched" state and returns them with their assigned `batch_id`.
53
+
54
+ **Signature:**
55
+ ```ruby
56
+ export_transactions() → Response
57
+ ```
58
+
59
+ **Returns:**
60
+ - `Response` object with transactions array
61
+ - Each transaction includes: `id`, `batch_id`, `status`, `type`, `type_method`
62
+
63
+ **Raises:**
64
+ - `ConfigurationError` - if not in prelive environment
65
+
66
+ **Example:**
67
+ ```ruby
68
+ response = ZaiPayment.batch_transactions.export_transactions
69
+
70
+ response.data
71
+ # => [
72
+ # {
73
+ # "id" => "439970a2-e0a1-418e-aecf-6b519c115c55",
74
+ # "batch_id" => "dabcfd50-bf5a-0138-7b40-0a58a9feac03",
75
+ # "status" => "batched",
76
+ # "type" => "payment_funding",
77
+ # "type_method" => "credit_card"
78
+ # }
79
+ # ]
80
+ ```
81
+
82
+ **Note:** Store the returned `batch_id` - you'll need it for the next steps. Transaction IDs may be cleared after 24 hours in prelive.
83
+
84
+ ---
85
+
86
+ ### `update_transaction_states`
87
+
88
+ Updates the state of one or more batch transactions within a batch.
89
+
90
+ **Signature:**
91
+ ```ruby
92
+ update_transaction_states(batch_id, exported_ids:, state:) → Response
93
+ ```
94
+
95
+ **Parameters:**
96
+ - `batch_id` (String) - The batch ID from `export_transactions` response
97
+ - `exported_ids` (Array<String>) - Array of transaction IDs to update
98
+ - `state` (Integer) - Target state code: `12700` (bank_processing) or `12000` (successful)
99
+
100
+ **Returns:**
101
+ - `Response` object with `aggregated_jobs_uuid`, `msg`, and `errors` array
102
+
103
+ **Raises:**
104
+ - `ConfigurationError` - if not in prelive environment
105
+ - `ValidationError` - if batch_id is blank
106
+ - `ValidationError` - if exported_ids is nil, empty, not an array, or contains nil/empty values
107
+ - `ValidationError` - if state is not 12700 or 12000
108
+
109
+ **Example:**
110
+ ```ruby
111
+ response = ZaiPayment.batch_transactions.update_transaction_states(
112
+ 'batch_id',
113
+ exported_ids: ['439970a2-e0a1-418e-aecf-6b519c115c55'],
114
+ state: 12700
115
+ )
116
+
117
+ response.body
118
+ # => {
119
+ # "aggregated_jobs_uuid" => "c1cbc502-9754-42fd-9731-2330ddd7a41f",
120
+ # "msg" => "1 jobs have been sent to the queue.",
121
+ # "errors" => []
122
+ # }
123
+ ```
124
+
125
+ ---
126
+
127
+ ### `process_to_bank_processing`
128
+
129
+ Convenience method to move transactions to the "bank_processing" state (12700).
130
+
131
+ **Signature:**
132
+ ```ruby
133
+ process_to_bank_processing(batch_id, exported_ids:) → Response
134
+ ```
135
+
136
+ **Parameters:**
137
+ - `batch_id` (String) - The batch ID from `export_transactions` response
138
+ - `exported_ids` (Array<String>) - Array of transaction IDs to update
139
+
140
+ **Returns:**
141
+ - `Response` object with `aggregated_jobs_uuid`, `msg`, and `errors` (12700)
142
+
143
+ **Raises:**
144
+ - Same as `update_transaction_states`
145
+
146
+ **Example:**
147
+ ```ruby
148
+ response = ZaiPayment.batch_transactions.process_to_bank_processing(
149
+ 'batch_id',
150
+ exported_ids: ['transaction_id_1', 'transaction_id_2']
151
+ )
152
+ ```
153
+
154
+ **Note:** This is equivalent to calling `update_transaction_states` with `state: 12700`.
155
+
156
+ ---
157
+
158
+ ### `process_to_successful`
159
+
160
+ Convenience method to move transactions to the "successful" state (12000). This triggers batch transaction webhooks.
161
+
162
+ **Signature:**
163
+ ```ruby
164
+ process_to_successful(batch_id, exported_ids:) → Response
165
+ ```
166
+
167
+ **Parameters:**
168
+ - `batch_id` (String) - The batch ID from `export_transactions` response
169
+ - `exported_ids` (Array<String>) - Array of transaction IDs to update
170
+
171
+ **Returns:**
172
+ - `Response` object with `aggregated_jobs_uuid`, `msg`, and `errors` (12000)
173
+
174
+ **Raises:**
175
+ - Same as `update_transaction_states`
176
+
177
+ **Example:**
178
+ ```ruby
179
+ response = ZaiPayment.batch_transactions.process_to_successful(
180
+ 'batch_id',
181
+ exported_ids: ['transaction_id_1']
182
+ )
183
+
184
+ # This will trigger a batch_transactions webhook
185
+ ```
186
+
187
+ **Note:** This is equivalent to calling `update_transaction_states` with `state: 12000`.
188
+
189
+ ---
190
+
191
+ ## Complete Workflow Example
192
+
193
+ Here's the typical workflow for testing batch transactions in prelive:
194
+
195
+ ```ruby
196
+ # Step 1: Export pending transactions
197
+ export_response = ZaiPayment.batch_transactions.export_transactions
198
+
199
+ batch_id = export_response.data.first['batch_id']
200
+ transaction_ids = export_response.data.map { |t| t['id'] }
201
+
202
+ puts "Exported #{transaction_ids.length} transactions to batch #{batch_id}"
203
+
204
+ # Step 2: Move to bank_processing state
205
+ processing_response = ZaiPayment.batch_transactions.process_to_bank_processing(
206
+ batch_id,
207
+ exported_ids: transaction_ids
208
+ )
209
+
210
+ puts "Transactions moved to bank_processing (12700)"
211
+
212
+ # Step 3: Move to successful state (triggers webhook)
213
+ success_response = ZaiPayment.batch_transactions.process_to_successful(
214
+ batch_id,
215
+ exported_ids: transaction_ids
216
+ )
217
+
218
+ puts "Transactions completed successfully (12000)"
219
+ puts "Webhook should now be triggered"
220
+ ```
221
+
222
+ ## State Codes
223
+
224
+ | Code | State | Description |
225
+ |------|-------|-------------|
226
+ | 12700 | bank_processing | Transactions are being processed by the bank |
227
+ | 12000 | successful | Final state - processing complete, webhooks triggered |
228
+
229
+ ## Error Handling
230
+
231
+ ### ConfigurationError
232
+
233
+ Raised when attempting to use batch transaction endpoints outside of prelive:
234
+
235
+ ```ruby
236
+ ZaiPayment.configure do |config|
237
+ config.environment = :production # Wrong environment!
238
+ end
239
+
240
+ begin
241
+ ZaiPayment.batch_transactions.export_transactions
242
+ rescue ZaiPayment::Errors::ConfigurationError => e
243
+ puts e.message
244
+ # => "Batch transaction endpoints are only available in prelive environment.
245
+ # Current environment: production"
246
+ end
247
+ ```
248
+
249
+ ### ValidationError
250
+
251
+ Raised when parameters are invalid:
252
+
253
+ ```ruby
254
+ # Blank batch_id
255
+ ZaiPayment.batch_transactions.process_to_bank_processing(
256
+ '',
257
+ exported_ids: ['id']
258
+ )
259
+ # => ValidationError: batch_id is required and cannot be blank
260
+
261
+ # Empty exported_ids
262
+ ZaiPayment.batch_transactions.process_to_bank_processing(
263
+ 'batch_id',
264
+ exported_ids: []
265
+ )
266
+ # => ValidationError: exported_ids is required and must be a non-empty array
267
+
268
+ # exported_ids contains nil
269
+ ZaiPayment.batch_transactions.process_to_bank_processing(
270
+ 'batch_id',
271
+ exported_ids: ['valid_id', nil]
272
+ )
273
+ # => ValidationError: exported_ids cannot contain nil or empty values
274
+
275
+ # Invalid state code
276
+ ZaiPayment.batch_transactions.update_transaction_states(
277
+ 'batch_id',
278
+ exported_ids: ['id'],
279
+ state: 99999
280
+ )
281
+ # => ValidationError: state must be 12700 (bank_processing) or 12000 (successful), got: 99999
282
+ ```
283
+
284
+ ## Testing Webhooks
285
+
286
+ The `process_to_successful` method triggers batch transaction webhooks, allowing you to test your webhook handlers:
287
+
288
+ ```ruby
289
+ # Set up webhook handler to receive batch_transactions events
290
+ # Then complete the batch transaction flow:
291
+
292
+ export_response = ZaiPayment.batch_transactions.export_transactions
293
+ batch_id = export_response.data.first['batch_id']
294
+ transaction_ids = export_response.data.map { |t| t['id'] }
295
+
296
+ # Process through states
297
+ ZaiPayment.batch_transactions.process_to_bank_processing(batch_id, exported_ids: transaction_ids)
298
+ ZaiPayment.batch_transactions.process_to_successful(batch_id, exported_ids: transaction_ids)
299
+
300
+ # Your webhook handler should now receive the batch_transactions webhook
301
+ ```
302
+
303
+ ## Querying Batch Transactions
304
+
305
+ After exporting and processing transactions, you can query them using the item's transaction endpoints:
306
+
307
+ ```ruby
308
+ # List transactions for an item
309
+ item_id = 'your_item_id'
310
+ response = ZaiPayment.items.list_transactions(item_id)
311
+
312
+ # List batch transactions for an item
313
+ response = ZaiPayment.items.list_batch_transactions(item_id)
314
+ ```
315
+
316
+ You can also query batch transactions directly via the API using cURL:
317
+
318
+ ```bash
319
+ curl https://test.api.promisepay.com/batch_transactions/dabcfd50-bf5a-0138-7b40-0a58a9feac03 \
320
+ -H "Authorization: Bearer YOUR_TOKEN"
321
+ ```
322
+
323
+ ## Important Notes
324
+
325
+ 1. **Prelive Only**: These endpoints only work in prelive environment
326
+ 2. **24-Hour Limit**: Transaction IDs may be cleared after 24 hours in prelive
327
+ 3. **Webhook Trigger**: Only the successful state (12000) triggers webhooks
328
+ 4. **Batch ID Required**: You must save the `batch_id` from `export_transactions` for subsequent calls
329
+ 5. **Array Support**: You can process multiple transactions at once by passing multiple IDs
330
+
331
+ ## Related Resources
332
+
333
+ - [Items](./items.md) - For creating items that generate transactions
334
+ - [Webhooks](./webhooks.md) - For handling batch transaction webhooks
335
+ - [Webhook Security](./webhook_security_quickstart.md) - For securing your webhook endpoints
336
+
337
+ ## API Reference
338
+
339
+ For more details on the underlying API endpoints, see the [Zai API Documentation](https://developer.hellozai.com/reference).
340
+