# Run this script from the command line using# $ python epg_sample.pyfromCrypto.CipherimportAESimportbase64fromhashlibimportsha256importurllib,urllib2# merchant and secret_keymerchant_id='2150'secret='pasteYourMd5SecretKey'epg_endpoint='https://checkout-stg.easypaymentgateway.com/EPGCheckout/rest/online/pay'defencrypt(plaintext,key):# the block size for the cipher object; must be 16, 24, or 32 for AESBLOCK_SIZE=16# one-liner to sufficiently pad the text to be encryptedpad=lambdas:s+(BLOCK_SIZE-len(s)%BLOCK_SIZE)*chr(BLOCK_SIZE-len(s)%BLOCK_SIZE)# one-liner to encrypt a string# encrypt with AES, encode with base64EncodeAES=lambdac,s:base64.b64encode(c.encrypt(pad(s)))# create a cipher object using the secret keycipher=AES.new(key,AES.MODE_ECB)encoded=EncodeAES(cipher,plaintext)print'cyphertext:',encodedreturnencodeddefintegrity_hash(plaintext):hash=sha256(plaintext).hexdigest()print'checksum:',hashreturnhashparams='amount=10¤cy=USD&country=GB&operationType=debit&merchantId='+merchant_idprint'plaintext:',params# encode a stringencoded=encrypt(params,secret)# generate the checksumintegrity_check=integrity_hash(params)payload={'encrypted':encoded,'integrityCheck':integrity_check,'merchantId':merchant_id}print'POST:',urllib.urlencode(payload)print'to endpoint:',epg_endpointrequest=urllib2.Request(url=epg_endpoint,data=urllib.urlencode(payload))f=urllib2.urlopen(request)printf.read()
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.