[7195] | 1 | ## $Id: payment.py 12277 2014-12-21 08:43:44Z 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 |
---|
[12277] | 25 | from zope.component import getUtilitiesFor |
---|
[9505] | 26 | from zope.event import notify |
---|
[11949] | 27 | from waeup.ikoba.payments.interfaces import ( |
---|
[12159] | 28 | IPayment, STATE_UNPAID, STATE_FAILED, STATE_PAID, |
---|
[12277] | 29 | IPaymentGatewayService, |
---|
[12140] | 30 | ) |
---|
[11949] | 31 | from waeup.ikoba.utils.logger import Logger |
---|
[6864] | 32 | |
---|
[12135] | 33 | |
---|
[12277] | 34 | def get_payment_providers(): |
---|
| 35 | """Get all services of payment gateways registered. |
---|
| 36 | """ |
---|
| 37 | return dict( |
---|
| 38 | getUtilitiesFor(IPaymentGatewayService) |
---|
| 39 | ) |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | class PaymentProviderServiceBase(grok.GlobalUtility): |
---|
| 43 | |
---|
| 44 | grok.baseclass() |
---|
| 45 | grok.implements(IPaymentGatewayService) |
---|
| 46 | |
---|
| 47 | title = u'Sample Credit Card Service' |
---|
| 48 | |
---|
| 49 | |
---|
[12163] | 50 | class Payment(grok.Model, Logger): |
---|
[6864] | 51 | """This is a payment. |
---|
| 52 | """ |
---|
| 53 | grok.implements(IPayment) |
---|
| 54 | grok.provides(IPayment) |
---|
| 55 | |
---|
[11949] | 56 | logger_name = 'waeup.ikoba.${sitename}.payments' |
---|
[9769] | 57 | logger_filename = 'payments.log' |
---|
| 58 | logger_format_str = '"%(asctime)s","%(user)s",%(message)s' |
---|
| 59 | |
---|
[6864] | 60 | def __init__(self): |
---|
| 61 | super(Payment, self).__init__() |
---|
[8194] | 62 | self.creation_date = datetime.utcnow() |
---|
[12134] | 63 | self.payment_date = None |
---|
| 64 | self.payment_id = u'PAY_' + unicode(uuid.uuid4().hex) |
---|
| 65 | self.amount = decimal.Decimal("0.00") |
---|
| 66 | self.payed_item_id = None |
---|
| 67 | self.payer_id = None |
---|
| 68 | self.state = STATE_UNPAID |
---|
[6864] | 69 | return |
---|
| 70 | |
---|
[12139] | 71 | def approve(self, payment_date=None): |
---|
| 72 | """A payment was approved. |
---|
[6869] | 73 | |
---|
[12139] | 74 | Successful ending; the payment is marked as payed. |
---|
| 75 | |
---|
| 76 | If `payment_date` is given, it must be a datetime object |
---|
| 77 | giving a datetime in UTC timezone. |
---|
| 78 | |
---|
| 79 | Raises ObjectModifiedEvent. |
---|
| 80 | """ |
---|
| 81 | if payment_date is None: |
---|
| 82 | payment_date = datetime.utcnow() |
---|
| 83 | self.payment_date = payment_date |
---|
[12152] | 84 | self.state = STATE_PAID |
---|
[12139] | 85 | notify(grok.ObjectModifiedEvent(self)) |
---|
| 86 | |
---|
| 87 | def mark_failed(self, reason=None): |
---|
| 88 | """Mark payment as failed. |
---|
| 89 | |
---|
| 90 | Raises ObjectModifiedEvent. |
---|
| 91 | """ |
---|
[12152] | 92 | self.state = STATE_FAILED |
---|
[12139] | 93 | notify(grok.ObjectModifiedEvent(self)) |
---|