[views] fix bug and replace prints with logger cubicweb-cmcicpay-version-0.2.1
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Thu, 01 Jul 2010 17:08:09 +0200
changeset 16 8b25be0693f8
parent 15 c6477e1537bf
child 17 043a09072bdf
[views] fix bug and replace prints with logger
views.py
--- a/views.py	Thu Jul 01 17:07:35 2010 +0200
+++ b/views.py	Thu Jul 01 17:08:09 2010 +0200
@@ -29,6 +29,13 @@
 def get_tpe(_cw):
     return cmcic.get_tpe(os.path.join(_cw.vreg.config.apphome,'tpe'))
 
+def traceback_as_str():
+    from StringIO import StringIO
+    import traceback
+    out = StringIO()
+    traceback.print_exc(file=out)
+    return out.getvalue()
+
 ## urls
 
 class ConfRewrite(SimpleReqRewriter):
@@ -75,36 +82,36 @@
         try:
             ack = self.handle_msg(tpe)
         except Exception, exc:
-            print exc
+            self.error(traceback_as_str())
+            self.error('form data was: %s' % repr(self._cw.form))
             ack = 1
         ret = 'version=2\ncdr=%s' % ack
-        print 'cmcic returning', ret
+        self.info('cmcic returning %s' % repr(ret))
         return ret
 
 
     def handle_msg(self, tpe):
         params = dict(self._cw.form)
-        print params
+        self.info('cmcic handle_msg, params=%s' % repr(params))
         rep = tpe.read_paymentresponse(params)
         msg, mac = tpe.paymentresponse_msg(rep)
         if tpe.is_valid_msg(msg, mac):
+            self.info('cmcic received valid message %s %s' % (repr(rep.reference), repr(rep.return_code)))
             if rep.return_code == "Annulation":
                 # Payment has been refused
                 # The payment may be accepted later
                 # put your code here (email sending / Database update)
-                print 'cmcic received', rep.reference, rep.return_code
                 return 0
 
             elif rep.return_code in ("payetest", "paiement"):
                 # Payment has been accepeted on the productive server
                 # put your code here (email sending / Database update)
-                print 'cmcic received', rep.reference, rep.return_code
                 comment = u'payed by %(brand)s on %(date)s, auth %(numauto)s' % rep.as_dict()
                 try:
-                    self.appli.repo.shopcart_checkout(rep.reference, comment)
+                    self.appli.repo.shopcart_checkout(int(rep.reference), comment)
                     return 0
                 except ValidationError, exc:
-                    print 'cmcic raised exception trying to checkout cart', exc
+                    self.error(traceback_as_str())
                     return 1
 
             #*** ONLY FOR MULTIPART PAYMENT ***#
@@ -113,14 +120,14 @@
                 # return code is like paiement_pf[#N]
                 # put your code here (email sending / Database update)
                 # You have the amount of the payment part in Rpeification['montantech']
-                print 'cmcic', rep.reference, rep.return_code
+                pass
 
             elif rep.return_code.startswith("Annulation_pf"):
                 # Payment has been refused on the productive server for the part #N
                 # return code is like Annulation_pf[#N]
                 # put your code here (email sending / Database update)
                 # You have the amount of the payment part in Repification['montantech']
-                print 'cmcic', rep.reference, rep.return_code
+                pass
         else:
-            print "cmcic received invalid message (the HMAC doesn't match)."
+            self.warning("cmcic received invalid message %s %s" % (repr(rep.reference), repr(rep.return_code)))
         return 1