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

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

pep8.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1## $Id: payment.py 12641 2015-03-01 22:53:17Z 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 zope.component import getUtilitiesFor
26from zope.event import notify
27from waeup.ikoba.utils.helpers import attrs_to_fields
28from waeup.ikoba.payments.interfaces import (
29    IPayment, STATE_UNPAID, STATE_FAILED, STATE_PAID,
30    IPaymentGatewayService, IPayer, IPaymentItem, IPayee,
31    )
32from waeup.ikoba.utils.logger import Logger
33
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
51@attrs_to_fields
52class Payment(grok.Container, Logger):
53    """This is a payment.
54    """
55    grok.implements(IPayment)
56    grok.provides(IPayment)
57
58    logger_name = 'waeup.ikoba.${sitename}.payments'
59    logger_filename = 'payments.log'
60    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
61
62    def __init__(self):
63        super(Payment, self).__init__()
64        self.creation_date = datetime.utcnow()
65        self.payment_date = None
66        self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex)
67        #self.gateway_service = None
68        self.amount = decimal.Decimal("0.00")
69        #self.payed_item_id = None
70        #self.payer_id = None
71        self.state = STATE_UNPAID
72        return
73
74    def approve(self, payment_date=None):
75        """A payment was approved.
76
77        Successful ending; the payment is marked as payed.
78
79        If `payment_date` is given, it must be a datetime object
80        giving a datetime in UTC timezone.
81
82        Raises ObjectModifiedEvent.
83        """
84        if payment_date is None:
85            payment_date = datetime.utcnow()
86        self.payment_date = payment_date
87        self.state = STATE_PAID
88        notify(grok.ObjectModifiedEvent(self))
89
90    def mark_failed(self, reason=None):
91        """Mark payment as failed.
92
93        Raises ObjectModifiedEvent.
94        """
95        self.state = STATE_FAILED
96        notify(grok.ObjectModifiedEvent(self))
97
98    def add_payment_item(self, item):
99        """Add `item`
100
101        Returns the key under which the `item` was stored.
102        """
103        cnt = 0
104        while str(cnt) in self:
105            cnt += 1
106        self[str(cnt)] = item
107        return str(cnt)
108
109
110@attrs_to_fields
111class Payer(object):
112    """A Payment is for testing.
113
114    It cannot be stored in ZODB.
115    """
116    grok.implements(IPayer)
117
118
119@attrs_to_fields
120class PaymentItem(grok.Model):
121
122    grok.implements(IPaymentItem)
123
124    def __init__(self):
125        super(PaymentItem, self).__init__()
126
127
128@attrs_to_fields
129class Payee(object):
130    """Someone being paid.
131
132    This is for testing only and cannot be stored in ZODB.
133    """
134    grok.implements(IPayee)
Note: See TracBrowser for help on using the repository browser.