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