Prerequisites
To use PaymentsDS with the Ruby SDK you need to have:
Receive money from a mobile account to a business account
require 'paymentsds/mpesa'client = Paymentsds::MPesa::Client.new do |config|config.api_key = '<REPLACE>' # API Keyconfig.public_key = '<REPLACE>' # Public Keyconfig.service_provider_code = '<REPLACE>' # input_ServiceProviderCodeendbeginpayment_data = {from: '841234567', # input_CustomerMSISDNreference: '11114', # input_ThirdPartyReferencetransaction: 'T12344CC', # input_TransactionReferenceamount: '10' # input_Amount}result = client.receive(payment_data)if result.success?puts result.dataendrescueputs 'Operation failed'end
Send money from a business account to a mobile account
require 'paymentsds/mpesa'client = Paymentsds::MPesa::Client.new do |config|config.api_key = '<REPLACE>' # API Keyconfig.public_key = '<REPLACE>' # Public Keyconfig.service_provider_code = '<REPLACE>' # input_ServiceProviderCodeendbeginpayment_data = {to: '841234567', # input_CustomerMSISDNreference: '11114', # input_ThirdPartyReferencetransaction: 'T12344CC', # input_TransactionReferenceamount: '10' # input_Amount}result = client.send(payment_data)if result.success?puts result.dataendrescueputs 'Operation failed'end
Send money from a business account to a another business account
require 'paymentsds/mpesa'client = Paymentsds::MPesa::Client.new do |config|config.api_key = '<REPLACE>' # API Keyconfig.public_key = '<REPLACE>' # Public Keyconfig.service_provider_code = '<REPLACE>' # input_ServiceProviderCodeendbeginpayment_data = {to: '979797', # input_ReceiverPartyCodereference: '11114', # input_ThirdPartyReferencetransaction: 'T12344CC', # input_TransactionReferenceamount: '10' # input_Amount}result = client.send(payment_data)if result.success?puts result.dataendrescueputs 'Operation failed'end
Revert a transaction
require 'paymentsds/mpesa'client = Paymentsds::MPesa::Client.new do |config|config.api_key = '<REPLACE>' # API Keyconfig.public_key = '<REPLACE>' # Public Keyconfig.service_provider_code = '<REPLACE>' # input_ServiceProviderCodeconfig.initiator_identifier = '<REPLACE>' # input_InitiatorIdentifier,config.security_identifier = '<REPLACE>' # input_SecurityCredentialendbeginreversion_data = {reference: '11114', # input_ThirdPartyReferencetransaction: 'T12344CC', # input_TransactionReferenceamount: '10' # input_ReversalAmounts}result = client.reversion(payment_data)if result.success?# Handle success scenarioendrescue# Handle failure scenarioend