PaypalService.py 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
#                    François-Xavier Algrain <fxalgrain@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import zope
from urllib import urlencode
from urllib2 import urlopen, Request
from zLOG import LOG, DEBUG
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.Document import newTempDocument


class PaypalService(XMLObject):
  """Paypal Service for payment"""

  meta_type = 'Paypal Service'
  portal_type = 'Paypal Service'
  security = ClassSecurityInfo()
  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"""

60 61 62 63 64 65 66
  def _getFieldList(self, paypal_dict):
    field_list = []
    for k,v in paypal_dict.iteritems():
      field_list.append((k, v))
    return field_list

  def navigate(self, REQUEST=None, **kw):
67 68
    """See Payment Service Interface Documentation"""
    self.Base_checkConsistency()
69 70
    page_template = kw.pop("page_template")
    paypal_dict = kw.get("paypal_dict", {})
71 72 73
    temp_document = newTempDocument(self, 'id')
    temp_document.edit(
      link_url_string=self.getLinkUrlString(),
74 75
      title=self.getTitle(),
      field_list=self._getFieldList(paypal_dict),
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      # append the rest of transmitted parameters page template
      **kw
    )
    return getattr(temp_document, page_template)()

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

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

  def notifyCancel(self, redirect_path=None, REQUEST=None):
    """See Payment Service Interface Documentation"""
    return self._getTypeBasedMethod("abortPayment")(redirect_path=redirect_path)

  def reportPaymentStatus(self, REQUEST=None):
    """See Payment Service Interface Documentation"""
    param_dict = REQUEST.form
    LOG("PaypalService", DEBUG, param_dict)
    param_dict["cmd"] = "_notify-validate"
    if param_dict.has_key("service"):
      param_dict.pop("service")
    param_list = urlencode(param_dict)
    paypal_url = self.getLinkUrlString()
    request = Request(paypal_url, param_list)
    request.add_header("Content-type", "application/x-www-form-urlencoded")
    response = urlopen(request)
    status = response.read()
    method = self._getTypeBasedMethod("reportPaymentStatus")
    LOG("PaypalService status", DEBUG, status)
    if method and status == "VERIFIED":
      method(REQUEST=REQUEST)
    return True