Class: Killbill::PayuLatam::PaymentPlugin

Inherits:
Killbill::Plugin::ActiveMerchant::PaymentPlugin
  • Object
show all
Defined in:
lib/payu_latam/api.rb

Instance Method Summary collapse

Constructor Details

#initializePaymentPlugin

Returns a new instance of PaymentPlugin.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/payu_latam/api.rb', line 5

def initialize
  gateway_builder = Proc.new do |config|
    ::OffsitePayments.mode = config[:test] ? :test : :production
    ::ActiveMerchant::Billing::PayULatamGateway.new :api_login          => config[:api_login],
                                                    :api_key            => config[:api_key],
                                                    :country_account_id => config[:country_account_id],
                                                    :merchant_id        => config[:merchant_id]
  end

  super(gateway_builder,
        :payu_latam,
        ::Killbill::PayuLatam::PayuLatamPaymentMethod,
        ::Killbill::PayuLatam::PayuLatamTransaction,
        ::Killbill::PayuLatam::PayuLatamResponse)
end

Instance Method Details

#add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/payu_latam/api.rb', line 116

def add_payment_method(, kb_payment_method_id, payment_method_props, set_default, properties, context)
  # Pass extra parameters for the gateway here
  options = {
      :payer_user_id => 
  }

  add_required_options(properties, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, payment_method_props, set_default, properties, context)
end

#authorize_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/payu_latam/api.rb', line 21

def authorize_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  add_required_options(properties, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#build_form_descriptor(kb_account_id, descriptor_fields, properties, context) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/payu_latam/api.rb', line 168

def build_form_descriptor(, descriptor_fields, properties, context)
  payu_hpp_credentials             = payu_hpp_credentials(properties_to_hash(properties))

  # Prepare the PayU request
  # Available fields:
  #  * :expiration_date
  #  * :customer (:name, :email, :phone, :dni_number)
  #  * :billing_address (:address1, :city, :state, :country)
  form_fields                      = properties_to_hash(descriptor_fields)
  payu_reference_code              = form_fields[:externalKey] || SecureRandom.uuid
  options                          = {
      # Required fields
      :order_id       => payu_reference_code,
      :description    => form_fields['description'] || 'Kill Bill payment',
      :payment_method => form_fields['paymentMethod'] || 'BALOTO',
  }.merge(payu_hpp_credentials)
  descriptor_fields                = merge_properties(descriptor_fields, options)

  # Create the voucher
  descriptor                       = super(, descriptor_fields, properties, context)
  payu_response                    = properties_to_hash(descriptor.form_fields)
  descriptor.form_url              = payu_response[:url]

  # Create the payment in Kill Bill to start the polling
                         = @kb_apis..(, context)
  kb_payment_method_id             = (@kb_apis.payment_api.(, false, [], context).find { |pm| pm.plugin_name == 'killbill-payu-latam' }).id
  kb_payment_id                    = nil
  payment_external_key             = payu_response[:order_id]
  payment_transaction_external_key = payu_reference_code
  # See purchase call above
  properties                       = hash_to_properties(:from_hpp => true, :payu_order_id => payu_response[:order_id], :payu_transaction_id => payu_response[:transaction_id])

  @kb_apis.payment_api.create_purchase(,
                                       kb_payment_method_id,
                                       kb_payment_id,
                                       form_fields[:amount],
                                       form_fields[:currency] || .currency,
                                       payment_external_key,
                                       payment_transaction_external_key,
                                       properties,
                                       context)

  descriptor
end

#capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/payu_latam/api.rb', line 31

def capture_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  add_required_options(properties, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/payu_latam/api.rb', line 80

def credit_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  add_required_options(properties, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/payu_latam/api.rb', line 128

def delete_payment_method(, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, properties, context)
end

#get_payment_info(kb_account_id, kb_payment_id, properties, context) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/payu_latam/api.rb', line 100

def get_payment_info(, kb_payment_id, properties, context)
  t_info_plugins = super(, kb_payment_id, properties, context)

  t_info_plugins = get_payment_info_from_payu(kb_payment_id, properties_to_hash(properties), context) if t_info_plugins.empty?

  t_info_plugins
end

#get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/payu_latam/api.rb', line 136

def get_payment_method_detail(, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, properties, context)
end

#get_payment_methods(kb_account_id, refresh_from_gateway, properties, context) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/payu_latam/api.rb', line 148

def get_payment_methods(, refresh_from_gateway, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, refresh_from_gateway, properties, context)
end

#process_notification(notification, properties, context) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/payu_latam/api.rb', line 213

def process_notification(notification, properties, context)
  # Pass extra parameters for the gateway here
  options    = {}
  properties = merge_properties(properties, options)

  super(notification, properties, context) do |gw_notification, service|
    # Retrieve the payment
    # gw_notification.kb_payment_id =
    #
    # Set the response body
    # gw_notification.entity =
  end
end

#purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



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

def purchase_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Direct payment?
  if find_value_from_properties(properties, 'from_hpp') != 'true'
    options = {}
    add_required_options(properties, options)
    properties = merge_properties(properties, options)
    super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  else
    # Nope, HPP
    payu_order_id                = find_value_from_properties(properties, 'payu_order_id')
    payu_transaction_id          = find_value_from_properties(properties, 'payu_transaction_id')
     = find_value_from_properties(properties, 'payment_processor_account_id')

    # Record a response row, to keep track of the created vouchers in PayU
    response                     = @response_model.create(:api_call                     => :build_form_descriptor,
                                                          :kb_account_id                => ,
                                                          :kb_payment_id                => kb_payment_id,
                                                          :kb_payment_transaction_id    => kb_payment_transaction_id,
                                                          :transaction_type             => :PURCHASE,
                                                          :authorization                => [payu_order_id, payu_transaction_id].join(';'),
                                                          :payment_processor_account_id => ,
                                                          :kb_tenant_id                 => context.tenant_id,
                                                          :success                      => true)

    # Get the payment status from PayU (we are required to fetch the status from PayU within 7 minutes of the URL creation)
    get_payment_transaction_info_from_payu(kb_payment_id, kb_payment_transaction_id, amount, currency, properties_to_hash(properties), context.tenant_id, response)
  end
end

#refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/payu_latam/api.rb', line 90

def refund_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  add_required_options(properties, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#reset_payment_methods(kb_account_id, payment_methods, properties, context) ⇒ Object



164
165
166
# File 'lib/payu_latam/api.rb', line 164

def reset_payment_methods(, payment_methods, properties, context)
  super
end

#search_payment_methods(search_key, offset, limit, properties, context) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/payu_latam/api.rb', line 156

def search_payment_methods(search_key, offset, limit, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(search_key, offset, limit, properties, context)
end

#search_payments(search_key, offset, limit, properties, context) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/payu_latam/api.rb', line 108

def search_payments(search_key, offset, limit, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(search_key, offset, limit, properties, context)
end

#set_default_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



144
145
146
# File 'lib/payu_latam/api.rb', line 144

def set_default_payment_method(, kb_payment_method_id, properties, context)
  # TODO
end

#void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/payu_latam/api.rb', line 70

def void_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  add_required_options(properties, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context)
end