source: main/waeup.ikoba/trunk/src/waeup/ikoba/payments/payment.py @ 12682

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

Merge changes from uli-fake-gw-provider back into trunk.

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