source: main/waeup.ikoba/branches/uli-fake-gw-provider/src/waeup/ikoba/payments/payment.py @ 12652

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

More Payment.amount tests.

  • Property svn:keywords set to Id
File size: 3.8 KB
RevLine 
[7195]1## $Id: payment.py 12648 2015-03-02 01:01:10Z 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"""
[12311]21import decimal
[6864]22import grok
[12311]23import uuid
[6869]24from datetime import datetime
[12311]25from zope.component import getUtilitiesFor
[9505]26from zope.event import notify
[12461]27from waeup.ikoba.utils.helpers import attrs_to_fields
[11949]28from waeup.ikoba.payments.interfaces import (
[12311]29    IPayment, STATE_UNPAID, STATE_FAILED, STATE_PAID,
[12636]30    IPaymentGatewayService, IPayer, IPaymentItem, IPayee,
[12311]31    )
[11949]32from waeup.ikoba.utils.logger import Logger
[6864]33
[12311]34
35def get_payment_providers():
36    """Get all services of payment gateways registered.
37    """
38    return dict(
39        getUtilitiesFor(IPaymentGatewayService)
40        )
41
42
43class PaymentProviderServiceBase(grok.GlobalUtility):
44
45    grok.baseclass()
46    grok.implements(IPaymentGatewayService)
47
48    title = u'Sample Credit Card Service'
49
50
[12636]51@attrs_to_fields
52class Payment(grok.Container, Logger):
[6864]53    """This is a payment.
54    """
55    grok.implements(IPayment)
56    grok.provides(IPayment)
57
[11949]58    logger_name = 'waeup.ikoba.${sitename}.payments'
[9769]59    logger_filename = 'payments.log'
60    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
61
[12646]62    @property
63    def amount(self):
64        """The amount of a payment.
65
66        Equals the sum of items contained.
67        """
[12648]68        return sum(
69            [item.amount for item in self.values()],
70            decimal.Decimal("0.00")  # default value
71        )
[12646]72
[6864]73    def __init__(self):
74        super(Payment, self).__init__()
[8194]75        self.creation_date = datetime.utcnow()
[12311]76        self.payment_date = None
77        self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex)
78        self.state = STATE_UNPAID
[6864]79        return
80
[12311]81    def approve(self, payment_date=None):
82        """A payment was approved.
[6869]83
[12311]84        Successful ending; the payment is marked as payed.
[6869]85
[12311]86        If `payment_date` is given, it must be a datetime object
87        giving a datetime in UTC timezone.
[9984]88
[12311]89        Raises ObjectModifiedEvent.
90        """
91        if payment_date is None:
92            payment_date = datetime.utcnow()
93        self.payment_date = payment_date
94        self.state = STATE_PAID
95        notify(grok.ObjectModifiedEvent(self))
[6864]96
[12311]97    def mark_failed(self, reason=None):
98        """Mark payment as failed.
[6864]99
[12311]100        Raises ObjectModifiedEvent.
101        """
102        self.state = STATE_FAILED
[9505]103        notify(grok.ObjectModifiedEvent(self))
[12461]104
[12640]105    def add_payment_item(self, item):
106        """Add `item`
[12461]107
[12643]108        Returns the key under which the `item` was stored. Please do
109        not make anby assumptions about the key. It will be a
110        string. That is all we can tell.
111
[12640]112        """
113        cnt = 0
114        while str(cnt) in self:
115            cnt += 1
116        self[str(cnt)] = item
117        return str(cnt)
118
119
[12461]120@attrs_to_fields
[12636]121class Payer(object):
122    """A Payment is for testing.
123
124    It cannot be stored in ZODB.
125    """
126    grok.implements(IPayer)
127
128
129@attrs_to_fields
[12461]130class PaymentItem(grok.Model):
131
132    grok.implements(IPaymentItem)
[12636]133
134    def __init__(self):
135        super(PaymentItem, self).__init__()
136
137
138@attrs_to_fields
139class Payee(object):
140    """Someone being paid.
141
142    This is for testing only and cannot be stored in ZODB.
143    """
144    grok.implements(IPayee)
Note: See TracBrowser for help on using the repository browser.