Class: TicketService::OldForm

Inherits:
ApplicationService
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/services/ticket_service/old_form.rb

Overview

TODO remove this class after new system fully implemented

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(params, user = nil) ⇒ OldForm

Returns a new instance of OldForm.



6
7
8
9
# File 'app/services/ticket_service/old_form.rb', line 6

def initialize(params, user = nil)
  @user   = user
  @params = params
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



4
5
6
# File 'app/services/ticket_service/old_form.rb', line 4

def code
  @code
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'app/services/ticket_service/old_form.rb', line 4

def params
  @params
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'app/services/ticket_service/old_form.rb', line 4

def user
  @user
end

Instance Method Details

#alipay?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'app/services/ticket_service/old_form.rb', line 142

def alipay?
  payment_type_param.to_s == 'alipay_plus'
end

#cc_tokenObject



126
127
128
# File 'app/services/ticket_service/old_form.rb', line 126

def cc_token
  omise_token_param.presence || gb_primepay_card_param
end

#credit_card?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'app/services/ticket_service/old_form.rb', line 150

def credit_card?
  payment_type_param.to_s == 'credit_card'
end

#gb_primepay_card_paramObject



158
159
160
# File 'app/services/ticket_service/old_form.rb', line 158

def gb_primepay_card_param
  params[:gb_primepay_card]
end

#omise_token_paramObject



162
163
164
# File 'app/services/ticket_service/old_form.rb', line 162

def omise_token_param
  params[:omise_token]
end

#payment_type_paramObject



154
155
156
# File 'app/services/ticket_service/old_form.rb', line 154

def payment_type_param
  params[:payment_type]
end

#promptpay?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/services/ticket_service/old_form.rb', line 130

def promptpay?
  payment_type_param.to_s == 'promptpay'
end

#shopee_pay?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'app/services/ticket_service/old_form.rb', line 138

def shopee_pay?
  payment_type_param.to_s == 'shopee_pay'
end

#transaction_attributesObject

Raises:

  • (InvalidVoucherPurchase)


114
115
116
117
118
119
120
121
122
123
124
# File 'app/services/ticket_service/old_form.rb', line 114

def transaction_attributes
  raise InvalidVoucherPurchase unless valid?

  builded_tickets = build_tickets
  transaction_attr = ticket_transaction_params
  transaction_attr.merge(tickets_attributes: builded_tickets[:ticket_attrs]).
    merge(ticket_bundles_attributes: builded_tickets[:ticket_bundle_attrs]).
    merge(total_price: builded_tickets[:total_price_transaction]).
    merge(guest: guest_user).
    merge(user: user)
end

#true_wallet?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/services/ticket_service/old_form.rb', line 134

def true_wallet?
  payment_type_param.to_s == 'true_wallet'
end

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
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
61
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/services/ticket_service/old_form.rb', line 11

def valid?
  errors.clear

  if ticket_group_params.blank?
    errors.add(:base, 'Invalid ticket')
    return false
  end

  if guest_user_param.blank? && user.blank?
    errors.add(:base, 'Invalid params user or guest')
    return false
  end

  unless %i[promptpay true_wallet shopee_pay credit_card alipay_plus wechat_pay].include?(payment_type_param.to_sym)
    errors.add(:base, 'Invalid Payment params')
    return false
  end

  ticket_group_params.each do |t|
    ticket_group = TicketGroup.find_by(id: t[:id])
    limit_qty_per_user = ticket_group.limit_qty_per_user.to_i
    if ticket_group.blank?
      errors.add(:base, 'Invalid ticket')
      return false
    end

    if ticket_group.total_orders.to_i >= ticket_group.quantity.to_i
      errors.add(:base, "#{ticket_group.name} voucher sales quantity, has reached the limit")
      return false
    end

    if ticket_group.limit_per_order.to_i.positive? && ticket_group.limit_per_order.to_i < t[:quantity].to_i
      errors.add(:base, "#{ticket_group.name} has #{ticket_group.limit_per_order} limit quantity per order")
      return false
    end

    if ticket_group.partner_ticket_enabled?
      available_tickets = ticket_group.partner_ticket_codes.available
      on_keep_tickets = ticket_group.partner_ticket_codes.on_keep

      if available_tickets.blank? && on_keep_tickets.blank?
        errors.add(:base, "Sorry, #{ticket_group.name} vouchers have been sold")
        return false
      end

      if available_tickets.blank? && on_keep_tickets.present?
        errors.add(:base, "Sorry, #{ticket_group.name} vouchers have been fully booked")
        return false
      end

      quota = available_tickets.count
      if quota < t[:quantity].to_i
        errors.add(:base,
                   "Sorry, the quantity is not sufficient. #{ticket_group.name} has only #{quota} remaining vouchers.")
        return false
      end
    end

    if ticket_group.limit_user? && user.blank?
      errors.add(:base, 'Please Log in/Sign up to buy a voucher')
      return false
    end

    if limit_qty_per_user.positive? && t[:quantity].to_i > limit_qty_per_user
      errors.add(:base,
                 "You have reached maximum number of this voucher. Maximum #{limit_qty_per_user} vouchers per user")
      return false
    end

    if reached_user_limit_order?(ticket_group, t[:quantity].to_i)
      errors.add(:base, 'You have reached maximum number of this voucher')
      return false
    end

    time_zone = restaurant.persisted? ? restaurant.time_zone : Time::THAILAND_ZONE
    today = Time.now_in_tz(time_zone)

    if ticket_group.selling_end_date < today
      errors.add(:base, "Sorry, sale period for voucher #{ticket_group.name} has ended")
      return false
    end

    if ticket_group.selling_start_date > today
      errors.add(:base, "Sorry, sale period for voucher #{ticket_group.name} hasn't started")
      return false
    end
  end

  if payment_type_param.blank? || (!promptpay? && !true_wallet? && !shopee_pay? && !credit_card? && !alipay? && !wechat_pay?)
    errors.add(:base, 'Transaction failed, please select payment type to continue')
    return false
  end

  if credit_card? && gb_primepay_card_param.present? && gb_primepay_card_param[:token].blank?
    errors.add(:base, 'Something went wrong when processing your credit card, please re-enter your credit card')
    return false
  end

  return false if errors.present?

  true
end

#wechat_pay?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/services/ticket_service/old_form.rb', line 146

def wechat_pay?
  payment_type_param.to_s == 'wechat_pay'
end