source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/payment.py @ 12134

Last change on this file since 12134 was 12134, checked in by uli, 10 years ago

Conform to interface and generate unique ids.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1## $Id: payment.py 12134 2014-12-04 02:29:29Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19These are the payment tickets.
20"""
21import decimal
22import grok
23import uuid
24from datetime import datetime
25from grok import index
26from zope.event import notify
27from zope.component import getUtility
28from zope.i18n import translate
29from waeup.ikoba.interfaces import IIkobaUtils
30from waeup.ikoba.interfaces import MessageFactory as _
31from waeup.ikoba.payments.interfaces import (
32    IPayment, IOnlinePayment, STATE_UNPAID, STATE_FAILED, STATE_PAID,
33    payment_states)
34from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal
35from waeup.ikoba.utils.logger import Logger
36
37class Payment(grok.Container, Logger):
38    """This is a payment.
39    """
40    grok.implements(IPayment)
41    grok.provides(IPayment)
42    grok.baseclass()
43
44    logger_name = 'waeup.ikoba.${sitename}.payments'
45    logger_filename = 'payments.log'
46    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
47
48    def logger_info(self, comment=None):
49        """Get the logger's info method.
50        """
51        self.logger.info('%s' % comment)
52        return
53
54    def __init__(self):
55        super(Payment, self).__init__()
56        self.creation_date = datetime.utcnow()
57        self.payment_date = None
58        self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex)
59        self.amount = decimal.Decimal("0.00")
60        self.payed_item_id = None
61        self.payer_id = None
62        self.state = STATE_UNPAID
63        return
64
65    @property
66    def p_state_title(self):
67        return payment_states.getTermByToken(self.p_state).title
68
69    @property
70    def category(self):
71        utils = getUtility(IIkobaUtils)
72        return utils.PAYMENT_CATEGORIES.get(self.p_category, None)
73
74    @property
75    def display_item(self):
76        ikoba_utils = getUtility(IIkobaUtils)
77        return ikoba_utils.getPaymentItem(self)
78
79class OnlinePayment(Payment):
80    """This is an online payment.
81    """
82    grok.implements(IOnlinePayment)
83    grok.provides(IOnlinePayment)
84
85    def __init__(self):
86        super(OnlinePayment, self).__init__()
87        p_id = None
88        return
89
90    def approve(self):
91        "Approve online payment and set to paid."
92        self.r_amount_approved = self.amount_auth
93        self.r_code = u'AP'
94        self.p_state = 'paid'
95        user = get_current_principal()
96        if user is None:
97            # in tests
98            usertitle = 'system'
99        else:
100            usertitle = getattr(user, 'public_name', None)
101            if not usertitle:
102                usertitle = user.title
103        r_desc = _('Payment approved by ${a}', mapping = {'a': usertitle})
104        portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
105        self.r_desc = translate(r_desc, 'waeup.ikoba',
106            target_language=portal_language)
107        self.payment_date = datetime.utcnow()
108        # Update catalog
109        notify(grok.ObjectModifiedEvent(self))
110        return
111
112OnlinePayment = attrs_to_fields(OnlinePayment, omit=['display_item'])
Note: See TracBrowser for help on using the repository browser.