Class: Payments

Inherits:
Object
  • Object
show all
Includes:
AfricasTalking
Defined in:
lib/AfricasTalking/Payments.rb

Constant Summary collapse

HTTP_CREATED =
201
HTTP_OK =
200
BANK_CODES =
{
	'FCMB_NG' => 234001,
	'ZENITH_NG' => 234002,
	'ACCESS_NG' => 234003,
	'GTBANK_NG' => 234004,
	'ECOBANK_NG' => 234005,
	'DIAMOND_NG' => 234006,
	'PROVIDUS_NG' => 234007,
	'UNITY_NG' => 234008,
	'STANBIC_NG' => 234009,
	'STERLING_NG' => 234010,
	'PARKWAY_NG' => 234011,
	'AFRIBANK_NG' => 234012,
	'ENTREPRISE_NG' => 234013,
	'FIDELITY_NG' => 234014,
	'HERITAGE_NG' => 234015,
	'KEYSTONE_NG' => 234016,
	'SKYE_NG' => 234017,
	'STANCHART_NG' => 234018,
	'UNION_NG' => 234019,
	'UBA_NG' => 234020,
	'WEMA_NG' => 234021,
	'FIRST_NG' => 234022,
}
PROVIDERS =
{
	'MPESA'   => 'Mpesa',
	'SEGOVIA' => 'Segovia',
	'FLUTTERWAVE' => 'Flutterwave',
	'ADMIN' => 'Admin',
	'ATHENA' => 'Athena',
}

Constants included from AfricasTalking

AfricasTalking::DEBUG, AfricasTalking::VERSION

Instance Method Summary collapse

Constructor Details

#initialize(username, apikey) ⇒ Payments

Returns a new instance of Payments.



36
37
38
39
# File 'lib/AfricasTalking/Payments.rb', line 36

def initialize username, apikey
	@username    = username
	@apikey      = apikey
end

Instance Method Details

#bankCheckoutCharge(options) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/AfricasTalking/Payments.rb', line 141

def bankCheckoutCharge options
	validateParamsPresence? options, %w(bankAccount productName currencyCode amount narration metadata)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'bankAccount'  => options['bankAccount'],
		'currencyCode' => options['currencyCode'],
		'amount' => options['amount'],
		'narration' => options['narration'],
		'metadata' => options['metadata']
	}
	url      = getBankChargeCheckoutUrl()
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		# 
		return InitiateBankCheckoutResponse.new resultObj['status'], resultObj['transactionId'], resultObj['description']
	end
	raise AfricasTalkingException, response
end

#bankCheckoutValidate(options) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/AfricasTalking/Payments.rb', line 162

def bankCheckoutValidate options
	validateParamsPresence? options, %w(transactionId otp)
	parameters = {
		'username'    => @username,
		'transactionId' => options['transactionId'],
		'otp'  => options['otp']
	}
	# 
	url      = getValidateBankCheckoutUrl()
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		return ValidateBankCheckoutResponse.new resultObj['status'], resultObj['description']
	end
	raise AfricasTalkingException, response
end

#bankTransfer(options) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/AfricasTalking/Payments.rb', line 179

def bankTransfer options
	validateParamsPresence? options, %w(productName recipients)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'recipients'  => options['recipients']
	}
	url      = getBankTransferRequestUrl()
	response = sendJSONRequest(url, parameters)		
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)

		if (resultObj['entries'].length > 0)
			results = resultObj['entries'].collect{ |item|
				BankTransferEntries.new item['accountNumber'], item['status'], item['transactionId'], item['transactionFee'], item['errorMessage']
			}
			

			return BankTransferResponse.new results, resultObj['errorMessage']
		end

		raise AfricasTalkingException, resultObj['errorMessage']
	end
	raise AfricasTalkingException, response
	
end

#cardCheckoutCharge(options) ⇒ Object



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
# File 'lib/AfricasTalking/Payments.rb', line 206

def cardCheckoutCharge options
	validateParamsPresence? options, %w(productName currencyCode amount narration metadata)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'currencyCode' => options['currencyCode'],
		'amount' => options['amount'],
		'narration' => options['narration'],
		'metadata' => options['metadata']
	}
	if (options['checkoutToken'] == nil && options['paymentCard'] == nil)
		raise AfricasTalkingException, "Please make sure either the checkoutToken or paymentCard parameter is not empty"
	elsif (options['checkoutToken'] != nil && options['paymentCard'] != nil)
		raise AfricasTalkingException, "If you have a checkoutToken please make sure paymentCard parameter is empty"
	end
	if (options['checkoutToken'] != nil)
		parameters['checkoutToken'] = options['checkoutToken']
	end
	if (options['paymentCard'] != nil)
		if validateParamsPresence?(options['paymentCard'], ['number', 'cvvNumber', 'expiryMonth', 'expiryYear', 'countryCode', 'authToken'])
			parameters['paymentCard'] = options['paymentCard']
		end
	end
	url      = getCardCheckoutChargeUrl()
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		# 
		return InitiateCardCheckoutResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId']
	end
	raise AfricasTalkingException, response

end

#cardCheckoutValidate(options) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/AfricasTalking/Payments.rb', line 240

def cardCheckoutValidate options
	validateParamsPresence? options, %w(transactionId otp)
	parameters = {
		'username'    => @username,
		'transactionId' => options['transactionId'],
		'otp'  => options['otp']
	}
	url      = getValidateCardCheckoutUrl()
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		return ValidateCardCheckoutResponse.new resultObj['status'], resultObj['description'], resultObj['checkoutToken']
		# 
	end
	raise AfricasTalkingException, response
end

#fetchProductTransactions(options) ⇒ Object



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
# File 'lib/AfricasTalking/Payments.rb', line 295

def fetchProductTransactions options
	validateParamsPresence? options, %w(productName filters)
	filters = options['filters']
	validateParamsPresence? filters, %w(pageNumber count)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'pageNumber' => filters['pageNumber'],
		'count' => filters['count']
	}
	parameters['startDate'] = filters['startDate'] if !(filters['startDate'].nil? || filters['startDate'].empty?)
	parameters['endDate'] = filters['endDate'] if !(filters['endDate'].nil? || filters['endDate'].empty?)
	parameters['category'] = filters['category'] if !(filters['category'].nil? || filters['category'].empty?)
	parameters['status'] = filters['status'] if !(filters['status'].nil? || filters['status'].empty?)
	parameters['source'] = filters['source'] if !(filters['source'].nil? || filters['source'].empty?)
 	parameters['destination'] = filters['destination'] if !(filters['destination'].nil? || filters['destination'].empty?)
	parameters['providerChannel'] = filters['providerChannel'] if !(filters['providerChannel'].nil? || filters['providerChannel'].empty?)
	url      = getFetchTransactionsUrl() 
	response = sendJSONRequest(url, parameters, true)
	if (@response_code == HTTP_OK)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		results = []
		if (resultObj['responses'].length > 0)
			results = resultObj['responses'].collect{ |item|
				FetchTransactionsEntries.new item['sourceType'], item['source'], item['provider'], item['destinationType'], item['description'], item['providerChannel'], item['providerMetadata'],
				item['status'], item['productName'], item['category'], item['destination'], item['value'], item['transactionId'], item['creationTime'], item['requestMetadata']  
			}
		end
		return FetchTransactionsResponse.new resultObj['status'], resultObj['description'], results
	end
	raise AfricasTalkingException, response
end

#fetchWalletBalanceObject



381
382
383
384
385
386
387
388
389
390
391
# File 'lib/AfricasTalking/Payments.rb', line 381

def fetchWalletBalance
	parameters = { 'username' => @username }
	url        = getFetchWalletBalanceUrl()
	response = sendJSONRequest(url, parameters, true)
	if (@response_code == HTTP_OK)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		
		return FetchWalletBalanceResponse.new resultObj['status'], resultObj['balance']
	end
	raise AfricasTalkingException, response
end

#fetchWalletTransactions(options) ⇒ Object



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
357
358
# File 'lib/AfricasTalking/Payments.rb', line 328

def fetchWalletTransactions options
	validateParamsPresence? options, ['filters']
	filters = options['filters']
	validateParamsPresence? filters, %w(pageNumber count)
	parameters = {
		'username'    => @username,
		'pageNumber' => filters['pageNumber'],
		'count' => filters['count']
	}
	parameters['startDate'] = filters['startDate'] if !(filters['startDate'].nil? || filters['startDate'].empty?)
	parameters['endDate'] = filters['endDate'] if !(filters['endDate'].nil? || filters['endDate'].empty?)
	parameters['categories'] = filters['categories'] if !(filters['categories'].nil? || filters['categories'].empty?)
	url      = getFetchWalletUrl() 
	response = sendJSONRequest(url, parameters, true)
	if (@response_code == HTTP_OK)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		results = []
		if (resultObj['responses'].length > 0)
			results = resultObj['responses'].collect{ |item|
				transactionData = TransactionData.new item['transactionData']['requestMetadata'], item['transactionData']['sourceType'],
				item['transactionData']['source'], item['transactionData']['provider'], item['transactionData']['destinationType'],item['transactionData']['description'],
				item['transactionData']['providerChannel'], item['transactionData']['providerRefId'], item['transactionData']['providerMetadata'],item['transactionData']['status'],
				item['transactionData']['productName'], item['transactionData']['category'], item['transactionData']['transactionDate'], item['transactionData']['destination'],
				item['transactionData']['value'], item['transactionData']['transactionId'], item['transactionData']['creationTime']
				FetchWalletEntries.new item['description'], item['balance'], item['date'], item['category'], item['value'], item['transactionId'], transactionData 
			}
		end
		return FetchWalletResponse.new resultObj['status'], resultObj['description'], results
	end
	raise AfricasTalkingException, response
end

#findTransaction(options) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/AfricasTalking/Payments.rb', line 360

def findTransaction options
	validateParamsPresence? options, ['transactionId']
	parameters = {
		'username'    => @username,
		'transactionId' => options['transactionId']
	}
	url      = getFindTransactionUrl() 
	response = sendJSONRequest(url, parameters, true)
	if (@response_code == HTTP_OK)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		transactionData = nil
		if resultObj['status'] === 'Success'
			transactionData = TransactionData.new resultObj['data']['requestMetadata'], resultObj['data']['sourceType'],resultObj['data']['source'], resultObj['data']['provider'], resultObj['data']['destinationType'],resultObj['data']['description'],
				resultObj['data']['providerChannel'], resultObj['data']['providerRefId'], resultObj['data']['providerMetadata'],resultObj['data']['status'], resultObj['data']['productName'], resultObj['data']['category'], 
				resultObj['data']['transactionDate'], resultObj['data']['destination'], resultObj['data']['value'], resultObj['data']['transactionId'], resultObj['data']['creationTime']
		end
		return FindTransactionResponse.new resultObj['status'], transactionData
	end
	raise AfricasTalkingException, response
end

#mobileB2B(options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/AfricasTalking/Payments.rb', line 65

def mobileB2B options
	validateParamsPresence? options, %w(productName providerData currencyCode amount metadata)
	validateParamsPresence? options['providerData'], %w(provider destinationAccount destinationChannel transferType)
	parameters = {
		'username'           => @username,
		'productName'        => options['productName'],
		'provider'           => options['providerData']['provider'],
		'destinationChannel' => options['providerData']['destinationChannel'],
		'destinationAccount' => options['providerData']['destinationAccount'],
		'transferType'       => options['providerData']['transferType'],
		'currencyCode'       => options['currencyCode'],
		'amount'             => options['amount'],
		'metadata'           => options['metadata']
	}
	url      = getMobilePaymentB2BUrl()   
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		# 
		return MobileB2BResponse.new resultObj['status'], resultObj['transactionId'], resultObj['transactionFee'], resultObj['providerChannel'], resultObj['errorMessage']
	end
	raise AfricasTalkingException, response
end

#mobileB2C(options) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/AfricasTalking/Payments.rb', line 90

def mobileB2C options
	validateParamsPresence? options, %w(recipients productName)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'recipients'  => options['recipients']
	}
	url      = getMobilePaymentB2CUrl()
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		if (resultObj['entries'].length > 0)
			results = resultObj['entries'].collect{ |subscriber|
				MobileB2CResponse.new subscriber['provider'], subscriber['phoneNumber'], subscriber['providerChannel'], subscriber['transactionFee'], subscriber['status'], subscriber['value'], subscriber['transactionId'], subscriber['errorMessage']
			}
			# 
			return results
		end

		raise AfricasTalkingException, resultObj['errorMessage']
	end
	raise AfricasTalkingException, response
end

#mobileCheckout(options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/AfricasTalking/Payments.rb', line 41

def mobileCheckout options
	validateParamsPresence? options, %w(productName phoneNumber currencyCode amount)
	parameters = {
		'username'     => @username,
		'productName'  => options['productName'],
		'phoneNumber'  => options['phoneNumber'],
		'currencyCode' => options['currencyCode'],
		'amount'       => options['amount']
	}
	parameters['providerChannel'] = options['providerChannel'] if !(options['providerChannel'].nil? || options['providerChannel'].empty?)
	parameters['metadata'] = options['metadata'] if !(options['metadata'].nil? || options['metadata'].empty?)
	url      = getMobilePaymentCheckoutUrl()
	response = sendJSONRequest(url, parameters)
	if @response_code == HTTP_CREATED
		resultObj = JSON.parse(response, :quirky_mode =>true)
		# 
		if (resultObj['status'] == 'PendingConfirmation')
			return MobileCheckoutResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId'], resultObj['providerChannel']
		end
		raise AfricasTalkingException, resultObj['description']
	end
	raise AfricasTalkingException, response
end

#mobileData(options) ⇒ Object



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
# File 'lib/AfricasTalking/Payments.rb', line 114

def mobileData options
		validateParamsPresence? options, %w(productName)
		recipients = options['recipients'].each{|item| 
			validateParamsPresence? item, %w(phoneNumber quantity unit validity metadata)
		}
		parameters = {
			'username'    => @username,
			'productName' => options['productName'],
			'recipients' => recipients,
		}
		url      = getMobileDataUrl()
		response = sendJSONRequest(url, parameters)
		if (@response_code == HTTP_CREATED)
			resultObj = JSON.parse(response, :quirky_mode =>true)
			if (resultObj['entries'].length > 0)
results = resultObj['entries'].collect{ |data|
	MobileDataResponse.new data['phoneNumber'], data['provider'], data['status'], data['transactionId'], data['value'], data['errorMessage']
}
# 
return results
			end

			raise AfricasTalkingException, resultObj['errorMessage']
		end
		raise AfricasTalkingException, response
end

#topupStash(options) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/AfricasTalking/Payments.rb', line 277

def topupStash options
	validateParamsPresence? options, %w(productName currencyCode amount metadata)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'currencyCode' => options['currencyCode'],
		'amount' => options['amount'],
		'metadata' => options['metadata'] 
	}
	url      = getTopupStashUrl() 
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		return TopupStashResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId']
	end
	raise AfricasTalkingException, response
end

#walletTransfer(options) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/AfricasTalking/Payments.rb', line 257

def walletTransfer options
	validateParamsPresence? options, %w(productName targetProductCode currencyCode amount metadata)
	parameters = {
		'username'    => @username,
		'productName' => options['productName'],
		'targetProductCode' => options['targetProductCode'],
		'currencyCode' => options['currencyCode'],
		'amount' => options['amount'],
		'metadata' => options['metadata'] 
	}
	url      = getWalletTransferUrl()
	response = sendJSONRequest(url, parameters)
	if (@response_code == HTTP_CREATED)
		resultObj = JSON.parse(response, :quirky_mode =>true)
		# 
		return WalletTransferResponse.new resultObj['status'], resultObj['description'], resultObj['transactionId']
	end
	raise AfricasTalkingException, response
end