Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PegPay Python Sample Code
#pip install suds-jurko
#CC=clang sudo -E pip install pycrypto
from suds.client import Client
url="https://test.pegasus.co.ug:8019/pegpaytelecomsapi/PegPayTelecomsApi.asmx?WSDL"
client = Client(url)
print client ## shows the details of this service
from Crypto.Util.asn1 import DerSequence
from Crypto.PublicKey import RSA
from binascii import a2b_base64
import hashlib
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
from base64 import b64encode, b64decode
import datetime
def generate_digital_signature(data_to_sign):
'''
Generate Digital Signature PegPay '''
private_key_loc = 'private.key' ### Certificate Location
key = open(private_key_loc, "r").read()
rsakey = RSA.importKey(key)
##Encode the Data
data_to_sign = data_to_sign.encode('ascii', 'ignore')
## Hash The data
digest = SHA.new()
digest.update(data_to_sign)
## Sign The hashed data
signer = PKCS1_v1_5.new(rsakey)
sign = signer.sign(digest)
return b64encode(sign)
def CallValidatePhoneNumber(TelNo,VendorCode,Password):
signature = generate_digital_signature(TelNo+VendorCode+Password)
result = client.service.ValidatePhoneNumber(TelNo,VendorCode,Password,signature)
print result ## see: restult.txt below
def CallGetAccountBalance():
result = client.service.GetAccountBalance(VendorCode,Password)
print result
def get_transaction_object():
suds_transaction = client.factory.create('{http://PegPayTelecomsApi/}Transaction')
suds_transaction.SessionId = '123'
suds_transaction.VendorCode = ''
suds_transaction.Password = ''
suds_transaction.FromTelecom = 'MTN'
suds_transaction.ToTelecom = 'MTN'
suds_transaction.Telecom = ''
suds_transaction.PaymentCode = '1' #same or different network
suds_transaction.PaymentDate = datetime.datetime.today().strftime('%Y-%m-%d')
suds_transaction.TranAmount = '500'
suds_transaction.TranCharge = '0'
suds_transaction.VendorTranId = '1001'
suds_transaction.ToAccount = '256776843303'
suds_transaction.FromAccount = ''
suds_transaction.TranType = 'PUSH'
suds_transaction.Narration = 'Cart Payment'
suds_transaction.CustomerRef = ''
suds_transaction.CustomerName = 'TEST CUSTOMER'
suds_transaction.DigitalSignature = generate_digital_signature(suds_transaction.CustomerRef+suds_transaction.CustomerName+suds_transaction.FromTelecom+suds_transaction.ToTelecom+suds_transaction.VendorTranId+suds_transaction.VendorCode+suds_transaction.Password+suds_transaction.PaymentDate+suds_transaction.TranType+suds_transaction.PaymentCode+suds_transaction.TranAmount+suds_transaction.FromAccount+suds_transaction.ToAccount)
return suds_transaction
def PostTransaction():
transaction_object = get_transaction_object()
print transaction_object
result =client.service.PostTransaction(transaction_object)
print result
PostTransaction()
#CallValidatePhoneNumber()
#CallGetAccountBalance()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment