Class: VoucherService::CreateTransaction

Inherits:
ApplicationService show all
Includes:
DefaultErrorContainer, ElasticAPM::SpanHelpers
Defined in:
app/services/voucher_service/create_transaction.rb

Defined Under Namespace

Classes: VoucherTransactionAdapter

Constant Summary collapse

SUPPORTED_CURRENCIES =

Supported currencies for voucher transactions Using constants from Country model to ensure consistency

[
  Country::THAI_CURRENCY_CODE,
  Country::SINGAPORE_CURRENCY_CODE,
  Country::MALAYSIA_CURRENCY_CODE,
].freeze

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple

Methods inherited from ApplicationService

#execute!

Constructor Details

#initialize(params) ⇒ CreateTransaction

Returns a new instance of CreateTransaction.



14
15
16
# File 'app/services/voucher_service/create_transaction.rb', line 14

def initialize(params)
  @params = params
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'app/services/voucher_service/create_transaction.rb', line 4

def params
  @params
end

Instance Method Details

#executeObject



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
# File 'app/services/voucher_service/create_transaction.rb', line 18

def execute
  ActiveRecord::Base.transaction do
    transaction_payload = {
      payment_type: payment_type_param,
    }.merge(voucher_owner_attr)

    transaction = VoucherTransaction.new transaction_payload
    transaction.vouchers << build_vouchers

    # Set currency before validation
    transaction.total_price_currency = currency_code

    validate

    return ServiceResult.new(errors: errors, message: error_message_simple) if errors.present?

    transaction.set_total_price
    use_3d_secure = params[:use_3d_secure].to_s == 'true'

    reject_cc_payment unless use_3d_secure || omise_token_param.present?

    unless transaction.save
      merge_errors(transaction.errors)
      raise InvalidVoucherPurchase
    end

    # initialize firebase
    update_firebase(transaction)

    charge_transaction(transaction)

    unless transaction.save
      merge_errors(transaction.errors)
      raise InvalidVoucherPurchase
    end

    # update as paid if charge success
    update_firebase(transaction)
    update_transaction_status(transaction)

    return ServiceResult.new(errors: errors, message: error_message_simple) if errors.present?

    ServiceResult.new data: transaction
  end
rescue InvalidVoucherPurchase
  ServiceResult.new errors: errors, message: error_message_simple
end