Class: ChargeIO::Gateway

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/chargeio/models/gateway.rb

Constant Summary

Constants included from Connection

Connection::DEFAULT_TIMEOUT

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gateway

Returns a new instance of Gateway.


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chargeio/models/gateway.rb', line 4

def initialize(options={})
  @site = options[:site] || ChargeIO::DEFAULT_SITE
  @url = URI.join(@site, "/v1/")

  if options[:auth_user].nil?
    @auth = {
      :username => options[:secret_key],
      :password => '',
    }
    raise ArgumentError.new("secret_key not set") if @auth[:username].nil?
  else
    puts "DEPRECATION WARNING:"
    puts "You're using an old style authentication mechanism (:auth_user and :auth_password)."
    puts "Please use your secret_key instead and instantiate the Gateway Object like this:"
    puts "ChargeIO::Gateway.new(:secret_key => 'your secret key')"
    puts ""
    @auth = {
      :username => options[:auth_user],
      :password => options[:auth_password],
    }
    raise ArgumentError.new("auth_user not set") if @auth[:username].nil?
    raise ArgumentError.new("auth_password not set") if @auth[:password].nil?
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object


29
30
31
# File 'lib/chargeio/models/gateway.rb', line 29

def as_json(options={})
  { 'site' => @site, 'url' => @url }
end

#authObject


330
331
332
# File 'lib/chargeio/models/gateway.rb', line 330

def auth
  @auth
end

#authorize(amount, params = {}) ⇒ Object


121
122
123
124
125
126
127
128
129
130
# File 'lib/chargeio/models/gateway.rb', line 121

def authorize(amount, params={})
  headers = {}
  amount_value, amount_currency = amount_to_parts(amount)
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  transaction_params = params.merge(:auto_capture => false, :amount => amount_value, :currency => amount_currency)
  response = post('charges', transaction_params.to_json, headers)
  process_response(ChargeIO::Charge, response)
end

#banks(params = {}) ⇒ Object


105
106
107
108
# File 'lib/chargeio/models/gateway.rb', line 105

def banks(params={})
  response = get(:banks, nil, params)
  process_list_response(ChargeIO::Bank, response, 'results')
end

#cancel_recurring_charge(id) ⇒ Object


253
254
255
256
# File 'lib/chargeio/models/gateway.rb', line 253

def cancel_recurring_charge(id)
  response = post("recurring/charges/#{id}/cancel", nil)
  process_response(ChargeIO::RecurringCharge, response)
end

#capture(transaction_id, amount, params = {}) ⇒ Object


152
153
154
155
156
157
158
159
160
161
# File 'lib/chargeio/models/gateway.rb', line 152

def capture(transaction_id, amount, params={})
  amount_value, amount_currency = amount_to_parts(amount)
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  transaction_params = params.merge(:amount => amount_value, :currency => amount_currency)
  response = post("charges/#{transaction_id}/capture", transaction_params.to_json, headers)
  process_response(ChargeIO::Charge, response)
end

#cards(params = {}) ⇒ Object


84
85
86
87
# File 'lib/chargeio/models/gateway.rb', line 84

def cards(params={})
  response = get(:cards, nil, params)
  process_list_response(ChargeIO::Card, response, 'results')
end

#charge(amount, params = {}) ⇒ Object


132
133
134
135
136
137
138
139
140
141
# File 'lib/chargeio/models/gateway.rb', line 132

def charge(amount, params={})
  headers = {}
  amount_value, amount_currency = amount_to_parts(amount)
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  transaction_params = params.merge(:amount => amount_value, :currency => amount_currency)
  response = post('charges', transaction_params.to_json, headers)
  process_response(ChargeIO::Charge, response)
end

#create_bank(params = {}) ⇒ Object


89
90
91
92
93
# File 'lib/chargeio/models/gateway.rb', line 89

def create_bank(params={})
  bank_json = params.to_json
  response = post(:banks, bank_json)
  process_response(ChargeIO::Bank, response)
end

#create_card(params = {}) ⇒ Object


68
69
70
71
72
# File 'lib/chargeio/models/gateway.rb', line 68

def create_card(params={})
  card_json = params.to_json
  response = post(:cards, card_json)
  process_response(ChargeIO::Card, response)
end

#create_recurring_charge(params = {}) ⇒ Object


215
216
217
218
219
220
221
222
223
224
# File 'lib/chargeio/models/gateway.rb', line 215

def create_recurring_charge(params={})
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end

  json = params.to_json
  response = post('recurring/charges', json, headers)
  process_response(ChargeIO::RecurringCharge, response)
end

#create_token(params = {}) ⇒ Object


58
59
60
61
# File 'lib/chargeio/models/gateway.rb', line 58

def create_token(params={})
  response = form_post(:tokens, params)
  process_response(ChargeIO::Token, response)
end

#credit(amount, params = {}) ⇒ Object


174
175
176
177
178
179
180
181
182
183
# File 'lib/chargeio/models/gateway.rb', line 174

def credit(amount, params={})
  headers = {}
  amount_value, amount_currency = amount_to_parts(amount)
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  transaction_params = params.merge(:amount => amount_value, :currency => amount_currency)
  response = post('credits', transaction_params.to_json, headers)
  process_response(ChargeIO::Credit, response)
end

#delete_bank(token) ⇒ Object


95
96
97
98
# File 'lib/chargeio/models/gateway.rb', line 95

def delete_bank(token)
  response = delete(:banks, token)
  process_response(ChargeIO::Bank, response)
end

#delete_card(token) ⇒ Object


74
75
76
77
# File 'lib/chargeio/models/gateway.rb', line 74

def delete_card(token)
  response = delete(:cards, token)
  process_response(ChargeIO::Card, response)
end

#delete_recurring_charge(id) ⇒ Object


248
249
250
251
# File 'lib/chargeio/models/gateway.rb', line 248

def delete_recurring_charge(id)
  response = delete('recurring/charges', id)
  process_response(ChargeIO::RecurringCharge, response)
end

#events(params = {}) ⇒ Object

Events


286
287
288
289
# File 'lib/chargeio/models/gateway.rb', line 286

def events(params={})
  response = get(:events, nil, params)
  process_list_response(ChargeIO::Event, response, 'results')
end

#find_bank(token_id, params = {}) ⇒ Object


100
101
102
103
# File 'lib/chargeio/models/gateway.rb', line 100

def find_bank(token_id, params={})
  response = get(:banks, token_id, params)
  process_response(ChargeIO::Bank, response)
end

#find_card(token_id, params = {}) ⇒ Object


79
80
81
82
# File 'lib/chargeio/models/gateway.rb', line 79

def find_card(token_id, params={})
  response = get(:cards, token_id, params)
  process_response(ChargeIO::Card, response)
end

#find_event(event_id, params = {}) ⇒ Object


291
292
293
294
# File 'lib/chargeio/models/gateway.rb', line 291

def find_event(event_id, params={})
  response = get(:events, event_id, params)
  process_response(ChargeIO::Event, response)
end

#find_recurring_charge(id) ⇒ Object


210
211
212
213
# File 'lib/chargeio/models/gateway.rb', line 210

def find_recurring_charge(id)
  response = get('recurring/charges', id)
  process_response(ChargeIO::RecurringCharge, response)
end

#find_recurring_charge_occurrence(recurring_charge_id, occurrence_id) ⇒ Object


263
264
265
266
# File 'lib/chargeio/models/gateway.rb', line 263

def find_recurring_charge_occurrence(recurring_charge_id, occurrence_id)
  response = get("recurring/charges/#{recurring_charge_id}/occurrences/#{occurrence_id}", nil)
  process_response(ChargeIO::RecurringChargeOccurrence, response)
end

#find_signature(id) ⇒ Object


198
199
200
201
# File 'lib/chargeio/models/gateway.rb', line 198

def find_signature(id)
  response = get('signatures', id)
  process_response(ChargeIO::Signature, response)
end

#find_token(token_id, params = {}) ⇒ Object


63
64
65
66
# File 'lib/chargeio/models/gateway.rb', line 63

def find_token(token_id, params={})
  response = get(:tokens, token_id, params)
  process_response(ChargeIO::Token, response)
end

#find_transaction(transaction_id, params = {}) ⇒ Object


116
117
118
119
# File 'lib/chargeio/models/gateway.rb', line 116

def find_transaction(transaction_id, params={})
  response = get(:transactions, transaction_id, params)
  process_response(ChargeIO::Transaction, response)
end

#gatewayObject


318
319
320
# File 'lib/chargeio/models/gateway.rb', line 318

def gateway
  self
end

#ignore_recurring_charge_occurrence(recurring_charge_id, occurrence_id) ⇒ Object


278
279
280
281
# File 'lib/chargeio/models/gateway.rb', line 278

def ignore_recurring_charge_occurrence(recurring_charge_id, occurrence_id)
  response = post("recurring/charges/#{recurring_charge_id}/occurrences/#{occurrence_id}/ignore", nil)
  process_response(ChargeIO::RecurringChargeOccurrence, response)
end

#merchant(params = {}) ⇒ Object

Merchant operations


35
36
37
38
# File 'lib/chargeio/models/gateway.rb', line 35

def merchant(params={})
  response = get(:merchant, params)
  process_response(ChargeIO::Merchant, response)
end

#merchant_accounts(params = {}) ⇒ Object


310
311
312
# File 'lib/chargeio/models/gateway.rb', line 310

def merchant_accounts(params={})
  merchant(params).merchant_accounts
end

#patch_recurring_charge(id, params) ⇒ Object


237
238
239
240
241
242
243
244
245
246
# File 'lib/chargeio/models/gateway.rb', line 237

def patch_recurring_charge(id, params)
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end

  json = params.to_json
  response = patch('recurring/charges', id, json, headers)
  process_response(ChargeIO::RecurringCharge, response)
end

#pay_recurring_charge_occurrence(recurring_charge_id, occurrence_id, params = {}) ⇒ Object


268
269
270
271
272
273
274
275
276
# File 'lib/chargeio/models/gateway.rb', line 268

def pay_recurring_charge_occurrence(recurring_charge_id, occurrence_id, params={})
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end

  response = post("recurring/charges/#{recurring_charge_id}/occurrences/#{occurrence_id}/pay", nil, headers)
  process_response(ChargeIO::RecurringChargeOccurrence, response)
end

#primary_merchant_account(params = {}) ⇒ Object


314
315
316
# File 'lib/chargeio/models/gateway.rb', line 314

def (params={})
  merchant(params).
end

#purge_test_data(params = nil) ⇒ Object

Test data


299
300
301
302
303
304
305
306
307
# File 'lib/chargeio/models/gateway.rb', line 299

def purge_test_data(params=nil)
  headers = {}
  if params and params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end

  response = post('merchant/purge-test-data', params.to_json, headers)
  process_response(nil, response)
end

#recurring_charge_occurrences(recurring_charge_id, params = {}) ⇒ Object


258
259
260
261
# File 'lib/chargeio/models/gateway.rb', line 258

def recurring_charge_occurrences(recurring_charge_id, params={})
  response = get("recurring/charges/#{recurring_charge_id}/occurrences", nil, params)
  process_list_response(ChargeIO::RecurringChargeOccurrence, response, 'results')
end

#recurring_charges(params = {}) ⇒ Object

Recurring charges


205
206
207
208
# File 'lib/chargeio/models/gateway.rb', line 205

def recurring_charges(params={})
  response = get('recurring/charges', nil, params)
  process_list_response(ChargeIO::RecurringCharge, response, 'results')
end

#refund(transaction_id, amount, params = {}) ⇒ Object


163
164
165
166
167
168
169
170
171
172
# File 'lib/chargeio/models/gateway.rb', line 163

def refund(transaction_id, amount, params={})
  headers = {}
  amount_value, amount_currency = amount_to_parts(amount)
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  transaction_params = params.merge(:amount => amount_value, :currency => amount_currency)
  response = post("charges/#{transaction_id}/refund", transaction_params.to_json, headers)
  process_response(ChargeIO::Refund, response)
end

#sign(transaction_id, signature, gratuity = nil, mime_type = 'chargeio/jsignature', params = {}) ⇒ Object


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/chargeio/models/gateway.rb', line 185

def sign(transaction_id, signature, gratuity=nil, mime_type='chargeio/jsignature', params={})
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  transaction_params = params.merge(:mime_type => mime_type, :data => signature)
  if (gratuity)
    transaction_params[:gratuity] = gratuity
  end
  response = post("transactions/#{transaction_id}/sign", transaction_params.to_json, headers)
  process_response(ChargeIO::Transaction, response)
end

#siteObject


322
323
324
# File 'lib/chargeio/models/gateway.rb', line 322

def site
  @site
end

#transactions(params = {}) ⇒ Object


111
112
113
114
# File 'lib/chargeio/models/gateway.rb', line 111

def transactions(params={})
  response = get(:transactions, nil, params)
  process_list_response(ChargeIO::Transaction, response, 'results')
end

#update_ach_account(account_id, params) ⇒ Object


52
53
54
55
56
# File 'lib/chargeio/models/gateway.rb', line 52

def (, params)
   = params.to_json
  response = put('ach-accounts', , )
  process_response(ChargeIO::AchAccount, response)
end

#update_merchant(params = {}) ⇒ Object


40
41
42
43
44
# File 'lib/chargeio/models/gateway.rb', line 40

def update_merchant(params={})
  merchant_json = params.to_json
  response = put(:merchant, nil, merchant_json)
  process_response(ChargeIO::Merchant, response)
end

#update_merchant_account(account_id, params) ⇒ Object


46
47
48
49
50
# File 'lib/chargeio/models/gateway.rb', line 46

def (, params)
   = params.to_json
  response = put(:accounts, , )
  process_response(ChargeIO::MerchantAccount, response)
end

#update_recurring_charge(id, params) ⇒ Object


226
227
228
229
230
231
232
233
234
235
# File 'lib/chargeio/models/gateway.rb', line 226

def update_recurring_charge(id, params)
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end

  json = params.to_json
  response = put('recurring/charges', id, json, headers)
  process_response(ChargeIO::RecurringCharge, response)
end

#urlObject


326
327
328
# File 'lib/chargeio/models/gateway.rb', line 326

def url
  @url
end

#void(transaction_id, params = {}) ⇒ Object


143
144
145
146
147
148
149
150
# File 'lib/chargeio/models/gateway.rb', line 143

def void(transaction_id, params={})
  headers = {}
  if params.has_key?(:ip_address)
    headers['X-Relayed-IP-Address'] = params.delete(:ip_address)
  end
  response = post("transactions/#{transaction_id}/void", params.to_json, headers)
  process_response(ChargeIO::Transaction, response)
end