class SecupayRuby::Payment

Constants

INIT_FIELDS
STATUS_FIELDS

Attributes

amount[R]
api_key[R]
demo[R]
payment_type[R]
response[R]

Public Class Methods

get_types(api_key: SecupayRuby::ApiKey::MasterKey.new) click to toggle source
# File lib/secupay_ruby/payment.rb, line 94
def get_types(api_key: SecupayRuby::ApiKey::MasterKey.new)
  response = SecupayRuby::Requests::GetTypes.post(api_key: api_key)

  response.data
end
new(api_key: SecupayRuby::ApiKey::MasterKey.new, hash: nil) click to toggle source
# File lib/secupay_ruby/payment.rb, line 29
def initialize(api_key: SecupayRuby::ApiKey::MasterKey.new, hash: nil)
  @api_key = api_key
  @hash = hash
end

Public Instance Methods

cancel() click to toggle source
# File lib/secupay_ruby/payment.rb, line 84
def cancel
  raise_if_not_initiated

  @response = nil

  @response = SecupayRuby::Requests::Cancel.post(api_key: api_key,
                                                 payment: self)
end
capture() click to toggle source
# File lib/secupay_ruby/payment.rb, line 75
def capture
  raise_if_not_initiated

  @response = nil

  @response = SecupayRuby::Requests::Capture.post(api_key: api_key,
                                                  payment: self)
end
hash?() click to toggle source
# File lib/secupay_ruby/payment.rb, line 34
def hash?
  !hash.nil?
end
init(amount:, payment_type:, demo: 0, user: nil) click to toggle source
# File lib/secupay_ruby/payment.rb, line 38
def init(amount:, payment_type:, demo: 0, user: nil)
  @response = nil

  @amount = amount
  @payment_type = payment_type
  @demo = demo

  raise_if_payment_not_initiated
  raise_if_payment_type_invalid

  params = {
    amount: amount,
    demo: demo,
    payment_type: payment_type
  }
  params = params.merge(user.to_api_fields) if user

  @response = SecupayRuby::Requests::Init.post(api_key: api_key,
                                               payment: self,
                                               body: params)

  extract_response_params(INIT_FIELDS)
end
load_status() click to toggle source
# File lib/secupay_ruby/payment.rb, line 62
def load_status
  raise_if_not_initiated

  @response = nil

  params = { hash: hash }
  @response = SecupayRuby::Requests::Status.post(api_key: api_key,
                                                 payment: self,
                                                 body: params)

  extract_response_params(STATUS_FIELDS)
end

Private Instance Methods

extract_response_params(fields) click to toggle source
# File lib/secupay_ruby/payment.rb, line 117
def extract_response_params(fields)
  fields.each do |field_name, param_name|
    instance_variable_set("@#{field_name}", @response.data[param_name])
  end
end
raise_if_not_initiated() click to toggle source
# File lib/secupay_ruby/payment.rb, line 103
def raise_if_not_initiated
  raise PaymentStatusError.new "Not initiated" unless hash?
end
raise_if_payment_not_initiated() click to toggle source
# File lib/secupay_ruby/payment.rb, line 113
def raise_if_payment_not_initiated
  raise PaymentStatusError.new "Payment already initiated" if hash?
end
raise_if_payment_type_invalid() click to toggle source
# File lib/secupay_ruby/payment.rb, line 107
def raise_if_payment_type_invalid
  unless valid_payment_type?
    raise PaymentStatusError.new "Payment type not supported"
  end
end
valid_payment_type?() click to toggle source
# File lib/secupay_ruby/payment.rb, line 123
def valid_payment_type?
  return false unless @payment_type

  valid_types = Types.constants.map { |constant| Types.const_get constant }
  valid_types.include?(@payment_type)
end