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', optional = {}) ⇒ Response

Performs a purchase transaction against the gateway

Parameters:

  • amount (Integer)

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

  • card_data (Hash)

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

  • reference (String)

    a reference for the purchase

  • customer_ip (String)

    the customers IP address (for fraud prevention)

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

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

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

    any optional parameters to be included in the payload

Options Hash (card_data):

  • card_holder (String)

    the card holders name

  • card_number (String)

    the customers credit card number

  • expiry (Date)

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

  • cvv (String)

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

Returns:

  • (Response)

    response (purchase) object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fat_zebra/models/purchase.rb', line 85

def create(amount, card_data, reference, customer_ip, currency = 'AUD', optional = {})
  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 {|_, value| value.nil? } # If token is nil, remove, otherwise, remove card values
  params.merge!(optional)
  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

Parameters:

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

    for lookup

Options Hash (options):

  • id (String)

    the unique purchase ID

  • reference (String)

    the merchant reference

  • from (DateTime)

    the start date to search from

  • to (DateTime)

    the end date time to search to

  • offset (Integer)

    the start page for pagination

  • limit (Integer)

    the maximum number of records to return in the query. Maximum 10

Returns:

  • (Array<Purchase>)

    array of purchases



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fat_zebra/models/purchase.rb', line 116

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

#capture(amount = nil) ⇒ Response

Captures an authorization

Parameters:

  • amount (Integer) (defaults to: nil)

    the amount to capture. (Optional)

Returns:

  • (Response)

    Purchase response object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fat_zebra/models/purchase.rb', line 23

def capture(amount = nil)
  if amount.nil?
    params = {}
  else
    params = {
      :amount => amount
    }
  end
  response = FatZebra.gateway.make_request(:post, "purchases/#{self.id}/capture", params)
  resp = Response.new(response)

  if resp.successful && resp.purchase.successful
    self.captured_amount = amount || self.amount
    return true
  else
    raise StandardError, "Unable to capture purchase - #{resp.errors.join(',')}"
  end
end

#refund(amount, reference) ⇒ Object

Refunds the current transaction

Parameters:

  • amount (Integer)

    the amount to be refunded

  • reference (String)

    the refund reference



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

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

#to_hashHash

Returns the record as a Hash

Returns:

  • (Hash)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fat_zebra/models/purchase.rb', line 45

def to_hash
  {
    id: self.id,
    amount: self.amount,
    reference: self.reference,
    message: self.message,
    authorization: self.authorization,
    card_number: self.card_number,
    card_holder: self.card_holder,
    card_expiry: self.card_expiry,
    card_token: self.card_token,
    currency: self.currency,
    authorized: self.authorized,
    successful: self.successful,
    captured: self.captured,
    captured_amount: self.captured_amount,
    response_code: self.response_code,
    cvv_match: self.cvv_match,
    rrn: self.rrn,
    fraud_result: self.fraud_result,
    fraud_messages: self.fraud_messages,
    metadata: self. || {}
  }
end