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

Last change on this file since 12814 was 12808, checked in by Henrik Bettermann, 10 years ago

Henrik ist mutig.

  • Property svn:keywords set to Id
File size: 8.1 KB
RevLine 
[7195]1## $Id: payment.py 12808 2015-03-21 13:22:39Z henrik $
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"""
19These are the payment tickets.
20"""
[12311]21import decimal
[6864]22import grok
[12311]23import uuid
[6869]24from datetime import datetime
[12741]25from zope.catalog.interfaces import ICatalog
26from zope.component import getUtilitiesFor, getUtility
[9505]27from zope.event import notify
[12741]28from waeup.ikoba.interfaces import MessageFactory as _
[12461]29from waeup.ikoba.utils.helpers import attrs_to_fields
[11949]30from waeup.ikoba.payments.interfaces import (
[12311]31    IPayment, STATE_UNPAID, STATE_FAILED, STATE_PAID,
[12671]32    IPaymentGatewayService, IPayer, IPaymentItem, IPayee,
[12741]33    IPaymentGatewayServicesLister, IPayableFinder, IPayerFinder,
34    IPaymentWaitingForGatewayEvent, IPaymentFinishedEvent,
[12311]35    )
[6864]36
[12311]37
[12741]38def format_amount(amount, currency):
39    """Turn `amount`, `currency` into a readable string.
40    """
41    cncy_map = {'USD': u'US$', 'EUR': u'\u20ac', 'NGN': u'\u20a6'}
42    currency = cncy_map.get(currency, currency)
43    return '%s %s' % (currency, '{:,.2f}'.format(amount))
44
45
46def get_payment(payment_id):
47    """Get payment by payment id.
48
49    If no such payment can be found in catalog, return none.
50    """
51    cat = getUtility(ICatalog, name='payments_catalog')
52    result_set = [x for x in cat.searchResults(
53        payment_id=(payment_id, payment_id))]
54    if len(result_set):
55        return result_set[0]
56    return None
57
[12772]58
[12753]59def get_payments_from_payer_id(payer_id):
60    """Get payments by payer id.
[12741]61
[12753]62    If no such payment can be found in catalog, return none.
63    """
64    cat = getUtility(ICatalog, name='payments_catalog')
65    result_set = [x for x in cat.searchResults(
66        payer_id=(payer_id, payer_id))]
67    if len(result_set):
68        return result_set
69    return None
70
71
[12772]72def get_payments_from_payable_id(payable_id):
73    """Get payments by payer id.
74
75    If no such payment can be found in catalog, return none.
76    """
77    cat = getUtility(ICatalog, name='payments_catalog')
78    result_set = [x for x in cat.searchResults(
79        payable_id=(payable_id, payable_id))]
80    if len(result_set):
81        return result_set
82    return None
83
84
[12741]85def find_payable_from_payable_id(payable_id):
86    """Find a payable from its id.
87
88    Looks up all registered IPayableFinders and returns the first
89    positive result found.
90    """
91    for name, util in getUtilitiesFor(IPayableFinder):
92        result = util.get_payable_by_id(payable_id)
93        if result is not None:
94            return result
95    return None
96
97
98def find_payer_from_payer_id(payer_id):
99    """Find a payer from its id.
100
101    Looks up all registered IPayerFinders and returns the first
102    positive result found.
103    """
104    for name, util in getUtilitiesFor(IPayerFinder):
105        result = util.get_payer_by_id(payer_id)
106        if result is not None:
107            return result
108    return None
109
110
111def format_payment_item_values(payment_item_values, currency):
112    """Format tuples (description, currency, amount) for output.
113
114    `currency` passed in is the 'target' currency.
115
[12749]116    Returns a list of formatted values. Last item is total sum.
[12741]117    XXX: we do not really respect currency. If different items
118         have different currencies, we are choked.
119    """
120    result = []
121    total = decimal.Decimal("0.00")
122    for descr, item_currency, amount in payment_item_values:
123        total += amount
124        if item_currency != currency:
125            raise ValueError(
126                "Different currencies in payment items not supported.")
127        result.append((descr, '%s %0.2f' % (item_currency, amount)))
128    result.append((_('Total'), '%s %0.2f' % (currency, total)))
129    return result
130
131
[12311]132def get_payment_providers():
[12671]133    """Get all payment providers registered.
[12311]134    """
135    return dict(
136        getUtilitiesFor(IPaymentGatewayService)
[12671]137    )
[12311]138
139
[12741]140class PaymentWaitingForGatewayEvent(object):
141    grok.implements(IPaymentWaitingForGatewayEvent)
142
143    def __init__(self, obj):
144        self.object = obj
145
146
147class PaymentFinishedEvent(object):
148    grok.implements(IPaymentFinishedEvent)
149
150    def __init__(self, obj):
151        self.object = obj
152
153
[12671]154class PaymentGatewayServicesLister(grok.GlobalUtility):
155    grok.implements(IPaymentGatewayServicesLister)
156
157    def __call__(self):
158        """Get all services of payment gateways registered.
159        """
160        return get_payment_providers()
161
162
[12311]163class PaymentProviderServiceBase(grok.GlobalUtility):
[12741]164    """Base for IPaymentGatewayServices.
165    """
[12311]166    grok.baseclass()
167    grok.implements(IPaymentGatewayService)
168
169    title = u'Sample Credit Card Service'
170
[12741]171    def store(self, payment):
172        """Store `payment` in site.
173        """
174        site = grok.getSite()
175        payments = site['payments']
176        if payment.payment_id in payments:
177            del site['payments'][payment.payment_id]
178        site['payments'][payment.payment_id] = payment
[12311]179
[12741]180
[12766]181class Payment(grok.Model):
[6864]182    """This is a payment.
183    """
184    grok.implements(IPayment)
185    grok.provides(IPayment)
186
[12793]187    form_fields_interface = IPayment
188
[12774]189    @property
190    def amount(self):
191        """Total amount.
192
193        Sum of all contained payment items.
194        """
195        amounts = [item.amount for item in self.payment_items]
196        return sum(amounts, decimal.Decimal("0.00"))
197
[12741]198    def __init__(self, payer, payable, payee=None):
[6864]199        super(Payment, self).__init__()
[12774]200        items = tuple([x for x in payable.payment_items])
201        self.payment_items = items
[12741]202        self.payer_id = payer.payer_id
203        self.payable_id = payable.payable_id
204        self.title = payable.title
[8194]205        self.creation_date = datetime.utcnow()
[12311]206        self.payment_date = None
[12808]207        self.payment_id = unicode(uuid.uuid4().hex)
[12311]208        self.state = STATE_UNPAID
[12741]209        self.currency = payable.currency
210        if payee is not None:
211            self.payee_id = payee.payee_id
[6864]212        return
213
[12311]214    def approve(self, payment_date=None):
215        """A payment was approved.
[6869]216
[12311]217        Successful ending; the payment is marked as payed.
[6869]218
[12311]219        If `payment_date` is given, it must be a datetime object
220        giving a datetime in UTC timezone.
[9984]221
[12311]222        Raises ObjectModifiedEvent.
223        """
224        if payment_date is None:
225            payment_date = datetime.utcnow()
226        self.payment_date = payment_date
227        self.state = STATE_PAID
228        notify(grok.ObjectModifiedEvent(self))
[6864]229
[12311]230    def mark_failed(self, reason=None):
231        """Mark payment as failed.
[6864]232
[12311]233        Raises ObjectModifiedEvent.
234        """
235        self.state = STATE_FAILED
[9505]236        notify(grok.ObjectModifiedEvent(self))
[12461]237
[12787]238Payment = attrs_to_fields(Payment, omit="amount")
[12461]239
[12787]240
[12461]241@attrs_to_fields
[12671]242class Payer(object):
[12749]243    """A Payer for testing.
[12671]244
245    It cannot be stored in ZODB.
246    """
247    grok.implements(IPayer)
248
249
250@attrs_to_fields
[12741]251class PaymentItem(object):
[12461]252
253    grok.implements(IPaymentItem)
[12671]254
[12741]255    def __init__(
[12800]256            self, title=u"", amount=decimal.Decimal("0.00")):
[12671]257        super(PaymentItem, self).__init__()
[12741]258        self.title = title
259        self.amount = amount
[12671]260
[12790]261    def __repr__(self):
[12800]262        result = "%s(title=%r, amount=%r)" % (
263            self.__class__.__name__, self.title, self.amount)
[12790]264        return result
265
[12779]266    def to_string(self):
267        """A string representation that can be used in exports.
[12671]268
[12788]269        Returned is a unicode string of format
270        ``(u'<ITEM_ID>', u'<TITLE>', u'<AMOUNT>')``.
[12779]271        """
[12800]272        string = u"(u'%s', u'%s')" % (self.title, self.amount)
[12779]273        string = string.replace("u'None'", "None")
274        return string
275
276
[12671]277@attrs_to_fields
278class Payee(object):
279    """Someone being paid.
280
281    This is for testing only and cannot be stored in ZODB.
282    """
283    grok.implements(IPayee)
Note: See TracBrowser for help on using the repository browser.