PayzenService.py 3.41 KB
Newer Older
1
import zope
Łukasz Nowak's avatar
Łukasz Nowak committed
2
import hashlib
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type.XMLObject import XMLObject

class PayzenService(XMLObject):
  meta_type = 'Payzen Service'
  portal_type = 'Payzen Service'

  zope.interface.implements(interfaces.IPaymentService)

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.XMLObject
                    , PropertySheet.Reference
                    )
  def initialize(self, REQUEST=None, **kw):
    """See Payment Service Interface Documentation"""
    pass

  def navigate(self, REQUEST=None, **kw):
27 28 29 30 31 32
    """Navigation not implemented

    Payzen.eu assumes that POST is done directly to the website thus there is
    no need to provide "proxy" method.
    """
    raise NotImplementedError('Method will not be implemented')
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  def notifySuccess(self, REQUEST=None, **kw):
    """See Payment Service Interface Documentation"""
    raise NotImplementedError
    return self._getTypeBasedMethod("acceptPayment")(**kw)

  def notifyFail(self, REQUEST=None, **kw):
    """See Payment Service Interface Documentation"""
    raise NotImplementedError
    return self._getTypeBasedMethod("failInPayment")(**kw)

  def notifyCancel(self, REQUEST=None, **kw):
    """See Payment Service Interface Documentation"""
    raise NotImplementedError
    return self._getTypeBasedMethod("abortPayment")(**kw)
Łukasz Nowak's avatar
Łukasz Nowak committed
48 49 50

  # proposed methods
  def getFormString(self, document, **kw):
Łukasz Nowak's avatar
Łukasz Nowak committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    """Returns unterminated form for current document

    The responsiblity of the caller is to finish the form."""
    self.Base_checkConsistency()
    content_kw = dict()
    content_kw['vads_action_mode'] = self.getPayzenVadsActionMode()
    content_kw['vads_amount'] = int(document.getTotalPrice() * 100)
    integration_tool = self.restrictedTraverse(self.getIntegrationSite())
    content_kw['vads_currency'] = integration_tool.getMappingFromCategory(
      'resource/currency_module/%s' % document.getPriceCurrencyReference()
      ).split('/')[-1]
    content_kw['vads_ctx_mode'] = self.getPayzenVadsCtxMode()
    content_kw['vads_page_action'] = self.getPayzenVadsPageAction()
    content_kw['vads_payment_config'] = document\
      .Base_getPayzenServicePaymentConfig()
    content_kw['vads_site_id'] = self.getServiceUsername()
    # date as YYYYMMDDHHMMSS
    content_kw['vads_trans_date'] = document.getStartDate().strftime(
      '%Y%m%d%H%M%S')
    content_kw['vads_trans_id'] = document.Base_getPayzenTransId()
    content_kw['vads_version'] = self.getPayzenVadsVersion()
    # all data are completed, now it is time to create signature
    sorted_keys = content_kw.keys()
    sorted_keys.sort()
    signature = ''
    form = '<FORM METHOD="POST" ACTION="%s">\n' % self.getLinkUrlString()
    for k in sorted_keys:
      v = str(content_kw[k])
      signature += v + '+'
      form += '<INPUT TYPE="HIDDEN" NAME="%s" VALUE="%s">\n' % (k, v)
    signature += self.getServicePassword()
    form += '<INPUT TYPE="HIDDEN" NAME="signature" VALUE="%s">' % \
      hashlib.sha1(signature).hexdigest()
    return form
Łukasz Nowak's avatar
Łukasz Nowak committed
85 86 87 88 89 90

  def getSignature(self, document):
    """Returns signature for current document"""

  def validateSignature(self, document, signature):
    """Checks if documents validates against signature"""