def __init__(self, name, prices, secret, callback): self.db = Connection()['payment_%s' % name] self.collection = self.db['order'] self.signature = Signature(secret) self.info = Info(prices) self.order = Order(self.collection, callback)
class SignatureTest(BaseTest): def setUp(self): self.signature = Signature('secretkey') self.params = {'c': 'a', 'b': 'v', 'a': 'z'} def test_string(self): self.eq(self.signature.string(self.params), 'a=zb=vc=a' + self.signature.key) def test_md5(self): self.eq(self.signature.md5(self.params), md5(self.signature.string(self.params)).hexdigest()) def test_check(self): self.true(self.signature.check(self.params, self.signature.md5(self.params))) self.false(self.signature.check(self.params, 'qwe'))
class Payment(BillingHandler): def __init__(self, name, prices, secret, callback): self.db = Connection()['payment_%s' % name] self.collection = self.db['order'] self.signature = Signature(secret) self.info = Info(prices) self.order = Order(self.collection, callback) def request(self, args): notification_type = args.get('notification_type') try: if not self.signature.check(args, args.pop('sig')): raise SignatureError() if notification_type.startswith(GET_ITEM): return self.info(args['item']) if notification_type.startswith(ORDER): return self.order(args['order_id'], args['receiver_id'], args['item'], args['status']) except (ItemFormatError, UnknownItemError, InvalidCountError, CallbackError, SignatureError) as error: return error.response()
def setUp(self): self.signature = Signature('secretkey') self.params = {'c': 'a', 'b': 'v', 'a': 'z'}