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, Connection::USER_AGENT

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


358
359
360
# File 'lib/chargeio/models/gateway.rb', line 358

def auth
  @auth
end

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


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

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


128
129
130
131
# File 'lib/chargeio/models/gateway.rb', line 128

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

#cancel_recurring_charge(id) ⇒ Object


281
282
283
284
# File 'lib/chargeio/models/gateway.rb', line 281

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

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


180
181
182
183
184
185
186
187
188
189
# File 'lib/chargeio/models/gateway.rb', line 180

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


107
108
109
110
# File 'lib/chargeio/models/gateway.rb', line 107

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

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


155
156
157
158
159
160
161
162
163
164
# File 'lib/chargeio/models/gateway.rb', line 155

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


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

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

#create_card(params = {}) ⇒ Object


91
92
93
94
95
# File 'lib/chargeio/models/gateway.rb', line 91

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


243
244
245
246
247
248
249
250
251
252
# File 'lib/chargeio/models/gateway.rb', line 243

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 = {}, encoding = 'json') ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'lib/chargeio/models/gateway.rb', line 76

def create_token(params={}, encoding='json')
  response = nil
  if encoding == 'form'
    response = form_post(:tokens, params)
  else
    response = post(:tokens, params.to_json)
  end
  process_response(ChargeIO::Token, response)
end

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


202
203
204
205
206
207
208
209
210
211
# File 'lib/chargeio/models/gateway.rb', line 202

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


118
119
120
121
# File 'lib/chargeio/models/gateway.rb', line 118

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

#delete_card(token) ⇒ Object


97
98
99
100
# File 'lib/chargeio/models/gateway.rb', line 97

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

#delete_recurring_charge(id) ⇒ Object


276
277
278
279
# File 'lib/chargeio/models/gateway.rb', line 276

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

#events(params = {}) ⇒ Object

Events


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

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

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


123
124
125
126
# File 'lib/chargeio/models/gateway.rb', line 123

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

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


102
103
104
105
# File 'lib/chargeio/models/gateway.rb', line 102

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

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


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

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

#find_recurring_charge(id) ⇒ Object


238
239
240
241
# File 'lib/chargeio/models/gateway.rb', line 238

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


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

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


226
227
228
229
# File 'lib/chargeio/models/gateway.rb', line 226

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

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


86
87
88
89
# File 'lib/chargeio/models/gateway.rb', line 86

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

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


139
140
141
142
# File 'lib/chargeio/models/gateway.rb', line 139

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

#gatewayObject


346
347
348
# File 'lib/chargeio/models/gateway.rb', line 346

def gateway
  self
end

#holds(params = {}) ⇒ Object


166
167
168
169
# File 'lib/chargeio/models/gateway.rb', line 166

def holds(params={})
  response = get('charges/holds', nil, params)
  process_list_response(ChargeIO::Charge, response, 'results')
end

#ignore_recurring_charge_occurrence(recurring_charge_id, occurrence_id) ⇒ Object


306
307
308
309
# File 'lib/chargeio/models/gateway.rb', line 306

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


338
339
340
# File 'lib/chargeio/models/gateway.rb', line 338

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

#patch_ach_account(account_id, params) ⇒ Object


70
71
72
73
74
# File 'lib/chargeio/models/gateway.rb', line 70

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

#patch_merchant(params = {}) ⇒ Object


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

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

#patch_merchant_account(account_id, params) ⇒ Object


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

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

#patch_recurring_charge(id, params) ⇒ Object


265
266
267
268
269
270
271
272
273
274
# File 'lib/chargeio/models/gateway.rb', line 265

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


296
297
298
299
300
301
302
303
304
# File 'lib/chargeio/models/gateway.rb', line 296

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


342
343
344
# File 'lib/chargeio/models/gateway.rb', line 342

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

#purge_test_data(params = nil) ⇒ Object

Test data


327
328
329
330
331
332
333
334
335
# File 'lib/chargeio/models/gateway.rb', line 327

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


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

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


233
234
235
236
# File 'lib/chargeio/models/gateway.rb', line 233

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

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


191
192
193
194
195
196
197
198
199
200
# File 'lib/chargeio/models/gateway.rb', line 191

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


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

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


350
351
352
# File 'lib/chargeio/models/gateway.rb', line 350

def site
  @site
end

#transactions(params = {}) ⇒ Object


134
135
136
137
# File 'lib/chargeio/models/gateway.rb', line 134

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

#update_ach_account(account_id, params) ⇒ Object


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

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


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

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

#update_recurring_charge(id, params) ⇒ Object


254
255
256
257
258
259
260
261
262
263
# File 'lib/chargeio/models/gateway.rb', line 254

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


354
355
356
# File 'lib/chargeio/models/gateway.rb', line 354

def url
  @url
end

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


171
172
173
174
175
176
177
178
# File 'lib/chargeio/models/gateway.rb', line 171

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