Class: Payment::Gateway

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer, PaymentTypes
Defined in:
app/my_lib/payment/gateway.rb

Constant Summary

Constants included from PaymentTypes

PaymentTypes::ALIPAY_PLUS, PaymentTypes::ALL, PaymentTypes::CREDIT_CARD, PaymentTypes::PROMPTPAY, PaymentTypes::SHOPEE_PAY, PaymentTypes::TRUE_WALLET, PaymentTypes::VENDOR_PAYMENT, PaymentTypes::WECHAT_PAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaymentTypes

valid?

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(transaction: nil, provider: nil, payment_type: nil, options: {}) ⇒ Gateway

Returns a new instance of Gateway.



11
12
13
14
15
16
# File 'app/my_lib/payment/gateway.rb', line 11

def initialize(transaction: nil, provider: nil, payment_type: nil, options: {})
  @transaction  = transaction
  @provider     = find_provider(provider)
  @payment_type = find_payment_type(payment_type)
  @options      = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'app/my_lib/payment/gateway.rb', line 6

def options
  @options
end

#payment_typeObject

Returns the value of attribute payment_type.



6
7
8
# File 'app/my_lib/payment/gateway.rb', line 6

def payment_type
  @payment_type
end

#providerObject

Returns the value of attribute provider.



6
7
8
# File 'app/my_lib/payment/gateway.rb', line 6

def provider
  @provider
end

#transactionObject

Returns the value of attribute transaction.



6
7
8
# File 'app/my_lib/payment/gateway.rb', line 6

def transaction
  @transaction
end

Instance Method Details

#charge(amount) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/my_lib/payment/gateway.rb', line 18

def charge(amount)
  return false unless valid?

  success = false
  ActiveRecord::Base.transaction do
    service = provider_service.new(transaction, options)
    unless service.charge(amount)
      merge_errors(transaction.errors)
      raise ActiveRecord::Rollback, 'failed to charge'
    end

    success = true
  end
  success
rescue StandardError => e
  APMErrorHandler.report(e)
  false
end

#void(reference_no: nil, gb_secret_key: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/my_lib/payment/gateway.rb', line 37

def void(reference_no: nil, gb_secret_key: nil)
  success = false

  ActiveRecord::Base.transaction do
    case provider.to_sym
    when :omise
      raise NotImplementedError
      # TODO: handle later
      # provider_service.void(reference_no)
    when :gb_primepay
      provider_service.void(reference_no, gb_secret_key)
    else
      raise NotImplementedError
    end
    success = true
  end
  APMErrorHandler.report 'can not void', { reference_no: reference_no }
  success
rescue StandardError => e
  APMErrorHandler.report(e)
end