africastalking-ruby 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ class Airtime
2
+ include AfricasTalking
3
+ HTTP_CREATED = 201
4
+ HTTP_OK = 200
5
+
6
+ #Set debug flag to to true to view response body
7
+ def initialize username, apikey
8
+ @username = username
9
+ @apikey = apikey
10
+ end
11
+
12
+ def send options
13
+ recipients = options.collect{ |r| r }
14
+ post_body = {
15
+ 'username' => @username,
16
+ 'recipients' => recipients.to_json
17
+ }
18
+ url = getAirtimeUrl() + "/send"
19
+ response = sendNormalRequest(url, post_body)
20
+ #
21
+ if (@response_code == HTTP_CREATED)
22
+ responses = JSON.parse(response, :quirky_mode =>true)
23
+ if (responses['responses'].length > 0)
24
+ #
25
+ results = responses['responses'].collect{ |result|
26
+ #
27
+ AirtimeResponse.new result['status'], result['phoneNumber'],result['amount'],result['requestId'], result['errorMessage'], result['discount']
28
+ }
29
+ #
30
+ return SendAirtimeResult.new responses["errorMessage"], responses["numSent"], responses["totalAmount"], responses["totalDiscount"], results
31
+ else
32
+ raise AfricasTalkingException, responses['errorMessage']
33
+ end
34
+ else
35
+ raise AfricasTalkingException, response
36
+ end
37
+ end
38
+
39
+ private
40
+ def getAirtimeUrl()
41
+ return getApiHost() + "/version1/airtime"
42
+ end
43
+
44
+ def getApiHost()
45
+ if(@username == "sandbox")
46
+ return "https://api.sandbox.africastalking.com"
47
+ else
48
+ return "https://api.africastalking.com"
49
+ end
50
+ end
51
+ end
52
+ class AirtimeResponse
53
+ attr_reader :amount, :phoneNumber, :requestId, :status, :errorMessage, :discount
54
+ def initialize(status_, number_, amount_, requestId_, errorMessage_, discount_)
55
+ @status = status_
56
+ @phoneNumber = number_
57
+ @amount = amount_
58
+ @requestId = requestId_
59
+ @errorMessage = errorMessage_
60
+ @discount = discount_
61
+ end
62
+ end
63
+ class SendAirtimeResult
64
+ attr_reader :errorMessage, :numSent, :totalAmount, :totalDiscount, :responses
65
+ def initialize errorMessage_, numSent_, totalAmount_, totalDiscount_, responses_
66
+ @errorMessage = errorMessage_
67
+ @numSent = numSent_
68
+ @totalAmount = totalAmount_
69
+ @totalDiscount = totalDiscount_
70
+ @responses = responses_
71
+ end
72
+ end
@@ -0,0 +1,43 @@
1
+ class Application
2
+ include AfricasTalking
3
+ HTTP_CREATED = 201
4
+ HTTP_OK = 200
5
+
6
+ #Set debug flag to to true to view response body
7
+ def initialize username, apikey
8
+ @username = username
9
+ @apikey = apikey
10
+ end
11
+
12
+ def fetchApplicationData
13
+ url = getUserDataUrl() + '?username='+@username+''
14
+ response = sendNormalRequest(url)
15
+ #
16
+ if (@response_code == HTTP_OK )
17
+ result = JSON.parse(response, :quirky_mode =>true)
18
+ return ApplicationDataResponse.new result["balance"]
19
+ else
20
+ raise AfricasTalkingException, response
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def getUserDataUrl()
27
+ return getApiHost() + "/version1/user"
28
+ end
29
+
30
+ def getApiHost()
31
+ if(@username == "sandbox")
32
+ return "https://api.sandbox.africastalking.com"
33
+ else
34
+ return "https://api.africastalking.com"
35
+ end
36
+ end
37
+ end
38
+ class ApplicationDataResponse
39
+ attr_reader :balance
40
+ def initialize balance_
41
+ @balance = balance_
42
+ end
43
+ end
@@ -0,0 +1,436 @@
1
+ class Payments
2
+ include AfricasTalking
3
+ HTTP_CREATED = 201
4
+ HTTP_OK = 200
5
+ #Set debug flag to to true to view response body
6
+ BANK_CODES = {
7
+ 'FCMB_NG' => 234001,
8
+ 'ZENITH_NG' => 234002,
9
+ 'ACCESS_NG' => 234003,
10
+ 'GTBANK_NG' => 234004,
11
+ 'ECOBANK_NG' => 234005,
12
+ 'DIAMOND_NG' => 234006,
13
+ 'PROVIDUS_NG' => 234007,
14
+ 'UNITY_NG' => 234008,
15
+ 'STANBIC_NG' => 234009,
16
+ 'STERLING_NG' => 234010,
17
+ 'PARKWAY_NG' => 234011,
18
+ 'AFRIBANK_NG' => 234012,
19
+ 'ENTREPRISE_NG' => 234013,
20
+ 'FIDELITY_NG' => 234014,
21
+ 'HERITAGE_NG' => 234015,
22
+ 'KEYSTONE_NG' => 234016,
23
+ 'SKYE_NG' => 234017,
24
+ 'STANCHART_NG' => 234018,
25
+ 'UNION_NG' => 234019,
26
+ 'UBA_NG' => 234020,
27
+ 'WEMA_NG' => 234021,
28
+ 'FIRST_NG' => 234022,
29
+ }
30
+ def initialize username, apikey
31
+ @username = username
32
+ @apikey = apikey
33
+ end
34
+
35
+ def mobileCheckout options
36
+ url = getMobilePaymentCheckoutUrl()
37
+ if validateParamsPresence?(options, ['productName', 'phoneNumber', 'currencyCode', 'amount', 'metadata'])
38
+ parameters = {
39
+ 'username' => @username,
40
+ 'productName' => options['productName'],
41
+ 'phoneNumber' => options['phoneNumber'],
42
+ 'currencyCode' => options['currencyCode'],
43
+ 'amount' => options['amount'],
44
+ 'metadata' => options['metadata']
45
+ }
46
+ response = sendJSONRequest(url, parameters)
47
+ end
48
+
49
+ if (@response_code == HTTP_CREATED)
50
+ resultObj = JSON.parse(response, :quirky_mode =>true)
51
+ #
52
+ if (resultObj['status'] == 'PendingConfirmation')
53
+ return MobileCheckoutResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId'], resultObj['providerChannel']
54
+ end
55
+ raise AfricasTalkingException, resultObj['description']
56
+ end
57
+ raise AfricasTalkingException, response
58
+ end
59
+
60
+ def mobileB2B options
61
+ validOptions = validateParamsPresence?(options, ['productName', 'providerData', 'currencyCode', 'amount', 'metadata'])
62
+ validProviderData = validateParamsPresence?(options['providerData'], ['provider', 'destinationAccount', 'destinationChannel', 'transferType'])
63
+ if validOptions && validProviderData
64
+ parameters = {
65
+ 'username' => @username,
66
+ 'productName' => options['productName'],
67
+ 'provider' => options['providerData']['provider'],
68
+ 'destinationChannel' => options['providerData']['destinationChannel'],
69
+ 'destinationAccount' => options['providerData']['destinationAccount'],
70
+ 'transferType' => options['providerData']['transferType'],
71
+ 'currencyCode' => options['currencyCode'],
72
+ 'amount' => options['amount'],
73
+ 'metadata' => options['metadata']
74
+ }
75
+ url = getMobilePaymentB2BUrl()
76
+ response = sendJSONRequest(url, parameters)
77
+ end
78
+
79
+ if (@response_code == HTTP_CREATED)
80
+ resultObj = JSON.parse(response, :quirky_mode =>true)
81
+ #
82
+ return MobileB2BResponse.new resultObj['status'], resultObj['transactionId'], resultObj['transactionFee'], resultObj['providerChannel']
83
+ end
84
+ raise AfricasTalkingException, response
85
+ end
86
+
87
+
88
+ def mobileB2C options
89
+ if validateParamsPresence?(options, ['recipients', 'productName'])
90
+ parameters = {
91
+ 'username' => @username,
92
+ 'productName' => options['productName'],
93
+ 'recipients' => options['recipients']
94
+ }
95
+ url = getMobilePaymentB2CUrl()
96
+ response = sendJSONRequest(url, parameters)
97
+ end
98
+ if (@response_code == HTTP_CREATED)
99
+ resultObj = JSON.parse(response, :quirky_mode =>true)
100
+ if (resultObj['entries'].length > 0)
101
+ results = resultObj['entries'].collect{ |subscriber|
102
+ MobileB2CResponse.new subscriber['provider'], subscriber['phoneNumber'], subscriber['providerChannel'], subscriber['transactionFee'], subscriber['status'], subscriber['value'], subscriber['transactionId']
103
+ }
104
+ #
105
+ return results
106
+ end
107
+
108
+ raise AfricasTalkingException, resultObj['errorMessage']
109
+ end
110
+ raise AfricasTalkingException, response
111
+ end
112
+
113
+ def bankCheckout options
114
+ if validateParamsPresence?(options, ['bankAccount', 'productName', 'currencyCode', 'amount', 'narration', 'metadata'])
115
+ parameters = {
116
+ 'username' => @username,
117
+ 'productName' => options['productName'],
118
+ 'bankAccount' => options['bankAccount'],
119
+ 'currencyCode' => options['currencyCode'],
120
+ 'amount' => options['amount'],
121
+ 'narration' => options['narration'],
122
+ 'metadata' => options['metadata']
123
+ }
124
+ url = getBankChargeCheckoutUrl()
125
+ response = sendJSONRequest(url, parameters)
126
+ end
127
+ if (@response_code == HTTP_CREATED)
128
+ resultObj = JSON.parse(response, :quirky_mode =>true)
129
+ #
130
+ return InitiateBankCheckoutResponse.new resultObj['status'], resultObj['transactionId'], resultObj['description']
131
+ end
132
+ raise AfricasTalkingException, response
133
+ end
134
+
135
+ def validateBankCheckout options
136
+ if validateParamsPresence?(options, ['transactionId', 'otp'])
137
+ parameters = {
138
+ 'username' => @username,
139
+ 'transactionId' => options['transactionId'],
140
+ 'otp' => options['otp']
141
+ }
142
+ #
143
+ url = getValidateBankCheckoutUrl()
144
+ response = sendJSONRequest(url, parameters)
145
+ end
146
+ if (@response_code == HTTP_CREATED)
147
+ resultObj = JSON.parse(response, :quirky_mode =>true)
148
+ return ValidateBankCheckoutResponse.new resultObj['status'], resultObj['description']
149
+ end
150
+ raise AfricasTalkingException, response
151
+ end
152
+
153
+ def bankTransfer options
154
+ if validateParamsPresence?(options, ['productName', 'recipients'])
155
+ parameters = {
156
+ 'username' => @username,
157
+ 'productName' => options['productName'],
158
+ 'recipients' => options['recipients']
159
+ }
160
+ url = getBankTransferRequestUrl()
161
+ response = sendJSONRequest(url, parameters)
162
+ end
163
+ if (@response_code == HTTP_CREATED)
164
+ resultObj = JSON.parse(response, :quirky_mode =>true)
165
+
166
+ if (resultObj['entries'].length > 0)
167
+ results = resultObj['entries'].collect{ |item|
168
+ BankTransferEntries.new item['accountNumber'], item['status'], item['transactionId'], item['transactionFee'], item['errorMessage']
169
+ }
170
+
171
+
172
+ return BankTransferResponse.new results, resultObj['errorMessage']
173
+ end
174
+
175
+ raise AfricasTalkingException, resultObj['errorMessage']
176
+ end
177
+ raise AfricasTalkingException, response
178
+
179
+ end
180
+
181
+ def cardCheckout options
182
+ if validateParamsPresence?(options, ['productName', 'currencyCode', 'amount', 'narration', 'metadata'])
183
+ parameters = {
184
+ 'username' => @username,
185
+ 'productName' => options['productName'],
186
+ 'currencyCode' => options['currencyCode'],
187
+ 'amount' => options['amount'],
188
+ 'narration' => options['narration'],
189
+ 'metadata' => options['metadata']
190
+ }
191
+ if (options['checkoutToken'] == nil && options['paymentCard'] == nil)
192
+ raise AfricasTalkingException "Please make sure either the checkoutToken or paymentCard parameter is not empty"
193
+ elsif (options['checkoutToken'] != nil && options['paymentCard'] != nil)
194
+ raise AfricasTalkingException "If you have a checkoutToken please make sure paymentCard parameter is empty"
195
+ end
196
+ if (options['checkoutToken'] != nil)
197
+ parameters['checkoutToken'] = options['checkoutToken']
198
+ end
199
+ if (options['paymentCard'] != nil)
200
+ parameters['paymentCard'] = options['paymentCard']
201
+ end
202
+ url = getCardCheckoutChargeUrl()
203
+ response = sendJSONRequest(url, parameters)
204
+ end
205
+ if (@response_code == HTTP_CREATED)
206
+ resultObj = JSON.parse(response, :quirky_mode =>true)
207
+ #
208
+ return InitiateCardCheckoutResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId']
209
+ end
210
+ raise AfricasTalkingException, response
211
+
212
+ end
213
+
214
+ def validateCardCheckout options
215
+ if validateParamsPresence?(options, ['transactionId', 'otp'])
216
+ parameters = {
217
+ 'username' => @username,
218
+ 'transactionId' => options['transactionId'],
219
+ 'otp' => options['otp']
220
+ }
221
+ url = getValidateCardCheckoutUrl()
222
+ #
223
+ response = sendJSONRequest(url, parameters)
224
+ end
225
+ if (@response_code == HTTP_CREATED)
226
+ resultObj = JSON.parse(response, :quirky_mode =>true)
227
+ return ValidateCardCheckoutResponse.new resultObj['status'], resultObj['description'], resultObj['checkoutToken']
228
+ #
229
+ end
230
+ raise AfricasTalkingException, response
231
+ end
232
+
233
+ def walletTransferRequest options
234
+ if validateParamsPresence?(options, ['productName', 'targetProductCode', 'currencyCode', 'amount', 'metadata'])
235
+ parameters = {
236
+ 'username' => @username,
237
+ 'productName' => options['productName'],
238
+ 'targetProductCode' => options['targetProductCode'],
239
+ 'currencyCode' => options['currencyCode'],
240
+ 'amount' => options['amount'],
241
+ 'metadata' => options['metadata']
242
+ }
243
+ url = getWalletTransferUrl()
244
+ response = sendJSONRequest(url, parameters)
245
+ end
246
+ if (@response_code == HTTP_CREATED)
247
+ resultObj = JSON.parse(response, :quirky_mode =>true)
248
+ #
249
+ return WalletTransferResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId']
250
+ end
251
+ raise AfricasTalkingException, response
252
+ end
253
+
254
+ def topupStashRequest options
255
+ if validateParamsPresence?(options, ['productName', 'currencyCode', 'amount', 'metadata'])
256
+ parameters = {
257
+ 'username' => @username,
258
+ 'productName' => options['productName'],
259
+ 'currencyCode' => options['currencyCode'],
260
+ 'amount' => options['amount'],
261
+ 'metadata' => options['metadata']
262
+ }
263
+ url = getTopupStashUrl()
264
+ response = sendJSONRequest(url, parameters)
265
+ end
266
+ if (@response_code == HTTP_CREATED)
267
+ resultObj = JSON.parse(response, :quirky_mode =>true)
268
+ return TopupStashResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId']
269
+ end
270
+ raise AfricasTalkingException, response
271
+ end
272
+
273
+ private
274
+
275
+ def getPaymentHost()
276
+ if(@username == "sandbox")
277
+ return "https://payments.sandbox.africastalking.com"
278
+ else
279
+ return "https://payments.africastalking.com"
280
+ end
281
+ end
282
+
283
+ def getMobilePaymentCheckoutUrl()
284
+ return getPaymentHost() + "/mobile/checkout/request"
285
+ end
286
+
287
+ def getMobilePaymentB2CUrl()
288
+ return getPaymentHost() + "/mobile/b2c/request"
289
+ end
290
+
291
+ def getMobilePaymentB2BUrl()
292
+ return getPaymentHost() + "/mobile/b2b/request"
293
+ end
294
+
295
+ def getBankChargeCheckoutUrl()
296
+ return getPaymentHost() + "/bank/checkout/charge"
297
+ end
298
+
299
+ def getValidateBankCheckoutUrl()
300
+ return getPaymentHost() + "/bank/checkout/validate"
301
+ end
302
+
303
+ def getBankTransferRequestUrl()
304
+ return getPaymentHost() + "/bank/transfer"
305
+ end
306
+
307
+ def getCardCheckoutChargeUrl()
308
+ return getPaymentHost() + "/card/checkout/charge"
309
+ end
310
+
311
+ def getValidateCardCheckoutUrl()
312
+ return getPaymentHost() + "/card/checkout/validate"
313
+ end
314
+
315
+ def getWalletTransferUrl()
316
+ return getPaymentHost() + "/transfer/wallet"
317
+ end
318
+
319
+ def getTopupStashUrl()
320
+ return getPaymentHost() + "/topup/stash"
321
+ end
322
+
323
+ def getApiHost()
324
+ if(@username == "sandbox")
325
+ return "https://api.sandbox.africastalking.com"
326
+ else
327
+ return "https://api.africastalking.com"
328
+ end
329
+ end
330
+
331
+ end
332
+
333
+ class MobileB2CResponse
334
+ attr_reader :provider, :phoneNumber, :providerChannel, :transactionFee, :status, :value, :transactionId
335
+
336
+ def initialize provider_, phoneNumber_, providerChannel_, transactionFee_, status_, value_, transactionId_
337
+ @provider = provider_
338
+ @phoneNumber = phoneNumber_
339
+ @providerChannel = providerChannel_
340
+ @transactionFee = transactionFee_
341
+ @status = status_
342
+ @value = value_
343
+ @transactionId = transactionId_
344
+ end
345
+ end
346
+
347
+ class MobileB2BResponse
348
+ attr_reader :status, :transactionId, :transactionFee, :providerChannel
349
+
350
+ def initialize status_, transactionId_, transactionFee_, providerChannel_
351
+ @providerChannel = providerChannel_
352
+ @transactionId = transactionId_
353
+ @transactionFee = transactionFee_
354
+ @status = status_
355
+ end
356
+ end
357
+
358
+ class BankTransferEntries
359
+ attr_reader :accountNumber, :status, :transactionId, :transactionFee, :errorMessage
360
+ def initialize accountNumber, status, transactionId, transactionFee, errorMessage
361
+ @accountNumber = accountNumber
362
+ @status = status
363
+ @transactionId = transactionId
364
+ @transactionFee = transactionFee
365
+ @errorMessage = errorMessage
366
+ end
367
+ end
368
+
369
+ class BankTransferResponse
370
+ attr_reader :entries, :errorMessage
371
+ def initialize entries_, errorMessage_
372
+ @entries = entries_
373
+ @errorMessage = errorMessage_
374
+ end
375
+ end
376
+
377
+ class MobileCheckoutResponse
378
+ attr_reader :status, :transactionFee, :transactionId, :providerChannel
379
+ def initialize accountNumber_, status_, transactionId_, transactionFee_
380
+ @accountNumber = accountNumber_
381
+ @status = status_
382
+ @transactionId = transactionId_
383
+ @transactionFee = transactionFee_
384
+ end
385
+ end
386
+ class InitiateBankCheckoutResponse
387
+ attr_reader :status, :description, :transactionId
388
+ def initialize status_, transactionId_, description_
389
+ @description = description_
390
+ @status = status_
391
+ @transactionId = transactionId_
392
+ end
393
+ end
394
+ class ValidateBankCheckoutResponse
395
+ attr_reader :status, :description
396
+ def initialize status_, description_
397
+ @description = description_
398
+ @status = status_
399
+ end
400
+ end
401
+
402
+ class InitiateCardCheckoutResponse
403
+ attr_reader :status, :description, :transactionId
404
+ def initialize status_, description_, transactionId_
405
+ @description = description_
406
+ @status = status_
407
+ @transactionId = transactionId_
408
+ end
409
+ end
410
+
411
+ class ValidateCardCheckoutResponse
412
+ attr_reader :status, :description, :checkoutToken
413
+ def initialize status_, description_, checkoutToken_
414
+ @description = description_
415
+ @status = status_
416
+ @checkoutToken = checkoutToken_
417
+ end
418
+ end
419
+
420
+ class WalletTransferResponse
421
+ attr_reader :status, :description, :transactionId
422
+ def initialize status_, description_, transactionId_
423
+ @description = description_
424
+ @status = status_
425
+ @transactionId = transactionId_
426
+ end
427
+ end
428
+
429
+ class TopupStashResponse
430
+ attr_reader :status, :description, :transactionId
431
+ def initialize status_, description_, transactionId_
432
+ @description = description_
433
+ @status = status_
434
+ @transactionId = transactionId_
435
+ end
436
+ end