Class: Transactions::TicketTransaction

Inherits:
BaseOperationService
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/services/payment_process_service/transactions/ticket_transaction.rb

Defined Under Namespace

Classes: DuplicateChargeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(ticket_transaction_id, by_admin = false) ⇒ TicketTransaction

Returns a new instance of TicketTransaction.



11
12
13
14
15
16
# File 'app/services/payment_process_service/transactions/ticket_transaction.rb', line 11

def initialize(ticket_transaction_id, by_admin = false)
  @ticket_transaction = TicketTransaction.
    includes(tickets: [:ticket_group, :package_type, :partner_ticket_code]).
    find_by(id: ticket_transaction_id)
  @by_admin = by_admin
end

Instance Attribute Details

#ticket_transactionObject

Returns the value of attribute ticket_transaction.



8
9
10
# File 'app/services/payment_process_service/transactions/ticket_transaction.rb', line 8

def ticket_transaction
  @ticket_transaction
end

Instance Method Details

#cancelled!(reason = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/payment_process_service/transactions/ticket_transaction.rb', line 62

def cancelled!(reason = nil)
  return unless ticket_transaction.active?
  return if ticket_transaction.charges.success_scope.present? && !vendor_prepaid

  reason = 'cancel booking because didnt pay' if reason.blank?
  restore_quota_params = {}
  success = false

  ActiveRecord::Base.transaction do
    ticket_transaction.lock!
    ticket_transaction.audit_comment = reason
    ticket_transaction.active = false
    ticket_transaction.mark_tickets_as_inactive!
    ticket_transaction.mark_partner_tickets_as_available!
    ticket_transaction.mark_paid_charges_as_refunded! if vendor_prepaid
    ticket_transaction.save!(validate: false)

    ticket_transaction.ticket_bundles.includes(:ticket_group).each do |ticket_bundle|
      ticket_group = ticket_bundle.ticket_group
      booked_ids = JSON.parse ticket_bundle.booked_quantity
      restore_quota_params[ticket_group.id] ||= []
      restore_quota_params[ticket_group.id].push booked_ids
    end
    success = true
  end

  if success
    restore_quota_params.each do |ticket_group_id, booked_ids|
      Workers::TicketTransactions::RestoreQuotaWorker.perform_async(ticket_group_id, booked_ids.flatten)
    end
  end
end

#complete!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/payment_process_service/transactions/ticket_transaction.rb', line 18

def complete!
  result = true

  ActiveRecord::Base.transaction do
    if ticket_transaction.blank?
      errors.add :base, 'Failed to complete process, ticket transaction not found'
      raise ActiveRecord::Rollback
    end

    if !by_admin
      ticket_transaction.ticket_bundles.includes(:ticket_group).each do |ticket_bundle|
        ticket_group = ticket_bundle.ticket_group
        # prevent overbook when VIM is not available after user has paid
        if ticket_group.is_full?
          HH_LOGGER.error('User has paid but the VIM quantity is not available')
          errors.add :base, "We're sorry this product is not available anymore"
          result = false
          raise ActiveRecord::Rollback
        end
      end
    end

    ticket_transaction.audit_comment = 'Marked as paid by admin' if by_admin
    ticket_transaction.active = true

    unless ticket_transaction.save
      errors.add :base, 'Failed updating ticket transaction data'
      raise ActiveRecord::Rollback
    end

    ticket_transaction.mark_tickets_as_active!
    ticket_transaction.mark_partner_tickets_as_sold!
    ticket_transaction.touch
  end

  if result
    send_notification
    give_reward_booking
    calculate_loyalty_spending
  end

  result
end