Class: FatZebra::Models::Purchase

Inherits:
Base
  • Object
show all
Defined in:
lib/fat_zebra/models/purchase.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attribute, #initialize, #inspect, #to_s

Constructor Details

This class inherits a constructor from FatZebra::Models::Base

Class Method Details

.create(amount, card_data, reference, customer_ip, currency = "AUD") ⇒ Response

Performs a purchase transaction against the gateway

Parameters:

  • the (Integer)

    amount as an integer e.g. (1.00 * 100).to_i

  • a (Hash)

    hash of the card data (example: => “John Smith”, :number => “…”, :expiry => “…”, :cvv => “123” or => “abcdefg1”)

  • the (String)

    card holders name

  • the (String)

    customers credit card number

  • the (Date)

    customers card expiry date (as Date or string [mm/yyyy])

  • the (String)

    credit card verification value (cvv, cav, csc etc)

  • a (String)

    reference for the purchase

  • the (String)

    customers IP address (for fraud prevention)

  • currency (String) (defaults to: "AUD")

    code (“AUD”, “USD”, etc)

Returns:

  • (Response)

    response (purchase) object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fat_zebra/models/purchase.rb', line 31

def create(amount, card_data, reference, customer_ip, currency = "AUD")
  params = {
    :amount => amount,
    :card_holder => card_data.delete(:card_holder),
    :card_number => card_data.delete(:number),
    :card_expiry => extract_date(card_data.delete(:expiry)),
    :cvv => card_data.delete(:cvv),
    :card_token => card_data.delete(:token),
    :reference => reference,
    :customer_ip => customer_ip,
    :currency => currency
  }

  params.delete_if {|key, value| value.nil? } # If token is nil, remove, otherwise, remove card values
  validate_params!(params)
  response = FatZebra.gateway.make_request(:post, "purchases", params)
  Response.new(response)
end

.find(options = {}) ⇒ Array<Purchase>

Retrieves purchases specified by the options hash

- from (Date)
- to (Date)
      - offset (defaults to 0) - for pagination
      - limit (defaults to 10) for pagination

Parameters:

  • options (Hash) (defaults to: {})

    for lookup includes:

    - id (unique purchase ID)
    - reference (merchant reference)
    

Returns:

  • (Array<Purchase>)

    array of purchases



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fat_zebra/models/purchase.rb', line 61

def find(options = {})
  id = options.delete(:id)
  options.merge!({:offets => 0, :limit => 10})

  # Format dates for the request
  options[:from] = options[:from].strftime("%Y%m%dT%H%M") if options[:from]
  options[:to] = options[:to].strftime("%Y%m%dT%H%M") if options[:to]


  if id.nil?
    response = FatZebra.gateway.make_request(:get, "purchases", options)
    if response["successful"]
      purchases = []
      response["response"].each do |purchase|
        purchases << Purchase.new(purchase)
      end

      purchases.size == 1 ? purchases.first : purchases
    else
      # TODO: This should raise a defined exception
      raise StandardError, "Unable to query purchases, #{response["errors"].inspect}"
    end
  else
    response = FatZebra.gateway.make_request(:get, "purchases/#{id}.json")
    if response["successful"]
      Purchase.new(response["response"])
    else
      raise StandardError, "Unable to query purchases, #{response["errors"].inspect}"
    end
  end
end

Instance Method Details

#refund(amount, reference) ⇒ Object

Refunds the current transaction

Parameters:

  • the (Integer)

    amount to be refunded

  • the (String)

    refund reference



13
14
15
# File 'lib/fat_zebra/models/purchase.rb', line 13

def refund(amount, reference)
  Refund.create(self.id, amount, reference)
end