Class: Campay::CamPay

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

Overview

Campay payment class

Instance Method Summary collapse

Constructor Details

#initialize(app_username, app_password, environment) ⇒ CamPay

Returns a new instance of CamPay.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/campay.rb', line 23

def initialize(app_username, app_password, environment)
  @app_username = app_username
  @app_password = app_password
  @environment = environment

  if @environment == "DEV"
    @host = "https://demo.campay.net"
    @debug = true
  else
    @host = "https://campay.net"
    @debug = false
  end
end

Instance Method Details

#collect(amount, currency, from, description, external_reference) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/campay.rb', line 79

def collect(amount, currency, from, description, external_reference)
  puts ">>>>>>>>>>>>>>>>>>:Collecting..." if @debug
  token = fetch_token[:token]

  if token
    #### Request collect
    collect_data = {
      "amount": to_str(amount),
      "currency": to_str(currency),
      "from": to_str(from),
      "description": to_str(description),
      "external_reference": to_str(external_reference)
    }
    collect_payload = MultiJson.dump(collect_data)
    collect_headers = {
      'Authorization': "Token #{token}",
      'Content-Type': "application/json"
    }

    got_json_response = false

    begin
      conn = Faraday.new(
        url: "#{@host}/api/collect/",
        headers: collect_headers
      )
      collect_response = conn.post("#{@host}/api/collect/", collect_payload, collect_headers)
      collect_response_json = collect_response.inspect
      got_json_response = true
      puts collect_response
    rescue StandardError => e
      puts e
    end

    if got_json_response
      puts ">>>>>>>>>>>>>>>>>>: #{collect_response_json}" if @debug
      collect_response_status_code = collect_response.status
      if collect_response_status_code == 200
        take_reference = MultiJson.load(collect_response.body)
        reference = take_reference.values[0]

        puts ">>>>>>>>>>>>>>>>>>: Confirm on phone..."
        is_successful = true
      else
        reference = "None"
        is_successful = false
      end

      if is_successful
        # ###Check Transaction Status
        status_headers = {
          'Authorization': "Token #{token}",
          'Content-Type': "application/json"
        }
        status = "PENDING"
        # trial = 0
        while status == "PENDING"
          sleep(5)
          got_json_response = false
          # trial += 1
          begin
            conn = Faraday.new(
              url: "#{@host}/api/transaction/",
              headers: status_headers
            )
            status_response = conn.get("#{@host}/api/transaction/#{reference}", status_headers)
            status_response_json = status_response.to_json
            got_json_response = true
          rescue StandardError => e
            puts e
          end

          next unless got_json_response

          status_response_status_code = status_response.status
          if status_response_status_code == 200
            status = status_response_json["status"]

            if status != "PENDING"
              puts ">>>>>>>>>>>>>>>>>>: #{status_response_json}" if @debug
              return status_response_json
            end
          else
            puts "Awaiting transaction details!"
          end
          # break if trial == 10
        end
      else
        message = collect_response_json["message"]
        puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
        { "status": "FAILED", "message": message }
      end
    else
      message = "Collect error"
      puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
      { "status": "FAILED", "message": message }
    end
  else
    message = "Token error. Please check your App Username and Pass password. Also check your environment"
    puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
    { "status": "FAILED", "message": message }
  end
end

#disburse(amount, currency, to, description, external_reference) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/campay.rb', line 258

def disburse(amount, currency, to, description, external_reference)
  puts ">>>>>>>>>>>>>>>>>>: Disbursing..." if @debug

  token = fetch_token[:token]
  if token

    #### Request withdraw
    withdraw_data = {
      "amount": to_str(amount),
      "currency": to_str(currency),
      "to": to_str(to),
      "description": to_str(description),
      "external_reference": to_str(external_reference)
    }
    withdraw_payload = MultiJson.dump(withdraw_data)
    withdraw_headers = {
      'Authorization': "Token #{token}",
      'Content-Type': "application/json"
    }
    # got_json_response = false
    begin
      conn = Faraday.new(
        url: "#{@host}/api/withdraw/",
        headers: withdraw_headers
      )
      withdraw_response = conn.post("#{@host}/api/withdraw/", withdraw_payload, withdraw_headers)
      withdraw_response_json = MultiJson.load(withdraw_response.body).values[0]

      got_json_response = true
      puts "The response is#{withdraw_response_json}"

      if got_json_response
        puts ">>>>>>>>>>>>>>>>>>: #{withdraw_response_json}" if @debug
        withdraw_response_status_code = withdraw_response.status
        if withdraw_response_status_code == 200
          reference = withdraw_response_json
          is_successful = true
        else
          reference = "None"
          is_successful = false
        end
        if is_successful
          # ###Check Transaction Status
          status_headers = {
            'Authorization': "Token #{token}",
            'Content-Type': "application/json"
          }

          status = "PENDING"
          while status == "PENDING"
            sleep(5)

            got_json_response = false
            begin
              conn = Faraday.new(
                url: "#{@host}/api/transaction/",
                headers: status_headers
              )
              status_response = conn.get("#{@host}/api/transaction/#{reference}", status_headers)
              status_response_json = MultiJson.dump(status_response)
              got_json_response = true
            rescue StandardError => e
              puts e
            end

            next unless got_json_response

            status_response_status_code = status_response.status
            if status_response_status_code == 200
              status = status_response_json.status

              if status != "PENDING"
                puts ">>>>>>>>>>>>>>>>>>: #{status_response_json}" if @debug
                return status_response_json
              end
            else
              pass
            end
          end

        else
          message = withdraw_response_json
          puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
          { "status": "FAILED", "message": message }
        end
      else
        message = "Disburse error"
        puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
        { "status": "FAILED", "message": message }
      end
    rescue StandardError => e
      puts e
    end
  else
    message = "Token error. Please check your App Username and Pass password. Also check your environment"
    puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
    { "status": "FAILED", "message": message }
  end
end

#fetch_balanceObject



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/campay.rb', line 449

def fetch_balance
  puts ">>>>>>>>>>>>>>>>>>: Getting balance..." if @debug
  token = fetch_token[:token]
  if token
    status_headers = {
      'Authorization': "Token #{token}",
      'Content-Type': "application/json"
    }

    begin
      conn = Faraday.new(
        url: "#{@host}/api/utilities/balance/",
        headers: status_headers
      )
      response = conn.get("#{@host}/api/balance/", status_headers)
      response_json = MultiJson.load(response.body).values[0]
      got_json_response = true
      if got_json_response
        puts ">>>>>>>>>>>>>>>>>>: #{to_str(response_json)}" if @debug
        to_str(response_json)
      else
        message = "Balance error"
        puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
        { "status": "FAILED", "message": message }
      end
    rescue StandardError => e
      puts e
    end
  else
    message = "Token error. Please check your App Username and Pass password. Also check your environment"
    puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
    { "status": "FAILED", "message": message }
  end
end


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/campay.rb', line 183

def fetch_payment_link(amount, currency, description, external_reference, redirect_url)
  puts ">>>>>>>>>>>>>>>>>>: Getting payment link..." if @debug

  token = fetch_token[:token]

  if token

    #### Request collect
    collect_data = {
      "amount": to_str(amount),
      "currency": to_str(currency),
      "description": to_str(description),
      "external_reference": to_str(external_reference),
      "redirect_url": to_str(redirect_url)
    }
    collect_payload = MultiJson.dump(collect_data)
    collect_headers = {
      'Authorization': "Token #{token}",
      'Content-Type': "application/json"
    }
    got_json_response = false

    begin
      conn = Faraday.new(
        url: "#{@host}/api/get_payment_link/",
        headers: collect_headers
      )
      collect_response = conn.post("#{@host}/api/get_payment_link/", collect_payload, collect_headers)

      collect_response_json = collect_response.inspect
      got_json_response = true

      puts "Payment link response #{collect_response_json}"
    rescue StandardError => e
      puts e
    end
    if got_json_response
      puts ">>>>>>>>>>>>>>>>>>: #{collect_response_json}" if @debug

      collect_response_status_code = collect_response.status
      if collect_response_status_code == 200
        link_value = MultiJson.load(collect_response.body)
        link = link_value.values[0]
        puts "The link is #{link}"
        if @debug
          puts ">>>>>>>>>>>>>>>>>>: Redirect your customer to the payment link..."
          is_successful = true
        end
      else
        link = "None"
        is_successful = false
      end

      if is_successful
        # ###Check Transaction Status
        { "status": "SUCCESSFUL", "link": link }

      else
        message = collect_response_json
        puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug

        { "status": "FAILED", "message": message }
      end
    else
      message = "Collect error"
      puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
      { "status": "FAILED", "message": message }
    end
  else
    message = "Token error. Please check your App Username and Pass password. Also check your environment"
    puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
    { "status": "FAILED", "message": message }
  end
end

#fetch_tokenObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/campay.rb', line 37

def fetch_token
  token_headers = {
    "Content-Type": "application/json"
  }

  data = {
    "username": @app_username,
    "password": @app_password
  }
  json_data = MultiJson.dump(data)
  got_json_response = false

  begin
    conn = Faraday.new(
      url: "#{@host}/api/token/",
      headers: token_headers
    )
    token_response = conn.post("#{@host}/api/token/", json_data, token_headers)
    json_token_response = MultiJson.dump(token_response)

    got_json_response = true
    puts got_json_response
  rescue StandardError => e
    puts e
  end
  if got_json_response
    token_response_response_status_code = token_response.status
    if token_response_response_status_code == 200
      take_token = JSON.parse(token_response.body)
      token = take_token.values[0]
      is_successful = true
    else
      token = "None"
      is_successful = false
      puts json_token_response if @debug
    end
    { "token": token, "is_successful": is_successful }
  else
    { "token": "None", "is_successful": false }
  end
end

#transfer_airtime(amount, to, external_reference) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/campay.rb', line 358

def transfer_airtime(amount, to, external_reference)
  puts ">>>>>>>>>>>>>>>>>>: Airtime Transfer..." if @debug
  token = fetch_token[:token]
  if token
    #### Request airtime
    airtime_data = {
      "amount": to_str(amount),
      "to": to_str(to),
      "external_reference": to_str(external_reference)
    }
    airtime_payload = MultiJson.dump(airtime_data)
    airtime_headers = {
      'Authorization': "Token #{token}",
      'Content-Type': "application/json"
    }
    got_json_response = false
    begin
      conn = Faraday.new(
        url: "#{@host}/api/utilities/airtime/transfer/",
        headers: airtime_headers
      )
      airtime_response = conn.post("#{@host}/api/utilities/airtime/transfer/", airtime_payload, airtime_headers)
      airtime_response_json = MultiJson.load(airtime_response.body).values[0]
      got_json_response = true
    rescue StandardError => e
      puts e
    end
    if got_json_response
      puts ">>>>>>>>>>>>>>>>>>: #{airtime_response_json}" if @debug
      airtime_response_status_code = airtime_response.status
      if airtime_response_status_code == 200
        reference = airtime_response_json
        is_successful = true
      else
        reference = "None"
        is_successful = false
      end
      if is_successful
        # ###Check Transaction Status
        status_headers = {
          'Authorization': "Token #{token}",
          'Content-Type': "application/json"
        }

        status = "PENDING"
        while status == "PENDING"
          sleep(5)

          got_json_response = false
          begin
            conn = Faraday.new(
              url: "#{@host}/api/utilities/transaction/",
              headers: airtime_headers
            )
            status_response = conn.get("#{@host}/api/utilities/transaction/#{reference}/", status_headers)
            status_response_json = MultiJson.dump(status_response)
            got_json_response = true
          rescue StandardError => e
            puts e
          end
          next unless got_json_response

          status_response_status_code = status_response.status
          if status_response_status_code == 200
            status = status_response_json

            if status != "PENDING"
              puts ">>>>>>>>>>>>>>>>>>: #{status_response_json}" if @debug
              return status_response_json
            end
          else
            pass
          end
        end
      else
        message = airtime_response_json
        puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
        { "status": "FAILED", "message": message }
      end
    else
      message = "Disburse error"
      puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
      { "status": "FAILED", "message": message }
    end
  else
    message = "Token error. Please check your App Username and Pass password. Also check your environment"
    puts ">>>>>>>>>>>>>>>>>>: #{message}" if @debug
    { "status": "FAILED", "message": message }
  end
end