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

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

Provide an overridable gateway lister.

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