Class: Epay::Transaction

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/epay/transaction.rb

Instance Attribute Summary

Attributes included from Model

#data, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#initialize, #inspect, inspect

Class Method Details

.create(params) ⇒ Object

Epay::Transaction.create(:card_no => ‘1234…’, :cvc => ‘123’, :exp_month => ‘11’, :exp_year => ‘12’, :amount => ‘119’, :order_no => ‘ND-TEST’)



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/epay/transaction.rb', line 139

def create(params)
  post = Api.default_post_for_params(params).merge({
    :orderid        => params[:order_no],
    :amount         => params[:amount]*100,
    :currency       => Epay::CURRENCY_CODES[(params[:currency] || Epay.default_currency).to_sym],
    :description    => params[:description],
    :cardholder     => params[:cardholder],
    :group          => params[:group],
    :instantcapture => params[:instant_capture] ? '1' : '0'
  })
  
  query = Api.authorize(post)
  
  if query['accept']
    # Find the transaction
    transaction = Transaction.find(query["tid"].to_i)
  else
    # Return a new transaction with error code filled
    new(nil, {
      'failed'      => true,
      'error'       => query["error"],
      'orderid'     => post[:orderid],
      'authamount'  => post[:amount],
      'description' => post[:description],
      'cardholder'  => post[:cardholder],
      'group'       => post[:group],
      'currency'    => post[:currency]
    })
  end
end

.find(id) ⇒ Object



132
133
134
135
# File 'lib/epay/transaction.rb', line 132

def find(id)
  transaction = new(id)
  transaction.reload
end

.inspectable_attributesObject



5
6
7
# File 'lib/epay/transaction.rb', line 5

def self.inspectable_attributes
  %w(id amount created_at order_no description cardholder acquirer currency group captured)
end

Instance Method Details

#acquirerObject



34
35
36
# File 'lib/epay/transaction.rb', line 34

def acquirer
  data['acquirer']
end

#amountObject



9
10
11
# File 'lib/epay/transaction.rb', line 9

def amount
  data['authamount'].to_f / 100
end

#captureObject



102
103
104
105
106
107
108
109
110
# File 'lib/epay/transaction.rb', line 102

def capture
  response = Epay::Api.request(PAYMENT_SOAP_URL, 'capture', :transactionid => id, :amount => (amount * 100).to_i)
  if response.success?
    reload
    true
  else
    false
  end
end

#capturedObject Also known as: captured?



62
63
64
# File 'lib/epay/transaction.rb', line 62

def captured
  !captured_at.nil?
end

#captured_amountObject



51
52
53
# File 'lib/epay/transaction.rb', line 51

def captured_amount
  data['capturedamount'].to_f / 100
end

#captured_atObject



55
56
57
58
59
60
# File 'lib/epay/transaction.rb', line 55

def captured_at
  return nil if failed?
  
  time = Time.parse(data['captureddate'].to_s)
  time < created_at ? nil : time
end

#cardObject



42
43
44
45
# File 'lib/epay/transaction.rb', line 42

def card
  # We can find some card data in the history:
  @card ||= Card.new(:kind => CARD_KINDS[data['cardtypeid'].to_i], :number => data['tcardno'], :exp_year => data['expyear'].to_i, :exp_month => data['expmonth'].to_i)
end

#cardholderObject



30
31
32
# File 'lib/epay/transaction.rb', line 30

def cardholder
  data['cardholder']
end

#created_atObject



13
14
15
16
# File 'lib/epay/transaction.rb', line 13

def created_at
  return nil if failed?
  Time.parse(data['authdate'])
end

#credit(amount_to_be_credited = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/epay/transaction.rb', line 112

def credit(amount_to_be_credited = nil)
  amount_to_be_credited ||= amount - credited_amount
  
  Epay::Api.request(PAYMENT_SOAP_URL, 'credit', :transactionid => id, :amount => amount_to_be_credited * 100) do |response|
    if response.success?
      true
    else
      raise TransactionInGracePeriod if response.data['epayresponse'] == "-1021"
      
      false
    end
  end
end

#credited_amountObject



47
48
49
# File 'lib/epay/transaction.rb', line 47

def credited_amount
  data['creditedamount'].to_f / 100
end

#currencyObject



22
23
24
# File 'lib/epay/transaction.rb', line 22

def currency
  Epay::CURRENCY_CODES.key(data['currency'])
end

#deleteObject



126
127
128
# File 'lib/epay/transaction.rb', line 126

def delete
  Epay::Api.request(PAYMENT_SOAP_URL, 'delete', :transactionid => id).success?
end

#descriptionObject



26
27
28
# File 'lib/epay/transaction.rb', line 26

def description
  data['description']
end

#errorObject



75
76
77
# File 'lib/epay/transaction.rb', line 75

def error
  data['error']
end

#failed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/epay/transaction.rb', line 71

def failed?
  !!data['failed']
end

#groupObject



38
39
40
# File 'lib/epay/transaction.rb', line 38

def group
  data['group']
end

#order_noObject



18
19
20
# File 'lib/epay/transaction.rb', line 18

def order_no
  data['orderid']
end

#permanent_error?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/epay/transaction.rb', line 83

def permanent_error?
  failed? && !temporary_error?
end

#production?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/epay/transaction.rb', line 91

def production?
  !test?
end

#reloadObject

Actions



96
97
98
99
100
# File 'lib/epay/transaction.rb', line 96

def reload
  response = Epay::Api.request(PAYMENT_SOAP_URL, 'gettransaction', :transactionid => id)
  @data = response.data['transactionInformation']
  self
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  !failed?
end

#temporary_error?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/epay/transaction.rb', line 79

def temporary_error?
  failed? && TEMPORARY_ERROR_CODES.include?(error)
end

#test?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/epay/transaction.rb', line 87

def test?
  data['mode'] == 'MODE_EPAY' || data['mode'] == 'MODE_TEST'
end