zai_payment 2.4.0 → 2.6.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,642 @@
1
+ # BPay Account Management Examples
2
+
3
+ This document provides practical examples for managing BPay accounts in Zai Payment.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Setup](#setup)
8
+ - [Show BPay Account Example](#show-bpay-account-example)
9
+ - [Show BPay Account User Example](#show-bpay-account-user-example)
10
+ - [Redact BPay Account Example](#redact-bpay-account-example)
11
+ - [Create BPay Account Example](#create-bpay-account-example)
12
+ - [Common Patterns](#common-patterns)
13
+
14
+ ## Setup
15
+
16
+ ```ruby
17
+ require 'zai_payment'
18
+
19
+ # Configure ZaiPayment
20
+ ZaiPayment.configure do |config|
21
+ config.environment = :prelive # or :production
22
+ config.client_id = ENV['ZAI_CLIENT_ID']
23
+ config.client_secret = ENV['ZAI_CLIENT_SECRET']
24
+ config.scope = ENV['ZAI_SCOPE']
25
+ end
26
+ ```
27
+
28
+ ## Show BPay Account Example
29
+
30
+ ### Example 1: Get BPay Account Details
31
+
32
+ Retrieve details of a specific BPay account.
33
+
34
+ ```ruby
35
+ # Get BPay account details
36
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
37
+
38
+ response = bpay_accounts.show('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
39
+
40
+ if response.success?
41
+ bpay_account = response.data
42
+ puts "BPay Account ID: #{bpay_account['id']}"
43
+ puts "Active: #{bpay_account['active']}"
44
+ puts "Verification Status: #{bpay_account['verification_status']}"
45
+ puts "Currency: #{bpay_account['currency']}"
46
+ puts "Created At: #{bpay_account['created_at']}"
47
+ puts "Updated At: #{bpay_account['updated_at']}"
48
+
49
+ # Access BPay details
50
+ bpay_details = bpay_account['bpay_details']
51
+ puts "\nBPay Details:"
52
+ puts " Account Name: #{bpay_details['account_name']}"
53
+ puts " Biller Code: #{bpay_details['biller_code']}"
54
+ puts " Biller Name: #{bpay_details['biller_name']}"
55
+ puts " CRN: #{bpay_details['crn']}"
56
+
57
+ # Access links
58
+ links = bpay_account['links']
59
+ puts "\nLinks:"
60
+ puts " Self: #{links['self']}"
61
+ puts " Users: #{links['users']}"
62
+ else
63
+ puts "Failed to retrieve BPay account"
64
+ puts "Error: #{response.error}"
65
+ end
66
+ ```
67
+
68
+ ### Example 2: Show BPay Account with Error Handling
69
+
70
+ Handle edge cases when retrieving BPay account details.
71
+
72
+ ```ruby
73
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
74
+
75
+ begin
76
+ response = bpay_accounts.show('bpay_account_id_here')
77
+
78
+ if response.success?
79
+ bpay_account = response.data
80
+
81
+ # Check if account is active
82
+ if bpay_account['active']
83
+ puts "BPay account is active"
84
+ puts "Biller: #{bpay_account['bpay_details']['biller_name']}"
85
+ puts "CRN: #{bpay_account['bpay_details']['crn']}"
86
+ else
87
+ puts "BPay account is inactive"
88
+ end
89
+
90
+ # Check verification status
91
+ case bpay_account['verification_status']
92
+ when 'verified'
93
+ puts "Account is verified and ready to use"
94
+ when 'not_verified'
95
+ puts "Account pending verification"
96
+ when 'pending_verification'
97
+ puts "Account verification in progress"
98
+ end
99
+ else
100
+ puts "Failed to retrieve BPay account: #{response.error}"
101
+ end
102
+ rescue ZaiPayment::Errors::NotFoundError => e
103
+ puts "BPay account not found: #{e.message}"
104
+ rescue ZaiPayment::Errors::ValidationError => e
105
+ puts "Invalid BPay account ID: #{e.message}"
106
+ rescue ZaiPayment::Errors::ApiError => e
107
+ puts "API error occurred: #{e.message}"
108
+ end
109
+ ```
110
+
111
+ ### Example 3: Verify BPay Account Details Before Processing
112
+
113
+ Check BPay account details before initiating a disbursement.
114
+
115
+ ```ruby
116
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
117
+
118
+ # Step 1: Retrieve BPay account
119
+ response = bpay_accounts.show('bpay_account_id')
120
+
121
+ if response.success?
122
+ bpay_account = response.data
123
+
124
+ # Step 2: Verify account is ready for disbursement
125
+ if bpay_account['active'] && bpay_account['verification_status'] == 'verified'
126
+ bpay_details = bpay_account['bpay_details']
127
+
128
+ puts "Ready to disburse to:"
129
+ puts " Biller: #{bpay_details['biller_name']}"
130
+ puts " Account: #{bpay_details['account_name']}"
131
+ puts " CRN: #{bpay_details['crn']}"
132
+
133
+ # Proceed with disbursement
134
+ # items.make_payment(...)
135
+ else
136
+ puts "Cannot disburse: Account not ready"
137
+ puts " Active: #{bpay_account['active']}"
138
+ puts " Status: #{bpay_account['verification_status']}"
139
+ end
140
+ end
141
+ ```
142
+
143
+ ## Show BPay Account User Example
144
+
145
+ ### Example 1: Get User Associated with BPay Account
146
+
147
+ Retrieve user details for a BPay account.
148
+
149
+ ```ruby
150
+ # Get user associated with BPay account
151
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
152
+
153
+ response = bpay_accounts.show_user('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
154
+
155
+ if response.success?
156
+ user = response.data
157
+ puts "User ID: #{user['id']}"
158
+ puts "Full Name: #{user['full_name']}"
159
+ puts "Email: #{user['email']}"
160
+ puts "First Name: #{user['first_name']}"
161
+ puts "Last Name: #{user['last_name']}"
162
+ puts "Location: #{user['location']}"
163
+ puts "Verification State: #{user['verification_state']}"
164
+ puts "Held State: #{user['held_state']}"
165
+ puts "Roles: #{user['roles'].join(', ')}"
166
+
167
+ # Access links
168
+ links = user['links']
169
+ puts "\nLinks:"
170
+ puts " Self: #{links['self']}"
171
+ puts " Items: #{links['items']}"
172
+ puts " Wallet Accounts: #{links['wallet_accounts']}"
173
+ else
174
+ puts "Failed to retrieve user"
175
+ puts "Error: #{response.error}"
176
+ end
177
+ ```
178
+
179
+ ### Example 2: Verify User Before Disbursement
180
+
181
+ Check user details before processing a disbursement to a BPay account.
182
+
183
+ ```ruby
184
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
185
+
186
+ begin
187
+ # Step 1: Get user associated with BPay account
188
+ user_response = bpay_accounts.show_user('bpay_account_id')
189
+
190
+ if user_response.success?
191
+ user = user_response.data
192
+
193
+ # Step 2: Verify user details
194
+ if user['verification_state'] == 'verified' && !user['held_state']
195
+ puts "User verified and not on hold"
196
+ puts "Name: #{user['full_name']}"
197
+ puts "Email: #{user['email']}"
198
+
199
+ # Proceed with disbursement
200
+ puts "✓ Ready to process disbursement"
201
+ else
202
+ puts "Cannot disburse:"
203
+ puts " Verification: #{user['verification_state']}"
204
+ puts " On Hold: #{user['held_state']}"
205
+ end
206
+ end
207
+ rescue ZaiPayment::Errors::NotFoundError => e
208
+ puts "BPay account not found: #{e.message}"
209
+ rescue ZaiPayment::Errors::ApiError => e
210
+ puts "API error: #{e.message}"
211
+ end
212
+ ```
213
+
214
+ ### Example 3: Get User Contact Information
215
+
216
+ Retrieve user contact details for notifications.
217
+
218
+ ```ruby
219
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
220
+
221
+ response = bpay_accounts.show_user('bpay_account_id')
222
+
223
+ if response.success?
224
+ user = response.data
225
+
226
+ # Extract contact information
227
+ contact_info = {
228
+ name: user['full_name'],
229
+ email: user['email'],
230
+ mobile: user['mobile'],
231
+ location: user['location']
232
+ }
233
+
234
+ puts "Contact Information:"
235
+ puts " Name: #{contact_info[:name]}"
236
+ puts " Email: #{contact_info[:email]}"
237
+ puts " Mobile: #{contact_info[:mobile]}"
238
+ puts " Location: #{contact_info[:location]}"
239
+
240
+ # Send notification
241
+ # NotificationService.send_disbursement_notice(contact_info)
242
+ end
243
+ ```
244
+
245
+ ## Redact BPay Account Example
246
+
247
+ ### Example 1: Redact a BPay Account
248
+
249
+ Redact (deactivate) a BPay account so it can no longer be used.
250
+
251
+ ```ruby
252
+ # Redact a BPay account
253
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
254
+
255
+ response = bpay_accounts.redact('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
256
+
257
+ if response.success?
258
+ puts "BPay account successfully redacted"
259
+ puts "Response: #{response.data}"
260
+ # => {"bpay_account"=>"Successfully redacted"}
261
+
262
+ # The BPay account can no longer be used for:
263
+ # - Disbursement destination
264
+ else
265
+ puts "Failed to redact BPay account"
266
+ puts "Error: #{response.error}"
267
+ end
268
+ ```
269
+
270
+ ### Example 2: Redact with Error Handling
271
+
272
+ Handle edge cases when redacting a BPay account.
273
+
274
+ ```ruby
275
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
276
+
277
+ begin
278
+ # Attempt to redact the BPay account
279
+ response = bpay_accounts.redact('bpay_account_id_here')
280
+
281
+ if response.success?
282
+ puts "BPay account redacted successfully"
283
+
284
+ # Log the action for audit purposes
285
+ Rails.logger.info("BPay account #{bpay_account_id} was redacted at #{Time.now}")
286
+ else
287
+ puts "Redaction failed: #{response.error}"
288
+ end
289
+ rescue ZaiPayment::Errors::NotFoundError => e
290
+ puts "BPay account not found: #{e.message}"
291
+ rescue ZaiPayment::Errors::ValidationError => e
292
+ puts "Invalid BPay account ID: #{e.message}"
293
+ rescue ZaiPayment::Errors::ApiError => e
294
+ puts "API error occurred: #{e.message}"
295
+ end
296
+ ```
297
+
298
+ ### Example 3: Verify Before Redacting
299
+
300
+ Verify BPay account details before redacting.
301
+
302
+ ```ruby
303
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
304
+
305
+ # Step 1: Retrieve BPay account to verify
306
+ response = bpay_accounts.show('bpay_account_id')
307
+
308
+ if response.success?
309
+ bpay_account = response.data
310
+
311
+ # Step 2: Confirm details before redacting
312
+ puts "About to redact:"
313
+ puts " Account: #{bpay_account['bpay_details']['account_name']}"
314
+ puts " Biller: #{bpay_account['bpay_details']['biller_name']}"
315
+ puts " Active: #{bpay_account['active']}"
316
+
317
+ # Step 3: Redact the account
318
+ redact_response = bpay_accounts.redact('bpay_account_id')
319
+
320
+ if redact_response.success?
321
+ puts "\n✓ BPay account redacted successfully"
322
+ else
323
+ puts "\n✗ Failed to redact BPay account"
324
+ end
325
+ end
326
+ ```
327
+
328
+ ## Create BPay Account Example
329
+
330
+ ### Example 1: Create Basic BPay Account
331
+
332
+ Create a BPay account for disbursement destination.
333
+
334
+ ```ruby
335
+ # Create a BPay account
336
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
337
+
338
+ response = bpay_accounts.create(
339
+ user_id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
340
+ account_name: 'My Water Bill Company',
341
+ biller_code: 123456,
342
+ bpay_crn: '987654321'
343
+ )
344
+
345
+ if response.success?
346
+ bpay_account = response.data
347
+ puts "BPay Account ID: #{bpay_account['id']}"
348
+ puts "Active: #{bpay_account['active']}"
349
+ puts "Verification Status: #{bpay_account['verification_status']}"
350
+ puts "Currency: #{bpay_account['currency']}"
351
+
352
+ # Access BPay details
353
+ bpay_details = bpay_account['bpay_details']
354
+ puts "\nBPay Details:"
355
+ puts " Account Name: #{bpay_details['account_name']}"
356
+ puts " Biller Code: #{bpay_details['biller_code']}"
357
+ puts " Biller Name: #{bpay_details['biller_name']}"
358
+ puts " CRN: #{bpay_details['crn']}"
359
+
360
+ # Access links
361
+ links = bpay_account['links']
362
+ puts "\nLinks:"
363
+ puts " Self: #{links['self']}"
364
+ puts " Users: #{links['users']}"
365
+ else
366
+ puts "Failed to create BPay account"
367
+ puts "Error: #{response.error}"
368
+ end
369
+ ```
370
+
371
+ ### Example 2: Create BPay Account with Error Handling
372
+
373
+ Handle validation errors when creating BPay accounts.
374
+
375
+ ```ruby
376
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
377
+
378
+ begin
379
+ response = bpay_accounts.create(
380
+ user_id: 'user_123',
381
+ account_name: 'My Electricity Bill',
382
+ biller_code: 456789,
383
+ bpay_crn: '123456789'
384
+ )
385
+
386
+ if response.success?
387
+ puts "BPay account created successfully!"
388
+ puts "Account ID: #{response.data['id']}"
389
+ else
390
+ puts "Failed to create BPay account: #{response.error}"
391
+ end
392
+ rescue ZaiPayment::Errors::ValidationError => e
393
+ puts "Validation error: #{e.message}"
394
+ rescue ZaiPayment::Errors::ApiError => e
395
+ puts "API error: #{e.message}"
396
+ puts "Status code: #{e.status_code}"
397
+ end
398
+ ```
399
+
400
+ ### Example 3: Create BPay Account for Utility Payment
401
+
402
+ Create a BPay account for utility bill payments.
403
+
404
+ ```ruby
405
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
406
+
407
+ # Water bill
408
+ response = bpay_accounts.create(
409
+ user_id: 'user_456',
410
+ account_name: 'Sydney Water Bill',
411
+ biller_code: 12345,
412
+ bpay_crn: '1122334455'
413
+ )
414
+
415
+ if response.success?
416
+ puts "Water bill BPay account created: #{response.data['id']}"
417
+ end
418
+
419
+ # Electricity bill
420
+ response = bpay_accounts.create(
421
+ user_id: 'user_456',
422
+ account_name: 'Energy Australia Bill',
423
+ biller_code: 67890,
424
+ bpay_crn: '9988776655'
425
+ )
426
+
427
+ if response.success?
428
+ puts "Electricity BPay account created: #{response.data['id']}"
429
+ end
430
+ ```
431
+
432
+ ## Common Patterns
433
+
434
+ ### Pattern 1: Retrieve and Verify BPay Account Before Disbursement
435
+
436
+ Check BPay account status before processing a payment.
437
+
438
+ ```ruby
439
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
440
+
441
+ # Step 1: Retrieve BPay account
442
+ response = bpay_accounts.show('bpay_account_id')
443
+
444
+ if response.success?
445
+ bpay_account = response.data
446
+
447
+ # Step 2: Verify account is ready
448
+ if bpay_account['active'] && bpay_account['verification_status'] == 'verified'
449
+ puts "BPay account is ready for disbursement"
450
+
451
+ # Step 3: Proceed with payment
452
+ items = ZaiPayment::Resources::Item.new
453
+ payment_response = items.make_payment(
454
+ item_id: 'item_123',
455
+ account_id: bpay_account['id']
456
+ )
457
+
458
+ puts "Payment initiated" if payment_response.success?
459
+ else
460
+ puts "Account not ready: #{bpay_account['verification_status']}"
461
+ end
462
+ end
463
+ ```
464
+
465
+ ### Pattern 2: Creating BPay Account After User Registration
466
+
467
+ Typical workflow when setting up disbursement accounts.
468
+
469
+ ```ruby
470
+ # Step 1: Create a payout user (seller)
471
+ users = ZaiPayment::Resources::User.new
472
+
473
+ user_response = users.create(
474
+ user_type: 'payout',
475
+ email: 'seller@example.com',
476
+ first_name: 'Sarah',
477
+ last_name: 'Seller',
478
+ country: 'AUS',
479
+ dob: '15/01/1990',
480
+ address_line1: '123 Market St',
481
+ city: 'Sydney',
482
+ state: 'NSW',
483
+ zip: '2000',
484
+ mobile: '+61412345678'
485
+ )
486
+
487
+ user_id = user_response.data['id']
488
+ puts "User created: #{user_id}"
489
+
490
+ # Step 2: Create BPay account for the seller
491
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
492
+
493
+ bpay_response = bpay_accounts.create(
494
+ user_id: user_id,
495
+ account_name: 'My Service Bill',
496
+ biller_code: 123456,
497
+ bpay_crn: '987654321'
498
+ )
499
+
500
+ if bpay_response.success?
501
+ bpay_account_id = bpay_response.data['id']
502
+ puts "BPay account created: #{bpay_account_id}"
503
+
504
+ # Step 3: Set as disbursement account
505
+ users.set_disbursement_account(user_id, bpay_account_id)
506
+ puts "Disbursement account set successfully"
507
+ end
508
+ ```
509
+
510
+ ### Pattern 2: BPay Account Form Handler
511
+
512
+ Implement BPay account creation in a Rails controller.
513
+
514
+ ```ruby
515
+ # In a Rails controller
516
+ class BpayAccountsController < ApplicationController
517
+ def create
518
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
519
+
520
+ begin
521
+ response = bpay_accounts.create(
522
+ user_id: params[:user_id],
523
+ account_name: params[:account_name],
524
+ biller_code: params[:biller_code].to_i,
525
+ bpay_crn: params[:bpay_crn]
526
+ )
527
+
528
+ if response.success?
529
+ bpay_account = response.data
530
+
531
+ render json: {
532
+ success: true,
533
+ bpay_account_id: bpay_account['id'],
534
+ message: 'BPay account created successfully'
535
+ }, status: :created
536
+ else
537
+ render json: {
538
+ success: false,
539
+ message: response.error
540
+ }, status: :unprocessable_entity
541
+ end
542
+ rescue ZaiPayment::Errors::ValidationError => e
543
+ render json: {
544
+ success: false,
545
+ message: e.message
546
+ }, status: :bad_request
547
+ rescue ZaiPayment::Errors::ApiError => e
548
+ render json: {
549
+ success: false,
550
+ message: 'An error occurred while creating the BPay account'
551
+ }, status: :internal_server_error
552
+ end
553
+ end
554
+ end
555
+ ```
556
+
557
+ ### Pattern 3: Validate BPay Details Before Creating Account
558
+
559
+ Validate biller code and CRN format before API call.
560
+
561
+ ```ruby
562
+ class BpayAccountValidator
563
+ def self.validate_biller_code(biller_code)
564
+ biller_code_str = biller_code.to_s
565
+
566
+ unless biller_code_str.match?(/\A\d{3,10}\z/)
567
+ return { valid: false, message: 'Biller code must be 3 to 10 digits' }
568
+ end
569
+
570
+ { valid: true }
571
+ end
572
+
573
+ def self.validate_bpay_crn(bpay_crn)
574
+ bpay_crn_str = bpay_crn.to_s
575
+
576
+ unless bpay_crn_str.match?(/\A\d{2,20}\z/)
577
+ return { valid: false, message: 'CRN must be between 2 and 20 digits' }
578
+ end
579
+
580
+ { valid: true }
581
+ end
582
+
583
+ def self.validate_all(biller_code, bpay_crn)
584
+ biller_validation = validate_biller_code(biller_code)
585
+ return biller_validation unless biller_validation[:valid]
586
+
587
+ crn_validation = validate_bpay_crn(bpay_crn)
588
+ return crn_validation unless crn_validation[:valid]
589
+
590
+ { valid: true }
591
+ end
592
+ end
593
+
594
+ # Usage
595
+ validation = BpayAccountValidator.validate_all(123456, '987654321')
596
+
597
+ if validation[:valid]
598
+ bpay_accounts = ZaiPayment::Resources::BpayAccount.new
599
+ response = bpay_accounts.create(
600
+ user_id: 'user_123',
601
+ account_name: 'My Bill',
602
+ biller_code: 123456,
603
+ bpay_crn: '987654321'
604
+ )
605
+
606
+ puts "BPay account created: #{response.data['id']}" if response.success?
607
+ else
608
+ puts "Validation failed: #{validation[:message]}"
609
+ end
610
+ ```
611
+
612
+ ## Important Notes
613
+
614
+ 1. **Required Fields**:
615
+ - `user_id` - The user ID associated with the BPay account
616
+ - `account_name` - Nickname for the BPay account
617
+ - `biller_code` - 3 to 10 digits
618
+ - `bpay_crn` - Customer Reference Number, 2 to 20 digits
619
+
620
+ 2. **Biller Code Validation**:
621
+ - Must be numeric
622
+ - Must be between 3 and 10 digits
623
+ - Example: 123456
624
+
625
+ 3. **BPay CRN Validation**:
626
+ - Must be numeric
627
+ - Must be between 2 and 20 digits
628
+ - Example: 987654321
629
+
630
+ 4. **BPay Account Usage**:
631
+ - Used as a disbursement destination
632
+ - Store the returned `:id` for future use
633
+ - Can be set as default disbursement account
634
+
635
+ 5. **Currency**:
636
+ - BPay accounts are typically in AUD (Australian Dollars)
637
+ - Currency is set automatically based on the marketplace configuration
638
+
639
+ 6. **Verification Status**:
640
+ - New BPay accounts typically have `verification_status: "not_verified"`
641
+ - Verification is handled by Zai
642
+