receive and acknowledge server's response
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Wed, 23 Jun 2010 19:17:20 +0200
changeset 3 b81e32bb48d6
parent 2 80232b459741
child 4 c1b42fe1ff88
receive and acknowledge server's response
localtestserver.py
views.py
--- a/localtestserver.py	Tue Jun 22 22:14:44 2010 +0200
+++ b/localtestserver.py	Wed Jun 23 19:17:20 2010 +0200
@@ -19,6 +19,7 @@
 <p>Sending response:</p>
 %s
 <p>Response sent to %s and and got return value %s.</p>
+<p>Return to the site at <a href="%s">%s</a>.</p>
 '''
 
 def dict_as_html_table(map):
@@ -62,10 +63,14 @@
             body = index_html % (self.server.action,
                                  self.server.tpe.tpe_company,
                                  self.server.tpe.tpe_number)
+            if self.server.action == 'accept':
+                next_url = req.url_ok
+            else:
+                next_url = req.url_err
             body += pay_html % (dict_as_html_table(req.as_dict()),
                                 dict_as_html_table(rep.as_dict()),
                                 self.server.tpe.return_url,
-                                ack)
+                                ack, next_url, next_url)
             self.send_response(200)
             self.send_header("Content-type", 'text/html')
             self.end_headers()
--- a/views.py	Tue Jun 22 22:14:44 2010 +0200
+++ b/views.py	Wed Jun 23 19:17:20 2010 +0200
@@ -18,6 +18,7 @@
 
 import os
 
+from cubicweb import ValidationError
 from cubicweb.web import controller
 from cubicweb.selectors import match_user_groups
 from cubicweb.view import StartupView
@@ -50,6 +51,7 @@
         ]
 ## views
 
+# make this a controller ? XXX
 class CmcicView(StartupView):
     __regid__ = 'cmcic'
     templatable = False
@@ -68,14 +70,18 @@
                 # 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"):
+            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
-                cart = self._cw.entity_from_eid(rep.reference)
-                text = u'payed by %(brand)s on %(date)s, auth %(numauto)s' % rep.as_dict()
-                cart.fire_transition('check out', comment=text)
+                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"):
@@ -84,6 +90,7 @@
                 # 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
@@ -91,8 +98,8 @@
                 # 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 = 0
-        else :
+                ack = 1
+        else:
             print "cmcic: the HMAC doesn't match."
             ack = 1 #\n" + mac
         self.w(u'version=%s\ncdr=%s' % (tpe.version, ack))
@@ -109,3 +116,18 @@
         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>')
+
+# handle callback from payment server
+
+from cubicweb.server.repository import Repository
+from logilab.common.decorators import monkeypatch
+
+@monkeypatch(Repository)
+def shopcart_checkout(self, eid, comment):
+    session = self.internal_session()
+    try:
+        cart = session.entity_from_eid(eid)
+        cart.fire_transition('check out', comment=comment)
+        session.commit()
+    finally:
+        session.close()