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

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

Provide a basic PaymentItem? implementation.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1## $Id: payment.py 12461 2015-01-13 09:09:24Z 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,
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
51class Payment(grok.Model, Logger):
52    """This is a payment.
53    """
54    grok.implements(IPayment)
55    grok.provides(IPayment)
56
57    logger_name = 'waeup.ikoba.${sitename}.payments'
58    logger_filename = 'payments.log'
59    logger_format_str = '"%(asctime)s","%(user)s",%(message)s'
60
61    def __init__(self):
62        super(Payment, self).__init__()
63        self.creation_date = datetime.utcnow()
64        self.payment_date = None
65        self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex)
66        self.gateway_service = None
67        self.amount = decimal.Decimal("0.00")
68        self.payed_item_id = None
69        self.payer_id = None
70        self.state = STATE_UNPAID
71        return
72
73    def approve(self, payment_date=None):
74        """A payment was approved.
75
76        Successful ending; the payment is marked as payed.
77
78        If `payment_date` is given, it must be a datetime object
79        giving a datetime in UTC timezone.
80
81        Raises ObjectModifiedEvent.
82        """
83        if payment_date is None:
84            payment_date = datetime.utcnow()
85        self.payment_date = payment_date
86        self.state = STATE_PAID
87        notify(grok.ObjectModifiedEvent(self))
88
89    def mark_failed(self, reason=None):
90        """Mark payment as failed.
91
92        Raises ObjectModifiedEvent.
93        """
94        self.state = STATE_FAILED
95        notify(grok.ObjectModifiedEvent(self))
96
97
98@attrs_to_fields
99class PaymentItem(grok.Model):
100
101    grok.implements(IPaymentItem)
Note: See TracBrowser for help on using the repository browser.