zai_payment 1.3.1 → 2.0.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.
data/examples/items.md ADDED
@@ -0,0 +1,598 @@
1
+ # Zai Payment - Items Examples
2
+
3
+ This document provides examples of how to use the Items resource in the Zai Payment gem.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Setup](#setup)
8
+ - [Create Item](#create-item)
9
+ - [List Items](#list-items)
10
+ - [Show Item](#show-item)
11
+ - [Update Item](#update-item)
12
+ - [Delete Item](#delete-item)
13
+ - [Show Item Seller](#show-item-seller)
14
+ - [Show Item Buyer](#show-item-buyer)
15
+ - [Show Item Fees](#show-item-fees)
16
+ - [Show Item Wire Details](#show-item-wire-details)
17
+ - [List Item Transactions](#list-item-transactions)
18
+ - [List Item Batch Transactions](#list-item-batch-transactions)
19
+ - [Show Item Status](#show-item-status)
20
+
21
+ ## Setup
22
+
23
+ ```ruby
24
+ require 'zai_payment'
25
+
26
+ # Configure the gem
27
+ ZaiPayment.configure do |config|
28
+ config.client_id = 'your_client_id'
29
+ config.client_secret = 'your_client_secret'
30
+ config.scope = 'your_scope'
31
+ config.environment = :prelive # or :production
32
+ end
33
+
34
+ # Access the items resource
35
+ items = ZaiPayment.items
36
+ ```
37
+
38
+ ## Create Item
39
+
40
+ Create a new item (transaction/payment) between a buyer and a seller.
41
+
42
+ ```ruby
43
+ # Create a basic item
44
+ response = items.create(
45
+ name: "Product Purchase",
46
+ amount: 10000, # Amount in cents (100.00)
47
+ payment_type: 2, # Payment type (1-7, 2 is default)
48
+ buyer_id: "buyer-123",
49
+ seller_id: "seller-456"
50
+ )
51
+
52
+ if response.success?
53
+ item = response.data
54
+ puts "Item created: #{item['id']}"
55
+ puts "Name: #{item['name']}"
56
+ puts "Amount: #{item['amount']}"
57
+ else
58
+ puts "Error: #{response.error_message}"
59
+ end
60
+ ```
61
+
62
+ ### Create Item with Optional Fields
63
+
64
+ ```ruby
65
+ response = items.create(
66
+ name: "Premium Product",
67
+ amount: 25000,
68
+ payment_type: 2,
69
+ buyer_id: "buyer-123",
70
+ seller_id: "seller-456",
71
+ description: "Purchase of premium product XYZ",
72
+ currency: "AUD",
73
+ fee_ids: ["fee-1", "fee-2"],
74
+ custom_descriptor: "MY STORE PURCHASE",
75
+ buyer_url: "https://buyer.example.com",
76
+ seller_url: "https://seller.example.com",
77
+ tax_invoice: true
78
+ )
79
+
80
+ if response.success?
81
+ item = response.data
82
+ puts "Item created with ID: #{item['id']}"
83
+ puts "Description: #{item['description']}"
84
+ puts "Tax Invoice: #{item['tax_invoice']}"
85
+ end
86
+ ```
87
+
88
+ ### Create Item with Custom ID
89
+
90
+ ```ruby
91
+ response = items.create(
92
+ id: "my-custom-item-#{Time.now.to_i}",
93
+ name: "Custom ID Product",
94
+ amount: 15000,
95
+ payment_type: 2,
96
+ buyer_id: "buyer-123",
97
+ seller_id: "seller-456"
98
+ )
99
+
100
+ if response.success?
101
+ item = response.data
102
+ puts "Item created with custom ID: #{item['id']}"
103
+ end
104
+ ```
105
+
106
+ ## List Items
107
+
108
+ Retrieve a list of all items with pagination and optional search/filtering.
109
+
110
+ ```ruby
111
+ # List items with default pagination (10 items)
112
+ response = items.list
113
+
114
+ if response.success?
115
+ items_list = response.data
116
+ items_list.each do |item|
117
+ puts "Item ID: #{item['id']}, Name: #{item['name']}, Amount: #{item['amount']}"
118
+ end
119
+
120
+ # Access metadata
121
+ meta = response.meta
122
+ puts "Total items: #{meta['total']}"
123
+ puts "Limit: #{meta['limit']}"
124
+ puts "Offset: #{meta['offset']}"
125
+ end
126
+ ```
127
+
128
+ ### List Items with Custom Pagination
129
+
130
+ ```ruby
131
+ # List 20 items starting from offset 40
132
+ response = items.list(limit: 20, offset: 40)
133
+
134
+ if response.success?
135
+ items_list = response.data
136
+ puts "Retrieved #{items_list.length} items"
137
+ end
138
+ ```
139
+
140
+ ### Search Items by Description
141
+
142
+ ```ruby
143
+ # Search for items with "product" in the description
144
+ response = items.list(search: "product")
145
+
146
+ if response.success?
147
+ items_list = response.data
148
+ puts "Found #{items_list.length} items matching 'product'"
149
+ items_list.each do |item|
150
+ puts " - #{item['name']}: #{item['description']}"
151
+ end
152
+ end
153
+ ```
154
+
155
+ ### Filter Items by Creation Date
156
+
157
+ ```ruby
158
+ # Get items created in a specific date range
159
+ response = items.list(
160
+ created_after: "2024-01-01T00:00:00Z",
161
+ created_before: "2024-12-31T23:59:59Z"
162
+ )
163
+
164
+ if response.success?
165
+ items_list = response.data
166
+ puts "Found #{items_list.length} items created in 2024"
167
+ end
168
+ ```
169
+
170
+ ### Combine Search and Filters
171
+
172
+ ```ruby
173
+ # Search with pagination and date filters
174
+ response = items.list(
175
+ limit: 50,
176
+ offset: 0,
177
+ search: "premium",
178
+ created_after: "2024-01-01T00:00:00Z"
179
+ )
180
+
181
+ if response.success?
182
+ items_list = response.data
183
+ puts "Found #{items_list.length} premium items created after Jan 1, 2024"
184
+ end
185
+ ```
186
+
187
+ ## Show Item
188
+
189
+ Get details of a specific item by ID.
190
+
191
+ ```ruby
192
+ response = items.show("item-123")
193
+
194
+ if response.success?
195
+ item = response.data
196
+ puts "Item ID: #{item['id']}"
197
+ puts "Name: #{item['name']}"
198
+ puts "Amount: #{item['amount']}"
199
+ puts "Payment Type: #{item['payment_type']}"
200
+ puts "Buyer ID: #{item['buyer_id']}"
201
+ puts "Seller ID: #{item['seller_id']}"
202
+ puts "Description: #{item['description']}"
203
+ puts "State: #{item['state']}"
204
+ puts "Buyer URL: #{item['buyer_url']}" if item['buyer_url']
205
+ puts "Seller URL: #{item['seller_url']}" if item['seller_url']
206
+ puts "Tax Invoice: #{item['tax_invoice']}" unless item['tax_invoice'].nil?
207
+ else
208
+ puts "Error: #{response.error_message}"
209
+ end
210
+ ```
211
+
212
+ ## Update Item
213
+
214
+ Update an existing item's details.
215
+
216
+ ```ruby
217
+ response = items.update(
218
+ "item-123",
219
+ name: "Updated Product Name",
220
+ description: "Updated product description",
221
+ amount: 12000,
222
+ buyer_url: "https://new-buyer.example.com",
223
+ tax_invoice: false
224
+ )
225
+
226
+ if response.success?
227
+ item = response.data
228
+ puts "Item updated: #{item['id']}"
229
+ puts "New name: #{item['name']}"
230
+ puts "New amount: #{item['amount']}"
231
+ puts "Tax Invoice: #{item['tax_invoice']}"
232
+ else
233
+ puts "Error: #{response.error_message}"
234
+ end
235
+ ```
236
+
237
+ ### Update Item Seller or Buyer
238
+
239
+ ```ruby
240
+ response = items.update(
241
+ "item-123",
242
+ seller_id: "new-seller-789",
243
+ buyer_id: "new-buyer-012"
244
+ )
245
+
246
+ if response.success?
247
+ puts "Item updated with new buyer and seller"
248
+ end
249
+ ```
250
+
251
+ ## Delete Item
252
+
253
+ Delete an item by ID.
254
+
255
+ ```ruby
256
+ response = items.delete("item-123")
257
+
258
+ if response.success?
259
+ puts "Item deleted successfully"
260
+ else
261
+ puts "Error: #{response.error_message}"
262
+ end
263
+ ```
264
+
265
+ ## Show Item Seller
266
+
267
+ Get the seller (user) details for a specific item.
268
+
269
+ ```ruby
270
+ response = items.show_seller("item-123")
271
+
272
+ if response.success?
273
+ seller = response.data
274
+ puts "Seller ID: #{seller['id']}"
275
+ puts "Seller Email: #{seller['email']}"
276
+ puts "Seller Name: #{seller['first_name']} #{seller['last_name']}"
277
+ puts "Country: #{seller['country']}"
278
+ else
279
+ puts "Error: #{response.error_message}"
280
+ end
281
+ ```
282
+
283
+ ## Show Item Buyer
284
+
285
+ Get the buyer (user) details for a specific item.
286
+
287
+ ```ruby
288
+ response = items.show_buyer("item-123")
289
+
290
+ if response.success?
291
+ buyer = response.data
292
+ puts "Buyer ID: #{buyer['id']}"
293
+ puts "Buyer Email: #{buyer['email']}"
294
+ puts "Buyer Name: #{buyer['first_name']} #{buyer['last_name']}"
295
+ puts "Country: #{buyer['country']}"
296
+ else
297
+ puts "Error: #{response.error_message}"
298
+ end
299
+ ```
300
+
301
+ ## Show Item Fees
302
+
303
+ Get the fees associated with an item.
304
+
305
+ ```ruby
306
+ response = items.show_fees("item-123")
307
+
308
+ if response.success?
309
+ fees = response.data
310
+
311
+ if fees && fees.any?
312
+ puts "Item has #{fees.length} fee(s):"
313
+ fees.each do |fee|
314
+ puts " Fee ID: #{fee['id']}"
315
+ puts " Name: #{fee['name']}"
316
+ puts " Amount: #{fee['amount']}"
317
+ puts " Fee Type: #{fee['fee_type']}"
318
+ end
319
+ else
320
+ puts "No fees associated with this item"
321
+ end
322
+ else
323
+ puts "Error: #{response.error_message}"
324
+ end
325
+ ```
326
+
327
+ ## Show Item Wire Details
328
+
329
+ Get wire transfer details for an item.
330
+
331
+ ```ruby
332
+ response = items.show_wire_details("item-123")
333
+
334
+ if response.success?
335
+ wire_details = response.data['wire_details']
336
+
337
+ if wire_details
338
+ puts "Wire Transfer Details:"
339
+ puts " Account Number: #{wire_details['account_number']}"
340
+ puts " Routing Number: #{wire_details['routing_number']}"
341
+ puts " Bank Name: #{wire_details['bank_name']}"
342
+ puts " Swift Code: #{wire_details['swift_code']}"
343
+ else
344
+ puts "No wire details available for this item"
345
+ end
346
+ else
347
+ puts "Error: #{response.error_message}"
348
+ end
349
+ ```
350
+
351
+ ## List Item Transactions
352
+
353
+ Get all transactions associated with an item.
354
+
355
+ ```ruby
356
+ # List transactions with default pagination
357
+ response = items.list_transactions("item-123")
358
+
359
+ if response.success?
360
+ transactions = response.data
361
+
362
+ if transactions && transactions.any?
363
+ puts "Item has #{transactions.length} transaction(s):"
364
+ transactions.each do |transaction|
365
+ puts " Transaction ID: #{transaction['id']}"
366
+ puts " Amount: #{transaction['amount']}"
367
+ puts " State: #{transaction['state']}"
368
+ puts " Type: #{transaction['type']}"
369
+ puts " Created At: #{transaction['created_at']}"
370
+ end
371
+ else
372
+ puts "No transactions found for this item"
373
+ end
374
+ else
375
+ puts "Error: #{response.error_message}"
376
+ end
377
+ ```
378
+
379
+ ### List Item Transactions with Pagination
380
+
381
+ ```ruby
382
+ # List 50 transactions starting from offset 100
383
+ response = items.list_transactions("item-123", limit: 50, offset: 100)
384
+
385
+ if response.success?
386
+ transactions = response.data
387
+ puts "Retrieved #{transactions.length} transactions"
388
+ end
389
+ ```
390
+
391
+ ## List Item Batch Transactions
392
+
393
+ Get all batch transactions associated with an item.
394
+
395
+ ```ruby
396
+ response = items.list_batch_transactions("item-123")
397
+
398
+ if response.success?
399
+ batch_transactions = response.data
400
+
401
+ if batch_transactions && batch_transactions.any?
402
+ puts "Item has #{batch_transactions.length} batch transaction(s):"
403
+ batch_transactions.each do |batch|
404
+ puts " Batch ID: #{batch['id']}"
405
+ puts " Amount: #{batch['amount']}"
406
+ puts " State: #{batch['state']}"
407
+ puts " Created At: #{batch['created_at']}"
408
+ end
409
+ else
410
+ puts "No batch transactions found for this item"
411
+ end
412
+ else
413
+ puts "Error: #{response.error_message}"
414
+ end
415
+ ```
416
+
417
+ ### List Item Batch Transactions with Pagination
418
+
419
+ ```ruby
420
+ # List 25 batch transactions starting from offset 50
421
+ response = items.list_batch_transactions("item-123", limit: 25, offset: 50)
422
+
423
+ if response.success?
424
+ batch_transactions = response.data
425
+ puts "Retrieved #{batch_transactions.length} batch transactions"
426
+ end
427
+ ```
428
+
429
+ ## Show Item Status
430
+
431
+ Get the current status of an item.
432
+
433
+ ```ruby
434
+ response = items.show_status("item-123")
435
+
436
+ if response.success?
437
+ item_status = response.data
438
+ puts "Item ID: #{item_status['id']}"
439
+ puts "State: #{item_status['state']}"
440
+ puts "Payment State: #{item_status['payment_state']}"
441
+ puts "Disbursement State: #{item_status['disbursement_state']}" if item_status['disbursement_state']
442
+ puts "Status Description: #{item_status['status_description']}" if item_status['status_description']
443
+ else
444
+ puts "Error: #{response.error_message}"
445
+ end
446
+ ```
447
+
448
+ ## Complete Workflow Example
449
+
450
+ Here's a complete example of creating an item and performing various operations on it:
451
+
452
+ ```ruby
453
+ require 'zai_payment'
454
+
455
+ # Configure
456
+ ZaiPayment.configure do |config|
457
+ config.client_id = ENV['ZAI_CLIENT_ID']
458
+ config.client_secret = ENV['ZAI_CLIENT_SECRET']
459
+ config.scope = ENV['ZAI_SCOPE']
460
+ config.environment = :prelive
461
+ end
462
+
463
+ items = ZaiPayment.items
464
+
465
+ # 1. Create an item
466
+ create_response = items.create(
467
+ name: "E-commerce Purchase",
468
+ amount: 50000, # $500.00
469
+ payment_type: 2,
470
+ buyer_id: "buyer-abc123",
471
+ seller_id: "seller-xyz789",
472
+ description: "Online store purchase - Order #12345",
473
+ currency: "AUD",
474
+ buyer_url: "https://buyer-portal.example.com",
475
+ seller_url: "https://seller-portal.example.com",
476
+ tax_invoice: true
477
+ )
478
+
479
+ if create_response.success?
480
+ item_id = create_response.data['id']
481
+ puts "✓ Item created: #{item_id}"
482
+
483
+ # 2. Get item details
484
+ show_response = items.show(item_id)
485
+ if show_response.success?
486
+ puts "✓ Item retrieved: #{show_response.data['name']}"
487
+ end
488
+
489
+ # 3. Get seller details
490
+ seller_response = items.show_seller(item_id)
491
+ if seller_response.success?
492
+ seller = seller_response.data
493
+ puts "✓ Seller: #{seller['email']}"
494
+ end
495
+
496
+ # 4. Get buyer details
497
+ buyer_response = items.show_buyer(item_id)
498
+ if buyer_response.success?
499
+ buyer = buyer_response.data
500
+ puts "✓ Buyer: #{buyer['email']}"
501
+ end
502
+
503
+ # 5. Check item status
504
+ status_response = items.show_status(item_id)
505
+ if status_response.success?
506
+ status = status_response.data
507
+ puts "✓ Item status: #{status['state']}"
508
+ end
509
+
510
+ # 6. List transactions
511
+ transactions_response = items.list_transactions(item_id)
512
+ if transactions_response.success?
513
+ txn_count = transactions_response.data&.length || 0
514
+ puts "✓ Item has #{txn_count} transaction(s)"
515
+ end
516
+
517
+ # 7. Update item if needed
518
+ update_response = items.update(
519
+ item_id,
520
+ description: "Updated: Online store purchase - Order #12345 (Confirmed)",
521
+ tax_invoice: true
522
+ )
523
+ if update_response.success?
524
+ puts "✓ Item updated successfully"
525
+ end
526
+ else
527
+ puts "✗ Error creating item: #{create_response.error_message}"
528
+ end
529
+ ```
530
+
531
+ ## Error Handling
532
+
533
+ Always check the response status and handle errors appropriately:
534
+
535
+ ```ruby
536
+ response = items.show("item-123")
537
+
538
+ if response.success?
539
+ # Handle successful response
540
+ item = response.data
541
+ puts "Item: #{item['name']}"
542
+ else
543
+ # Handle error
544
+ case response.status
545
+ when 404
546
+ puts "Item not found"
547
+ when 401
548
+ puts "Authentication failed - check your credentials"
549
+ when 422
550
+ puts "Validation error: #{response.error_message}"
551
+ else
552
+ puts "Error: #{response.error_message}"
553
+ end
554
+ end
555
+ ```
556
+
557
+ ## Payment Types
558
+
559
+ When creating items, you can specify different payment types:
560
+
561
+ - **1**: Direct Debit
562
+ - **2**: Credit Card (default)
563
+ - **3**: Bank Transfer
564
+ - **4**: Wallet
565
+ - **5**: BPay
566
+ - **6**: PayPal
567
+ - **7**: Other
568
+
569
+ Example:
570
+
571
+ ```ruby
572
+ # Create item with bank transfer payment type
573
+ response = items.create(
574
+ name: "Bank Transfer Payment",
575
+ amount: 30000,
576
+ payment_type: 3, # Bank Transfer
577
+ buyer_id: "buyer-123",
578
+ seller_id: "seller-456"
579
+ )
580
+ ```
581
+
582
+ ## API Reference
583
+
584
+ For more information about the Zai Items API, see:
585
+
586
+ - [Create Item](https://developer.hellozai.com/reference/createitem)
587
+ - [List Items](https://developer.hellozai.com/reference/listitems)
588
+ - [Show Item](https://developer.hellozai.com/reference/showitem)
589
+ - [Update Item](https://developer.hellozai.com/reference/updateitem)
590
+ - [Delete Item](https://developer.hellozai.com/reference/deleteitem)
591
+ - [Show Item Seller](https://developer.hellozai.com/reference/showitemseller)
592
+ - [Show Item Buyer](https://developer.hellozai.com/reference/showitembuyer)
593
+ - [Show Item Fees](https://developer.hellozai.com/reference/showitemfees)
594
+ - [Show Item Wire Details](https://developer.hellozai.com/reference/showitemwiredetails)
595
+ - [List Item Transactions](https://developer.hellozai.com/reference/listitemtransactions)
596
+ - [List Item Batch Transactions](https://developer.hellozai.com/reference/listitembatchtransactions)
597
+ - [Show Item Status](https://developer.hellozai.com/reference/showitemstatus)
598
+