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

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

Add interface methods in Payment class.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1## $Id: payment.py 12139 2014-12-04 04:01:49Z 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
37
38class Payment(grok.Container, Logger):
39    """This is a payment.
40    """
41    grok.implements(IPayment)
42    grok.provides(IPayment)
43    grok.baseclass()
44
45    logger_name = 'waeup.ikoba.${sitename}.payments'
46    logger_filename = 'payments.log'
47    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
48
49    def __init__(self):
50        super(Payment, self).__init__()
51        self.creation_date = datetime.utcnow()
52        self.payment_date = None
53        self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex)
54        self.amount = decimal.Decimal("0.00")
55        self.payed_item_id = None
56        self.payer_id = None
57        self.state = STATE_UNPAID
58        return
59
60    def approve(self, payment_date=None):
61        """A payment was approved.
62
63        Successful ending; the payment is marked as payed.
64
65        If `payment_date` is given, it must be a datetime object
66        giving a datetime in UTC timezone.
67
68        Raises ObjectModifiedEvent.
69        """
70        if payment_date is None:
71            payment_date = datetime.utcnow()
72        self.payment_date = payment_date
73        self.payment_state = STATE_PAID
74        notify(grok.ObjectModifiedEvent(self))
75
76    def mark_failed(self, reason=None):
77        """Mark payment as failed.
78
79        Raises ObjectModifiedEvent.
80        """
81        self.payment_state = STATE_FAILED
82        notify(grok.ObjectModifiedEvent(self))
83
84
85class OnlinePayment(Payment):
86    """This is an online payment.
87    """
88    grok.implements(IOnlinePayment)
89    grok.provides(IOnlinePayment)
90
91    def __init__(self):
92        super(OnlinePayment, self).__init__()
93        p_id = None
94        return
95
96    def approve(self):
97        "Approve online payment and set to paid."
98        self.r_amount_approved = self.amount_auth
99        self.r_code = u'AP'
100        self.p_state = 'paid'
101        user = get_current_principal()
102        if user is None:
103            # in tests
104            usertitle = 'system'
105        else:
106            usertitle = getattr(user, 'public_name', None)
107            if not usertitle:
108                usertitle = user.title
109        r_desc = _('Payment approved by ${a}', mapping={'a': usertitle})
110        portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
111        self.r_desc = translate(r_desc, 'waeup.ikoba',
112            target_language=portal_language)
113        self.payment_date = datetime.utcnow()
114        # Update catalog
115        notify(grok.ObjectModifiedEvent(self))
116        return
117
118OnlinePayment = attrs_to_fields(OnlinePayment, omit=['display_item'])
Note: See TracBrowser for help on using the repository browser.