Sha256: b23f9486152bf6c4e3f18b36316f3ed50a6d95ddffd9c250c09e8cbd1c13460b

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

require 'active_merchant'

class GatewayNotification < ActiveRecord::Base
  class EmptyChargeIdError < ArgumentError;end

  belongs_to :charge

  serialize :params

  before_create :set_charge_id

  attr_accessor :raw_post, :logger

  def acknowledge
    adapter.acknowledge
  end

  def complete?
    adapter.complete?
  end

  def need_response?
    adapter.respond_to?(:success_response)
  end

  def success_response
    adapter.respond_to?(:success_response) ? adapter.success_response : "OK"
  end

  def approve
    logger.info "real amount = #{real_amount}"
    charge.approve(real_amount) unless charge.ok?
  end

  def real_amount
    params[service.mappings[:amount]]
  end

  private

  def adapter
    @adapter ||= "ActiveMerchant::Billing::Integrations::#{gateway.classify}::Notification".classify.constantize.new(raw_post)
  rescue NameError
    raise "Unknown integration '#{gateway}'"
  end

  def service
    "ActiveMerchant::Billing::Integrations::#{gateway.classify}::Helper".classify.constantize
  end

  def set_charge_id
    self.charge_id = adapter.item_id || raise(EmptyChargeIdError)
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
supercharged-2.0.4 app/models/gateway_notification.rb
supercharged-2.0.3 app/models/gateway_notification.rb
supercharged-2.0.2 app/models/gateway_notification.rb
supercharged-2.0.1 app/models/gateway_notification.rb
supercharged-2.0.0 app/models/gateway_notification.rb
supercharged-1.0.0 app/models/gateway_notification.rb