1 | ## $Id: payment.py 12140 2014-12-04 04:03:33Z 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 | """ |
---|
19 | These are the payment tickets. |
---|
20 | """ |
---|
21 | import decimal |
---|
22 | import grok |
---|
23 | import uuid |
---|
24 | from datetime import datetime |
---|
25 | from zope.event import notify |
---|
26 | from zope.component import getUtility |
---|
27 | from zope.i18n import translate |
---|
28 | from waeup.ikoba.interfaces import IIkobaUtils |
---|
29 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
30 | from waeup.ikoba.payments.interfaces import ( |
---|
31 | IPayment, IOnlinePayment, STATE_UNPAID, STATE_FAILED, STATE_PAID, |
---|
32 | ) |
---|
33 | from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal |
---|
34 | from waeup.ikoba.utils.logger import Logger |
---|
35 | |
---|
36 | |
---|
37 | class Payment(grok.Container, Logger): |
---|
38 | """This is a payment. |
---|
39 | """ |
---|
40 | grok.implements(IPayment) |
---|
41 | grok.provides(IPayment) |
---|
42 | grok.baseclass() |
---|
43 | |
---|
44 | logger_name = 'waeup.ikoba.${sitename}.payments' |
---|
45 | logger_filename = 'payments.log' |
---|
46 | logger_format_str = '"%(asctime)s","%(user)s",%(message)s' |
---|
47 | |
---|
48 | def __init__(self): |
---|
49 | super(Payment, self).__init__() |
---|
50 | self.creation_date = datetime.utcnow() |
---|
51 | self.payment_date = None |
---|
52 | self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex) |
---|
53 | self.amount = decimal.Decimal("0.00") |
---|
54 | self.payed_item_id = None |
---|
55 | self.payer_id = None |
---|
56 | self.state = STATE_UNPAID |
---|
57 | return |
---|
58 | |
---|
59 | def approve(self, payment_date=None): |
---|
60 | """A payment was approved. |
---|
61 | |
---|
62 | Successful ending; the payment is marked as payed. |
---|
63 | |
---|
64 | If `payment_date` is given, it must be a datetime object |
---|
65 | giving a datetime in UTC timezone. |
---|
66 | |
---|
67 | Raises ObjectModifiedEvent. |
---|
68 | """ |
---|
69 | if payment_date is None: |
---|
70 | payment_date = datetime.utcnow() |
---|
71 | self.payment_date = payment_date |
---|
72 | self.payment_state = STATE_PAID |
---|
73 | notify(grok.ObjectModifiedEvent(self)) |
---|
74 | |
---|
75 | def mark_failed(self, reason=None): |
---|
76 | """Mark payment as failed. |
---|
77 | |
---|
78 | Raises ObjectModifiedEvent. |
---|
79 | """ |
---|
80 | self.payment_state = STATE_FAILED |
---|
81 | notify(grok.ObjectModifiedEvent(self)) |
---|
82 | |
---|
83 | |
---|
84 | class OnlinePayment(Payment): |
---|
85 | """This is an online payment. |
---|
86 | """ |
---|
87 | grok.implements(IOnlinePayment) |
---|
88 | grok.provides(IOnlinePayment) |
---|
89 | |
---|
90 | def __init__(self): |
---|
91 | super(OnlinePayment, self).__init__() |
---|
92 | p_id = None |
---|
93 | return |
---|
94 | |
---|
95 | def approve(self): |
---|
96 | "Approve online payment and set to paid." |
---|
97 | self.r_amount_approved = self.amount_auth |
---|
98 | self.r_code = u'AP' |
---|
99 | self.p_state = 'paid' |
---|
100 | user = get_current_principal() |
---|
101 | if user is None: |
---|
102 | # in tests |
---|
103 | usertitle = 'system' |
---|
104 | else: |
---|
105 | usertitle = getattr(user, 'public_name', None) |
---|
106 | if not usertitle: |
---|
107 | usertitle = user.title |
---|
108 | r_desc = _('Payment approved by ${a}', mapping={'a': usertitle}) |
---|
109 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
110 | self.r_desc = translate(r_desc, 'waeup.ikoba', |
---|
111 | target_language=portal_language) |
---|
112 | self.payment_date = datetime.utcnow() |
---|
113 | # Update catalog |
---|
114 | notify(grok.ObjectModifiedEvent(self)) |
---|
115 | return |
---|
116 | |
---|
117 | OnlinePayment = attrs_to_fields(OnlinePayment, omit=['display_item']) |
---|