Class: SwipeHQ::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/swipehq/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil) ⇒ Transaction

Returns a new instance of Transaction.



9
10
11
12
13
14
15
# File 'lib/swipehq/transaction.rb', line 9

def initialize(params = nil)
  if params
    params.each do |k,v|
      self.send("#{k}=",v)
    end
  end
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def amount
  @amount
end

#approvedObject (readonly)

Returns the value of attribute approved.



7
8
9
# File 'lib/swipehq/transaction.rb', line 7

def approved
  @approved
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def currency
  @currency
end

#default_quantityObject

Returns the value of attribute default_quantity.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def default_quantity
  @default_quantity
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/swipehq/transaction.rb', line 7

def id
  @id
end

#identifierObject

Returns the value of attribute identifier.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def identifier
  @identifier
end

#itemObject

Returns the value of attribute item.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def item
  @item
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def status
  @status
end

#user_dataObject

Returns the value of attribute user_data.



4
5
6
# File 'lib/swipehq/transaction.rb', line 4

def user_data
  @user_data
end

Instance Method Details

#attributesObject



17
18
19
20
21
22
23
# File 'lib/swipehq/transaction.rb', line 17

def attributes
  a = {}
  %w{ identifier id item description amount default_quantity user_data currency }.each do |att|
    a[att.to_sym] = self.send(att)
  end
  return a
end

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/swipehq/transaction.rb', line 25

def create
  params = {
    api_key: SwipeHQ.api_key,
    merchant_id: SwipeHQ.merchant_id
  }
  attributes.each do |k,v|
    params["td_#{k}".to_sym] = v
  end
  res = SwipeHQ.request(
    :get,
    'https://api.swipehq.com/createTransactionIdentifier.php',
    params
  )
  if res['response_code'] and res['response_code'] == 200
    @identifier = res['data']['identifier']
  else
    @identifier = nil
  end
end

#payment_urlObject



66
67
68
69
# File 'lib/swipehq/transaction.rb', line 66

def payment_url
  return nil unless @identifier
  "https://payment.swipehq.com/?identifier_id=#{@identifier}"
end

#verifyObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swipehq/transaction.rb', line 45

def verify
  return nil unless @identifier
  params = {
    api_key: SwipeHQ.api_key,
    merchant_id: SwipeHQ.merchant_id,
    transaction_id: @identifier
  }
  res = SwipeHQ.request(
    :get,
    'https://api.swipehq.com/verifyTransaction.php',
    params
  )
  if res['response_code'] and res['response_code'] == 200
    @id = res['data']['transaction_id']
    @status = res['data']['status'] || 'pending'
    @approved = res['data']['transaction_approved'] == 'yes'
  else
    @status = nil
  end
end