Class: Killbill::Litle::PaymentPlugin

Inherits:
Plugin::Payment
  • Object
show all
Defined in:
lib/litle/api.rb

Instance Method Summary collapse

Instance Method Details

#add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, call_context = nil, options = {}) ⇒ Object



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
# File 'lib/litle/api.rb', line 77

def add_payment_method(, kb_payment_method_id, payment_method_props, set_default, call_context = nil, options = {})
  # Set a default report group
  options[:merchant] ||= ()

  # TODO Add support for real credit cards
  token = find_value_from_payment_method_props payment_method_props, 'paypageRegistrationId'

  currency = ()
  gateway = Killbill::Litle.gateway_for_currency(currency)
  litle_response = gateway.store token, options
  response = save_response_and_transaction litle_response, :add_payment_method

  if response.success
    LitlePaymentMethod.create :kb_account_id => ,
                              :kb_payment_method_id => kb_payment_method_id,
                              :litle_token => response.litle_token,
                              :cc_first_name => find_value_from_payment_method_props(payment_method_props, 'ccFirstName'),
                              :cc_last_name => find_value_from_payment_method_props(payment_method_props, 'ccLastName'),
                              :cc_type => find_value_from_payment_method_props(payment_method_props, 'ccType'),
                              :cc_exp_month => find_value_from_payment_method_props(payment_method_props, 'ccExpMonth'),
                              :cc_exp_year => find_value_from_payment_method_props(payment_method_props, 'ccExpYear'),
                              :cc_last_4 => find_value_from_payment_method_props(payment_method_props, 'ccLast4'),
                              :address1 => find_value_from_payment_method_props(payment_method_props, 'address1'),
                              :address2 => find_value_from_payment_method_props(payment_method_props, 'address2'),
                              :city => find_value_from_payment_method_props(payment_method_props, 'city'),
                              :state => find_value_from_payment_method_props(payment_method_props, 'state'),
                              :zip => find_value_from_payment_method_props(payment_method_props, 'zip'),
                              :country => find_value_from_payment_method_props(payment_method_props, 'country')
  else
    raise response.message
  end
end

#after_requestObject

return DB connections to the Pool if required



12
13
14
# File 'lib/litle/api.rb', line 12

def after_request
  ActiveRecord::Base.connection.close
end

#delete_payment_method(kb_account_id, kb_payment_method_id, call_context = nil, options = {}) ⇒ Object



110
111
112
# File 'lib/litle/api.rb', line 110

def delete_payment_method(, kb_payment_method_id, call_context = nil, options = {})
  LitlePaymentMethod.mark_as_deleted! kb_payment_method_id
end

#get_payment_info(kb_account_id, kb_payment_id, tenant_context = nil, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/litle/api.rb', line 44

def get_payment_info(, kb_payment_id, tenant_context = nil, options = {})
  # We assume the payment is immutable in Litle and only look at our tables since there
  # doesn't seem to be a Litle API to fetch details for a given transaction.
  # TODO How can we support Authorization/Sale Recycling?
  litle_transaction = LitleTransaction.from_kb_payment_id(kb_payment_id)

  litle_transaction.litle_response.to_payment_response
end

#get_payment_method_detail(kb_account_id, kb_payment_method_id, tenant_context = nil, options = {}) ⇒ Object



114
115
116
# File 'lib/litle/api.rb', line 114

def get_payment_method_detail(, kb_payment_method_id, tenant_context = nil, options = {})
  LitlePaymentMethod.from_kb_payment_method_id(kb_payment_method_id).to_payment_method_response
end

#get_payment_methods(kb_account_id, refresh_from_gateway = false, call_context = nil, options = {}) ⇒ Object



122
123
124
# File 'lib/litle/api.rb', line 122

def get_payment_methods(, refresh_from_gateway = false, call_context = nil, options = {})
  LitlePaymentMethod.().collect { |pm| pm.to_payment_method_info_response }
end

#get_refund_info(kb_account_id, kb_payment_id, tenant_context = nil, options = {}) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/litle/api.rb', line 69

def get_refund_info(, kb_payment_id, tenant_context = nil, options = {})
  # We assume the refund is immutable in Litle and only look at our tables since there
  # doesn't seem to be a Litle API to fetch details for a given transaction.
  litle_transaction = LitleTransaction.refund_from_kb_payment_id(kb_payment_id)

  litle_transaction.litle_response.to_refund_response
end

#process_payment(kb_account_id, kb_payment_id, kb_payment_method_id, amount, currency, call_context = nil, options = {}) ⇒ Object



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
# File 'lib/litle/api.rb', line 16

def process_payment(, kb_payment_id, kb_payment_method_id, amount, currency, call_context = nil, options = {})
  amount_in_cents = (amount * 100).to_i

  # If the payment was already made, just return the status
  # TODO Should we set the Litle Id field to check for dups (https://www.litle.com/mc-secure/DupeChecking_V1.2.pdf)?
  litle_transaction = LitleTransaction.from_kb_payment_id(kb_payment_id) rescue nil
  return litle_transaction.litle_response.to_payment_response unless litle_transaction.nil?

  # Required argument
  # Note! The field is limited to 25 chars, so we convert the UUID (in hex) to base64
  options[:order_id] ||= Utils.compact_uuid kb_payment_id

  # Set the account identifier to the kb_account_id
  options[:customer] ||= 

  # Set a default report group
  options[:merchant] ||= report_group_for_currency(currency)
  # Retrieve the Litle payment method
  litle_pm = LitlePaymentMethod.from_kb_payment_method_id(kb_payment_method_id)

  # Go to Litle
  gateway = Killbill::Litle.gateway_for_currency(currency)
  litle_response = gateway.purchase amount_in_cents, litle_pm.to_litle_card_token, options
  response = save_response_and_transaction litle_response, :charge, kb_payment_id, amount_in_cents

  response.to_payment_response
end

#process_refund(kb_account_id, kb_payment_id, amount, currency, call_context = nil, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/litle/api.rb', line 53

def process_refund(, kb_payment_id, amount, currency, call_context = nil, options = {})
  amount_in_cents = (amount * 100).to_i

  litle_transaction = LitleTransaction.find_candidate_transaction_for_refund(kb_payment_id, amount_in_cents)

  # Set a default report group
  options[:merchant] ||= report_group_for_currency(currency)

  # Go to Litle
  gateway = Killbill::Litle.gateway_for_currency(currency)
  litle_response = gateway.credit amount_in_cents, litle_transaction.litle_txn_id, options
  response = save_response_and_transaction litle_response, :refund, kb_payment_id, amount_in_cents

  response.to_refund_response
end

#reset_payment_methods(kb_account_id, payment_methods) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/litle/api.rb', line 126

def reset_payment_methods(, payment_methods)
  return if payment_methods.nil?

  litle_pms = LitlePaymentMethod.()

  payment_methods.delete_if do |payment_method_info_plugin|
    should_be_deleted = false
    litle_pms.each do |litle_pm|
      # Do litle_pm and payment_method_info_plugin represent the same Litle payment method?
      if litle_pm.external_payment_method_id == payment_method_info_plugin.external_payment_method_id
        # Do we already have a kb_payment_method_id?
        if litle_pm.kb_payment_method_id == payment_method_info_plugin.payment_method_id
          should_be_deleted = true
          break
        elsif litle_pm.kb_payment_method_id.nil?
          # We didn't have the kb_payment_method_id - update it
          litle_pm.kb_payment_method_id = payment_method_info_plugin.payment_method_id
          should_be_deleted = litle_pm.save
          break
          # Otherwise the same token points to 2 different kb_payment_method_id. This should never happen,
          # but we cowardly will insert a second row below
        end
      end
    end

    should_be_deleted
  end

  # The remaining elements in payment_methods are not in our table (this should never happen?!)
  payment_methods.each do |payment_method_info_plugin|
    LitlePaymentMethod.create :kb_account_id => payment_method_info_plugin.,
                              :kb_payment_method_id => payment_method_info_plugin.payment_method_id,
                              :litle_token => payment_method_info_plugin.external_payment_method_id
  end
end

#search_payment_methods(search_key, offset = 0, limit = 100, call_context = nil, options = {}) ⇒ Object



162
163
164
# File 'lib/litle/api.rb', line 162

def search_payment_methods(search_key, offset = 0, limit = 100, call_context = nil, options = {})
  LitlePaymentMethod.search(search_key, offset, limit)
end

#set_default_payment_method(kb_account_id, kb_payment_method_id, call_context = nil, options = {}) ⇒ Object



118
119
120
# File 'lib/litle/api.rb', line 118

def set_default_payment_method(, kb_payment_method_id, call_context = nil, options = {})
  # No-op
end

#start_pluginObject



3
4
5
6
7
8
9
# File 'lib/litle/api.rb', line 3

def start_plugin
  Killbill::Litle.initialize! @logger, @conf_dir, @kb_apis

  super

  @logger.info 'Killbill::Litle::PaymentPlugin started'
end