Example #1
0
def order_code_generator():
	#Generates a unique order code by storing all used codes in
	#a file and reading from the list of used codes.
	used_codes = pickle.load( open( "order_code.dat", "rb" ) )
	unique = False

	while True:
		code = random_text_generator()
		if code not in used_codes:
			used_codes.append(code)
			pickle.dump(used_codes, open( "order_code.dat", "wb" ) )
			return code
    def test_P4A_confirmation(self):

        hash_info = [
            settings.P4A_MERCHANT_ID,
            random_text_generator(),
            random_decimal_generator(),
            settings.P4A_SECRET_KEY,
        ]
        digest = SHA256Hash(hash_info)

        # Check that no duplicate order ids are produced

        response = self.client.get(
            reverse("p4a_confirmation"),
            {
                "merchant": hash_info[0],
                "checkout": 364775,
                "amount": hash_info[2],
                "email": "*****@*****.**",
                "phone": "98349",
                "order": hash_info[1],
                "timestamp": int(time.time()),
                "digest": digest,
            },
        )

        # Confirm the server responded and created an object with the right digest.
        self.assertEquals(response.status_code, 200)
        self.assertTrue(CallBackNotification.objects.get(digest=digest))
        # Check that confirmational JSON token was produced
        self.assertEquals(ast.literal_eval(response.content), {"status": 1})

        hash_info = [
            settings.P4A_MERCHANT_ID,
            random_text_generator(),
            random_decimal_generator(),
            settings.P4A_SECRET_KEY,
        ]
        digest = SHA256Hash(hash_info)

        response = self.client.get(
            reverse("p4a_confirmation"),
            {
                "merchant": hash_info[0],
                "amount": hash_info[2],
                "order": hash_info[1],
                "timestamp": int(time.time()),
                "digest": digest,
            },
        )
        self.assertEquals(response.status_code, 200)
        self.assertTrue(CallBackNotification.objects.get(digest=digest))
        # Check that confirmational JSON token was produced
        self.assertEquals(ast.literal_eval(response.content), {"status": 1})

        response = self.client.get(
            reverse("p4a_confirmation"),
            {
                "merchant": hash_info[0],
                "amount": hash_info[2],
                "order": hash_info[1],
                "timestamp": "pie",
                "digest": digest,
            },
        )
        self.assertEquals(response.status_code, 200)
        # Check that error JSON token was produced
        self.assertEquals(ast.literal_eval(response.content), {"status": 0})