Class: Providers::GbPrimepay::CreditCard

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/my_lib/payment/providers/gb_primepay/credit_card.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(transaction, options = {}) ⇒ CreditCard

Returns a new instance of CreditCard.



7
8
9
10
11
12
13
14
15
# File 'app/my_lib/payment/providers/gb_primepay/credit_card.rb', line 7

def initialize(transaction, options = {})
  card_param = options[:card]
  @transaction = transaction
  @card = if card_param.is_a?(ActionController::Parameters)
            card_param.permit!.to_h.with_indifferent_access
          elsif card_param.present?
            card_param.with_indifferent_access
          end
end

Class Method Details

.void(reference_no, gb_secret_key = nil) ⇒ Object

Parameters:

  • reference_no (String)


49
50
51
52
53
54
55
56
57
# File 'app/my_lib/payment/providers/gb_primepay/credit_card.rb', line 49

def self.void(reference_no, gb_secret_key = nil)
  if gb_secret_key.nil?
    tmp_secret = RequestStore.store[GB_PRIMEPAY_SECRET_KEY]
    gb_secret_key = tmp_secret.presence || Figaro.env.GB_PRIMEPAY_SECRET_KEY!
  end

  client = ::Payment::Gateway.new(provider: :gb_primepay)
  client.void(reference_no, gb_secret_key)
end

Instance Method Details

#charge(charge_amount) ⇒ Object

Parameters:

  • charge_amount (Money)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/my_lib/payment/providers/gb_primepay/credit_card.rb', line 32

def charge(charge_amount)
  return false unless valid?

  attributes = charge_attribute(charge_amount)
  transaction.charges.build(attributes)

  external_charge_id = attributes[:omise_charge_id]

  gb_secret_key = RequestStore.store[GB_PRIMEPAY_SECRET_KEY]
  VoidFailedTransactionWorker.void_later(external_charge_id, gb_secret_key)

  set_authorization_url(external_charge_id)
  check_payment_status(external_charge_id)
  true
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/my_lib/payment/providers/gb_primepay/credit_card.rb', line 17

def valid?
  if card.nil?
    errors.add :base, 'Token or Card is blank'
    return false
  end

  if card.respond_to?(:permit!) && card.permit!.to_h.with_indifferent_access['token'].blank?
    error.add :base, 'Token or Card is blank'
    return false
  end

  true
end

#void(reference_no, gb_secret_key) ⇒ Object

use .void method for easy access



60
61
62
63
64
65
66
# File 'app/my_lib/payment/providers/gb_primepay/credit_card.rb', line 60

def void(reference_no, gb_secret_key)
  url = "#{BASE_URL}v1/void"
  body = { gbpReferenceNo: reference_no }
  payload = body_to_payload(body, gb_secret_key)

  api_call(url, payload)
end