Sha256: 37c672410e7061ea16fcc6a4038abfabb0da697f5fa5e3b012af6dffb58dcb91

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

module Clearsale
  class OrderResponse
    STATUS_MAP = {
      "APA" => :automatic_approval,
      "APM" => :manual_approval,
      "RPM" => :rejected_without_suspicion,
      "AMA" => :manual_analysis,
      "ERR" => :error,
      "NVO" => :waiting,
      "SUS" => :manual_rejection,
      "CAN" => :cancelled,
      "FRD" => :fraud,
    }

    attr_reader :order_id, :status, :score

    def self.build_from_send_order(package)
      new(package.fetch(:package_status, {}))
    end

    def self.build_from_update(package)
      new(package.fetch(:clear_sale, {}))
    end

    def initialize(hash)
      response = hash.fetch(:orders, {}).fetch(:order, {})

      if response.blank?
        @status = :inexistent_order
      else
        @order_id = response[:id].gsub(/[a-zA-Z]*/, '').to_i
        @score    = response[:score].to_f
        @status   = STATUS_MAP[response[:status]]
      end
    end

    def approved?
      @status == :automatic_approval || @status == :manual_approval
    end

    def rejected?
      @status == :rejected_without_suspicion || @status == :fraud || @status == :manual_rejection
    end

    def manual_analysis?
      @status == :manual_analysis
    end

    def inexistent_order?
      @status == :inexistent_order
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
clearsale-0.1.4 lib/clearsale/order_response.rb
clearsale-0.1.3 lib/clearsale/order_response.rb
clearsale-0.1.2 lib/clearsale/order_response.rb
clearsale-0.1.1 lib/clearsale/order_response.rb
clearsale-0.1.0 lib/clearsale/order_response.rb