3
\$\begingroup\$

I've tried making a payment system for an API called Payson, which is a European company similar to PayPal.

I get the right response when I press the button so it seems to work, and I wonder if you can look at my code to see if it looks OK. I didn't test the part receive but the part send i.e. the first part I've testet and it appears to work:

class PaysonHandler(webapp2.RequestHandler):

    def get(self):
        """
............Returns a simple HTML form for Payson
........"""
        logging.info('in payson')
        SellerEmail = '[email protected]'
        Cost = 250
        ExtraCost=0
        GuaranteeOffered=2
        OkUrl = self.request.host+"/payson_okurl" # TO DO
        Key = '319033-6152-402-b95a-37430de6b6'
        text = SellerEmail + ':' + str(Cost) + ':' + str(ExtraCost) + ':' + OkUrl + ':' + str(GuaranteeOffered) + Key
        logging.info('mdtext5: '+text)
        m = hashlib.md5()
        Generated_MD5_Hash_Value = hashlib.md5(text).hexdigest()
        BuyerEmail = '[email protected]'
        AgentID = 11366
        path = os.path.join(os.path.dirname(__file__), 'templates',
                            'payson.html')
        self.response.out.write(template.render(path, {
            'SellerEmail':SellerEmail, 'Cost':Cost,  'Cost':Cost, 'AgentID':AgentID,
                  'ExtraCost':ExtraCost, 'GuaranteeOffered':GuaranteeOffered,  'OkUrl':OkUrl, 'Key':Key,  'Generated_MD5_Hash_Value':Generated_MD5_Hash_Value,  'BuyerEmail':BuyerEmail,    }))

class PaysonReceiveHandler(webapp2.RequestHandler):

    def get(self):
        """
............Receives Payson messages. Not tested
........"""
        logging.info('in payson')
        SellerEmail = '[email protected]'
        Cost = 10
        ExtraCost=0
        GuaranteeOffered=2
        OkUrl = self.request.host+'/payson_okurl' # TO DO
        text = SellerEmail + ':' + str(Cost) + ':' + str(ExtraCost) + ':' + OkUrl + ':' + str(GuaranteeOffered) + Key
        logging.info('mdtext5: '+text)
        m = hashlib.md5()
        Generated_MD5_Hash_Value = hashlib.md5(text).hexdigest()
        AgentID = 11366

        strYourSecretKey = "3190fb33-6152-4052-b95a-379b430de6b6";
        strOkURL = self.request.GET.get('OkURL')
        strRefNr = self.request.GET.get('RefNr')
        strPaysonRef = self.request.GET.get('Paysonref')

        strTestMD5String = strOkURL + strPaysonRef + strYourSecretKey
        strMD5Hash = hashlib.md5(strTestMD5String).hexdigest()  

        logging.info('testing MD5 value')
        if strMD5Hash == self.request.GET.get('MD5'):
            logging.info('Korrekt anrop från Payson')
            logging.info('Rätt!')
            logging.info('OkUrl: ') + strOkURL
            logging.info('RefNr: ') + strRefNr
            logging.info('PaysonRef: ') + strPaysonRef
        else:
            #Felaktigt anrop från någon annan.
            logging.info("Felaktig MD5-summa!")
\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

First of all, please make sure that your indentation is correct, especially when posting Python code. Also, take a look at PEP 8. You are violating it in at least the following places:

  • You have tabs, not spaces
  • Comments should be in English
  • It is apparently not directly against PEP 8, but giving locals CapsWords names makes the code harder to understand, in my opinion (especially seeing as SE highlights them a different colour).

You also seem to have an awful lot of duplicate code. Try to factor common operations (such as setting up all those members) into separate functions.

Speaking of which, your functions aren't particularly clear. Why does a method called get not return anything? What is it supposed to get?

You've got plenty of hard-coded values in there. I hope that strYourSecretKey is a placeholder value; the other ones should be passed in as parameters. Could you make it determine the agent ID based on the email? As things are, it seems like this is prone to incorrect combinations of input if anything is changed.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Where does PEP 8 suggest commenting in English? \$\endgroup\$ Commented Jan 11, 2012 at 20:09
  • 1
    \$\begingroup\$ @WinstonEwert: Section Comments, "Python coders from non-English speaking countries: please write your comments in English, unless you are 120% sure that the code will never be read by people who don't speak your language." \$\endgroup\$
    – Komi Golov
    Commented Jan 11, 2012 at 21:08
  • \$\begingroup\$ Many thanks for the contructive criticism. I'm updating the question with a more developed part of the code if you feel like continuing reviewing the same use case (use case is paying to my web shop.) \$\endgroup\$ Commented Jan 21, 2012 at 23:22
  • 2
    \$\begingroup\$ @NickRosencrantz: Feel free to start a new question for the update. Maybe more people will answer an unanswered new question than an answered (and updated) one. \$\endgroup\$
    – palacsint
    Commented Jan 21, 2012 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.