[views] transform view cmcic into a controller cubicweb-cmcicpay-version-0.1.0
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Wed, 23 Jun 2010 20:34:18 +0200
changeset 5 1e1831e8ba3c
parent 4 c1b42fe1ff88
child 6 77ab4dfc5384
[views] transform view cmcic into a controller
views.py
--- a/views.py	Wed Jun 23 19:17:29 2010 +0200
+++ b/views.py	Wed Jun 23 20:34:18 2010 +0200
@@ -26,19 +26,6 @@
 
 from cubes.cmcicpay import cmcic
 
-# class CmcicController(controller.Controller):
-#     __regid__ = 'cmcic'
-
-#     def publish(self, rset=None):
-#         self.w(u'<h1>CM-CIC p@iement</h1>')
-#         tpe = cmcic.get_tpe(self._cw)
-#         self.w(u'<table>')
-#         for attr in ['numero','version','code_societe','url_paiement','url_ok','url_ko']:
-#             self.w(u'<tr><td>%s</td><td>%s</td></tr>' % (attr, getattr(tpe, attr)))
-#         self.w(u'</table>')
-#         self.w(repr(self._cw.form))
-
-
 def get_tpe(_cw):
     return cmcic.get_tpe(os.path.join(_cw.vreg.config.apphome,'tpe'))
 
@@ -46,64 +33,10 @@
 
 class ConfRewrite(SimpleReqRewriter):
     rules = [
-        (rgx('/cmcic'), dict(vid='cmcic')),
         (rgx('/cmcic_info'), dict(vid='cmcic_info')),
         ]
 ## views
 
-# make this a controller ? XXX
-class CmcicView(StartupView):
-    __regid__ = 'cmcic'
-    templatable = False
-    content_type = 'text/plain'
-
-    def call(self):
-        #print "Pragma: no-cache'
-        tpe = get_tpe(self._cw)
-        params = dict(self._cw.form)
-        del params['vid']
-        rep = tpe.read_paymentresponse(params)
-        msg, mac = tpe.paymentresponse_msg(rep)
-        if tpe.is_valid_msg(msg, mac):
-            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', rep.reference, rep.return_code
-                ack = 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', rep.reference, rep.return_code
-                comment = u'payed by %(brand)s on %(date)s, auth %(numauto)s' % rep.as_dict()
-                try:
-                    self._cw.session.cnx._repo.shopcart_checkout(rep.reference, comment)
-                    ack = 0
-                except ValidationError, exc:
-                    ack = 1
-
-            #*** ONLY FOR MULTIPART PAYMENT ***#
-            elif rep.return_code.startswith("paiement_pf"):
-                # Payment has been accepted on the productive server for the part #N
-                # 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
-                ack = 1
-
-            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
-                ack = 1
-        else:
-            print "cmcic: the HMAC doesn't match."
-            ack = 1 #\n" + mac
-        self.w(u'version=%s\ncdr=%s' % (tpe.version, ack))
-
 class CmcicInfoView(StartupView):
     __regid__ = 'cmcic_info'
     title = _('CMCIC paiement')
@@ -131,3 +64,59 @@
         session.commit()
     finally:
         session.close()
+
+class CmcicController(controller.Controller):
+    __regid__ = 'cmcic'
+
+    def publish(self, rset=None):
+        self._cw.set_content_type('text/plain')
+        self._cw.set_header('Pragma', 'no-cache')
+        tpe = get_tpe(self._cw)
+        try:
+            ack = self.handle_msg(tpe)
+        except Exception, exc:
+            print exc
+            ack = 1
+        return 'version=%s\ncdr=%s' % (tpe.version, ack)
+
+
+    def handle_msg(self, tpe):
+        params = dict(self._cw.form)
+        rep = tpe.read_paymentresponse(params)
+        msg, mac = tpe.paymentresponse_msg(rep)
+        if tpe.is_valid_msg(msg, mac):
+            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', 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', 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)
+                    return 0
+                except ValidationError, exc:
+                    return 1
+
+            #*** ONLY FOR MULTIPART PAYMENT ***#
+            elif rep.return_code.startswith("paiement_pf"):
+                # Payment has been accepted on the productive server for the part #N
+                # 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
+
+            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
+        else:
+            print "cmcic: the HMAC doesn't match."
+        return 1