Class: Barion::Payment

Inherits:
ApplicationRecord show all
Includes:
Currencies, DataFormats, JsonSerializer
Defined in:
app/models/barion/payment.rb

Overview

Represents a payment in Barion engine

Instance Method Summary collapse

Methods included from JsonSerializer

#deserialize, #key_names, #process_response, #serializable_hash

Methods included from DataFormats

phone_number

Instance Method Details

#deserialize_optionsObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'app/models/barion/payment.rb', line 258

def deserialize_options
  {
    assoc: {
      payment_transactions: {
        pos_transaction_id: 'POSTransactionId'
      }
    },
    map: {
      keys: {
        _all: :underscore,
        Transactions: 'payment_transactions',
        CreatedAt: 'barion_created_at'
      },
      values: {
        _all: proc { |v| v.respond_to?(:underscore) ? v.underscore : v },
        _except: %w[Locale Currency]
      }
    },
    before_save: :refresh_checksum
  }
end

#executeObject



211
212
213
214
215
216
217
218
219
220
# File 'app/models/barion/payment.rb', line 211

def execute
  if valid?
    ::Barion.config.endpoint['v2/Payment/Start'].post(
      as_json.to_json,
      { content_type: :json, accept: :json }
    ) { |response, request, _result| handle_response(response, request) }
  else
    false
  end
end

#payer_home_number=(number) ⇒ Object



197
198
199
# File 'app/models/barion/payment.rb', line 197

def payer_home_number=(number)
  super(::Barion::DataFormats.phone_number(number))
end

#payer_phone_number=(number) ⇒ Object



193
194
195
# File 'app/models/barion/payment.rb', line 193

def payer_phone_number=(number)
  super(::Barion::DataFormats.phone_number(number))
end

#payer_work_phone_number=(number) ⇒ Object



189
190
191
# File 'app/models/barion/payment.rb', line 189

def payer_work_phone_number=(number)
  super(::Barion::DataFormats.phone_number(number))
end

#payment_type=(value) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/barion/payment.rb', line 174

def payment_type=(value)
  case value.to_sym
  when :immediate
    self.reservation_period = nil
  when :reservation
    self.reservation_period = 30.minutes.to_i
  when :delayed_capture
    self.reservation_period = 1.minutes.to_i
    self.delayed_capture_period = 1.week.to_i
  else
    raise ArgumentError, "#{value} is not a valid payment_type"
  end
  super(value)
end

#poskey=(value = nil) ⇒ Object



169
170
171
172
# File 'app/models/barion/payment.rb', line 169

def poskey=(value = nil)
  value = ::Barion.config.poskey if value.nil?
  super(value)
end

#readonly?Boolean

Returns:

  • (Boolean)


201
202
203
204
205
# File 'app/models/barion/payment.rb', line 201

def readonly?
  return false if @_bypass_readonly

  !initial?
end

#refresh_checksumObject



207
208
209
# File 'app/models/barion/payment.rb', line 207

def refresh_checksum
  self.checksum = gen_checksum
end

#refresh_stateObject



222
223
224
225
226
227
228
229
230
231
# File 'app/models/barion/payment.rb', line 222

def refresh_state
  ::Barion.config.endpoint['v2/Payment/GetPaymentState'].get(
    {
      params: {
        POSKey: poskey,
        PaymentId: payment_id
      }
    }
  ) { |response, request, _| handle_response(response, request) }
end

#serialize_optionsObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/models/barion/payment.rb', line 233

def serialize_options
  { only: %i[callback_url card_holder_name_hint challenge_preference currency
             delayed_capture_period funding_sources guest_check_out initiate_recurrence
             locale order_number payer_hint payer_home_number payer_phone_number
             payer_work_phone_number payment_type payment_window poskey recurrence_type
             redirect_url reservation_period payment_request_id recurrence_id trace_id],
    include: %i[billing_address shipping_address payment_transactions payer_account purchase_information],
    map: {
      keys: {
        _all: :camelize,
        poskey: 'POSKey',
        payment_transactions: 'Transactions'
      },
      values: {
        _all: proc { |v| v.respond_to?(:camelize) ? v.camelize : v },
        _except: %w[poskey redirect_url callback_url locale payment_request_id payer_hint card_holder_name_hint],
        reservation_period: :as_time,
        delayed_capture_period: :as_time,
        payment_window: :as_time,
        funding_sources: :as_list,
        challenge_preference: :as_enum_id
      }
    } }
end