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

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

Fix state.

  • Property svn:keywords set to Id
File size: 3.6 KB
RevLine 
[7195]1## $Id: payment.py 12152 2014-12-05 17:13:45Z uli $
2##
[6864]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"""
[12134]21import decimal
[6864]22import grok
[12134]23import uuid
[6869]24from datetime import datetime
[9505]25from zope.event import notify
[8182]26from zope.component import getUtility
[8429]27from zope.i18n import translate
[11949]28from waeup.ikoba.interfaces import IIkobaUtils
29from waeup.ikoba.interfaces import MessageFactory as _
30from waeup.ikoba.payments.interfaces import (
[12134]31    IPayment, IOnlinePayment, STATE_UNPAID, STATE_FAILED, STATE_PAID,
[12140]32    )
[11949]33from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal
34from waeup.ikoba.utils.logger import Logger
[6864]35
[12135]36
[9769]37class Payment(grok.Container, Logger):
[6864]38    """This is a payment.
39    """
40    grok.implements(IPayment)
41    grok.provides(IPayment)
42    grok.baseclass()
43
[11949]44    logger_name = 'waeup.ikoba.${sitename}.payments'
[9769]45    logger_filename = 'payments.log'
46    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
47
[6864]48    def __init__(self):
49        super(Payment, self).__init__()
[8194]50        self.creation_date = datetime.utcnow()
[12134]51        self.payment_date = None
52        self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex)
53        self.amount = decimal.Decimal("0.00")
54        self.payed_item_id = None
55        self.payer_id = None
56        self.state = STATE_UNPAID
[6864]57        return
58
[12139]59    def approve(self, payment_date=None):
60        """A payment was approved.
[6869]61
[12139]62        Successful ending; the payment is marked as payed.
63
64        If `payment_date` is given, it must be a datetime object
65        giving a datetime in UTC timezone.
66
67        Raises ObjectModifiedEvent.
68        """
69        if payment_date is None:
70            payment_date = datetime.utcnow()
71        self.payment_date = payment_date
[12152]72        self.state = STATE_PAID
[12139]73        notify(grok.ObjectModifiedEvent(self))
74
75    def mark_failed(self, reason=None):
76        """Mark payment as failed.
77
78        Raises ObjectModifiedEvent.
79        """
[12152]80        self.state = STATE_FAILED
[12139]81        notify(grok.ObjectModifiedEvent(self))
82
83
[6864]84class OnlinePayment(Payment):
85    """This is an online payment.
86    """
87    grok.implements(IOnlinePayment)
88    grok.provides(IOnlinePayment)
89
[8420]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'
[8429]95        user = get_current_principal()
[8434]96        if user is None:
97            # in tests
98            usertitle = 'system'
99        else:
[8758]100            usertitle = getattr(user, 'public_name', None)
101            if not usertitle:
102                usertitle = user.title
[12135]103        r_desc = _('Payment approved by ${a}', mapping={'a': usertitle})
[11949]104        portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
105        self.r_desc = translate(r_desc, 'waeup.ikoba',
[8429]106            target_language=portal_language)
[8420]107        self.payment_date = datetime.utcnow()
[9505]108        # Update catalog
109        notify(grok.ObjectModifiedEvent(self))
[8420]110        return
111
[9984]112OnlinePayment = attrs_to_fields(OnlinePayment, omit=['display_item'])
Note: See TracBrowser for help on using the repository browser.