Commit f5f3534c authored by Nicolas Chauvat's avatar Nicolas Chauvat
Browse files

[views] transform view cmcic into a controller

parent c1b42fe1ff88
......@@ -26,19 +26,6 @@ from cubicweb.web.views.urlrewrite import SimpleReqRewriter, rgx
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,22 +33,55 @@ def get_tpe(_cw):
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'
class CmcicInfoView(StartupView):
__regid__ = 'cmcic_info'
title = _('CMCIC paiement')
__select__ = StartupView.__select__ & match_user_groups('managers')
def call(self):
#print "Pragma: no-cache'
self.w(u'<h1>CM-CIC p@iement</h1>')
tpe = 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>')
# 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()
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)
del params['vid']
rep = tpe.read_paymentresponse(params)
msg, mac = tpe.paymentresponse_msg(rep)
if tpe.is_valid_msg(msg, mac):
......@@ -70,7 +90,7 @@ class CmcicView(StartupView):
# The payment may be accepted later
# put your code here (email sending / Database update)
print 'cmcic', rep.reference, rep.return_code
ack = 0
return 0
elif rep.return_code in ("payetest", "paiement"):
# Payment has been accepeted on the productive server
......@@ -78,10 +98,10 @@ class CmcicView(StartupView):
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
self.appli.repo.shopcart_checkout(rep.reference, comment)
return 0
except ValidationError, exc:
ack = 1
return 1
#*** ONLY FOR MULTIPART PAYMENT ***#
elif rep.return_code.startswith("paiement_pf"):
......@@ -90,7 +110,6 @@ class CmcicView(StartupView):
# 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
......@@ -98,36 +117,6 @@ class CmcicView(StartupView):
# 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')
__select__ = StartupView.__select__ & match_user_groups('managers')
def call(self):
self.w(u'<h1>CM-CIC p@iement</h1>')
tpe = 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>')
# 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()
return 1
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment