Skip to content

Instantly share code, notes, and snippets.

@ikks
Created April 1, 2015 19:49
Show Gist options
  • Save ikks/3ff7a9ccad508266efa9 to your computer and use it in GitHub Desktop.
Save ikks/3ff7a9ccad508266efa9 to your computer and use it in GitHub Desktop.
Payu Helpers for python 2.x .
import hashlib
def payu_rounder(value):
u"""value is a unicode, returns the round according to Payu documentation
to be used by confirmationUrl
>>> payu_rounder(u'150.25')
u'150.2'
>>> payu_rounder(u'150.35')
u'150.4'
>>> payu_rounder(u'150.34')
u'150.3'
>>> payu_rounder(u'150.00')
u'150.0'
"""
new_value = value
if new_value[-3] == u'.':
last_int = int(new_value[-1])
if last_int < 5 or int(new_value[-2]) % 2 == 0:
new_value = new_value[:-2] + new_value[-2]
else:
new_value = new_value[:-2] + str(int(new_value[-2]) + 1)
return new_value
def payu_decimal(value):
u"""value is a unicode, returns the formatting according to Payu
documentation to be used by responseUrl
>>> payu_decimal(u'150.00')
u'150.0'
>>> payu_decimal(u'150.26')
u'150.26'
>>> payu_decimal(u'150.20')
u'150.2'
"""
if (value[-1] == u'0' and value[-3] == u'.'):
return value[:-1]
return value
def payu_signature(values):
u"""formtas an array of unicode values and returns the digest expected by
payu in payment form, responseUrl and confirmationUrl.
>>> payu_signature([u'6u39nqhq8ftd0hlvnjfs66eh8c', u'500238', u'TestPayU04', u'150.0', u'USD', u'6'])
'43e44ad9149e23ee4c16ab0646b4c83b'
>>> payu_signature([u'6u39nqhq8ftd0hlvnjfs66eh8c', u'500238', u'TestPayU05', u'150.26', u'USD', u'4'])
'91382296898d3937ed36cd125d696452'
>>> payu_signature([u'6u39nqhq8ftd0hlvnjfs66eh8c', u'500238', u'TestPayU04', u'150.2', u'USD', u'6'])
'8eebb5af1f32f7a9f930a1038810b854'
>>> payu_signature([u'6u39nqhq8ftd0hlvnjfs66eh8c', u'500238', u'TestPayU04', u'150.4', u'USD', u'6'])
'ce2285970f17bb6ce049b56e6b41a255'
>>> payu_signature([u'6u39nqhq8ftd0hlvnjfs66eh8c', u'500238', u'TestPayU04', u'150.3', u'USD', u'6'])
'01899cb431dd51623f5c94a62b1d2dc7'
>>> payu_signature([u'6u39nqhq8ftd0hlvnjfs66eh8c', u'500238', u'TestPayU', u'3', u'USD'])
'be2f083cb3391c84fdf5fd6176801278'
"""
md5 = hashlib.md5()
md5.update(u'~'.join(values))
return md5.hexdigest()
if __name__ == "__main__":
import doctest
doctest.testmod()
@martharojas2057
Copy link

pagar online

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment