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